Basic C# for Unity

nananana·2022년 9월 19일
0

Tried out basic C# on Unity
Youtube Lecture used #1
Youtube Lecture used #2
Wrote scrypts following(?) youtube excersise.
Coding in C# in general was like C with a Java like taste which was not quite C++.

NewBehaviourScripts.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{ 
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Hello World");
        Debug.Log("I am " + gameObject.name);
        int lv = 1;
        float strength = 5.5f;
        string playerName = "steve";
        bool isOnGround = true;

        Debug.Log(lv);
        Debug.Log(strength);
        Debug.Log(playerName);
        Debug.Log(isOnGround);

        string[] tool = {"sword", "bag", "flask"};
        for(int i = 0; i < tool.Length; i++){
            Debug.Log(tool[i]);
        }

        int[] monsterLv = {1,2,3,4,5};
        int[] heroLv = new int[5];
        for(int i = 0; i < monsterLv.Length; i++){
            heroLv[i] = monsterLv.Length - monsterLv[i];
            Debug.Log(monsterLv[i] + " vs " + heroLv[i]);
        }

        List<string> items = new List<string>();
        for(int i = 0; i < 16; i++){
            items.Add(gameObject.name + i);
        }
        for(int i = 0; i < items.Count; i++){
            Debug.Log(items[i]);
        }
        Debug.Log("==================================");
        List<int> tmp = new List<int>();
        for(int i = 0; i < items.Count; i++){
            if(i%3==0){
                tmp.Add(i);
            }
        }
        int placement = 0;
        for(int i = 0; i < tmp.Count; i++){
            items.RemoveAt(tmp[i] - placement);
            placement ++;
        }
        for(int i = 0; i < items.Count; i++){
            Debug.Log(items[i]);
        }


        int maxLv = 100;
        int curLv = 10;
        bool isMaxLv = maxLv == curLv;
        while(!isMaxLv){
            curLv += 10;
            isMaxLv = maxLv <= curLv;
            if(isMaxLv){
                Debug.Log("Player is currently max level: " + curLv + "/" + maxLv);
            }else{
                Debug.Log("Player is not currently max level: " + curLv + "\n" + "insufficient by: " + (maxLv - curLv));
            }
        }


        int val = 0;
        for(int i = 0; i < 20; i++){
            val++;
            bool multOfThree = (val % 3 == 0);
            string state = multOfThree ? "wow" : "cool";
            Debug.Log(state);
        }


        foreach (string elem in tool){
            Debug.Log(elem);
        }

        // Actor player = new Actor();
        Player player = new Player();
        player.id = 5728194;
        player.actorName = "Josh";
        Debug.Log(player.Talk());
        if(!player.HasWeapon()){
            // Debug.Log("if statement hit");
            player.ReceiveWeapon("Josh's sword");
        }
        Debug.Log(player.weaponName);
        Debug.Log(player.WeaponName());
        Debug.Log(player.getLv());
        player.LevelUp();
        Debug.Log(player.getLv());

        Debug.Log(player.test("well hello there"));


        Debug.Log(player.move());
    }

    // Update is called once per frame
    // void Update()
    // {
        
    // }
}

Actor.cs

public class Actor {
    public int id;
    public string weaponName;
    int weaponId = -1;
    int lv;
    public string actorName;

    public string Talk(){
        return "Hello There! I am " + actorName;
    }

    public string WeaponName(){
        return weaponName;
    }

    public void ReceiveWeapon(string tmpName){
        weaponName = tmpName;
        weaponId = 782195;
    }

    public string test(string tests){
        return tests;
    }

    public bool HasWeapon(){
        if(weaponId == -1){
            return false;
        }else{
            return true;
        }
    }

    public int getLv(){
        return lv;
    }

    public void LevelUp(){
        lv ++;
    }

}

Player.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : Actor{
    public string move(){
        return "Moved";
    }
}

LifeCycle.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour{
    void Awake(){
        Debug.Log("Finished Loading Up All Data");
    }

    void OnEnable(){
        //Executed Everytime Enabled
        Debug.Log("Logged in");
    }

    void Start(){
        Debug.Log("Starting Fnct.");
    }

    void FixedUpdate(){
        //Before Physical Update
        //Set Fixed Frame => Constant set time, use a lot of cpu 
        Debug.Log("Move");
    }

    void Update(){
        //Game Logic 
        //Depending on environment execution will vary
        Debug.Log("update");
    }

    void LateUpdate(){
        //Execute after all update executed
        //camera or aftermath of logic

        Debug.Log("I am late again");
    }

    void OnDisable(){
        Debug.Log("Logged Out");
    }

    void OnDestroy(){
        //before object is destroyed
        //Opposite of awake
        Debug.Log("Finalizing everything. Have a bad day :D");
    }
}

General Logic similar to what seen in arduino. A function that initialize executes once and a update function that executes constantly.
Multiple update functions for different purpose.
General grammer similar to C
Style similar to Java
Object Oriented Programming... never understood it quite well.

0개의 댓글