유니티 게임 컨트롤러

게임의 핵심부분을 담당함

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
 
public class GameController : MonoBehaviour { 
    public static GameController controller; //controller 
 
    public int grenades = 0; //current grenades 
    public GameObject grenade; 
    public int removedGrenades = 0; 
 
    public int remainCoins = 4; //remain coins 
    public GameObject remainCoinText; 
 
    public GameObject middle; //middle texts and images 
    public GameObject youwin; 
    public GameObject gameover; 
 
    public GameObject player; //player 
 
    public GameObject crossHair; //crosshair image 
 
    public GameObject target; //target 
 
    public bool canShoot = false; //determine able to shoot 
 
    public bool isOver = false; 
 
    public GameObject Explosion; 
 
    void OnEnable() 
    { 
        controller = this; 
    } 
  // Use this for initialization 
  void Start () { 
   
  } 
 
    // Update is called once per frame 
    private float time; 
    private int timeflag = 1; 
    private Vector3 v3; 
    void Update () { 
      if(canShoot && !isOver) 
        { 
            v3 = target.transform.position; 
            time += Time.deltaTime; 
            if(time >= 3) 
            { 
                timeflag *= -1; 
                time = 0; 
            } 
            v3.x +=  Time.deltaTime*timeflag; 
            target.transform.position = v3; 
            //target.GetComponent<Rigidbody>().AddForce(new Vector3(timeflag, 0)); 
            //bool debug = true; 
            if (Input.GetButtonDown("Fire1") && grenades > 0) 
            { 
                GameObject ins = Instantiate(grenade, player.transform.position, player.transform.rotation) as GameObject; 
                ins.GetComponent<Rigidbody>().AddForce(player.transform.forward * 5000f); 
                remainCoinText.GetComponent<Text>().text = "보유 수류탄: " + (--grenades) + "개"; 
            } 
        } 
        if (remainCoins == 0 && !isOver && grenades == 0 && removedGrenades >= 4) 
        { 
            gameOver(false); 
        } 
    } 
 
    public int getCoin() 
    { 
        remainCoinText.GetComponent<Text>().text = "보유 수류탄: " + (++grenades) + "개"; 
        return --remainCoins; 
    } 
 
    public void changeShootStatus(bool flag) 
    { 
        canShoot = flag; 
        crossHair.GetComponent<Image>().enabled = canShoot; 
    } 
 
    //true : youwin 
    //false : gameover 
    public void gameOver(bool flag) 
    { 
        if(flag) 
        { 
            youwin.SetActive(true); 
            isOver = true; 
        } 
        else 
        { 
            gameover.SetActive(true); 
            isOver = true; 
        } 
    } 
} 

 

유니티 아이템관련 스크립트

using UnityEngine; 
using System.Collections; 
using UnityEngine.UI; 
 
public class GunHandler : MonoBehaviour { 
    public float rotationSpeed = 100.0f; 
 
    void Start() 
    { 
         
    } 
 
  // Update is called once per frame 
  void Update () { 
        transform.Rotate(new Vector3(0, rotationSpeed * Time.deltaTime, 0)); 
  } 
 
    void OnTriggerEnter(Collider target) 
    { 
        if (target.tag == "Player") 
        { 
            GameController.controller.middle.GetComponent<Text>().enabled = true; 
            GameController.controller.middle.GetComponent<Text>().text = "수류탄 구매됨. 코인 " + GameController.controller.getCoin() + "개 남음"; 
            Destroy(gameObject); 
        } 
    } 
} 

 

C# 스크립트 true/false

using UnityEngine; 
using System.Collections; 
 
public class CarController : MonoBehaviour { 
 
    public GameObject car; 
    //public float moveSpeed = 0.1f; 
    public Vector3 v3; 
    private bool flagB = true; 
    private int flagI = -1; //원래는 1이 기본값이었지만... 
    //차량 회전이 반대로 되서!! 
 
    float f; 
 
  // Use this for initialization 
  void Start () { 
        f = Random.Range(0.0f, 1.0f); //초기랜덤값 
    } 
   
  // Update is called once per frame 
  void Update () { 
        if(Input.GetKeyDown("space")) 
        { 
            f = Random.Range(0.0f, 1.0f); //스페이스바를 누를 때 마다 랜덤값이 바뀜 
            flagB = !flagB; //true는 false로 false는 true로 
            if(flagB) 
            { 
                flagI = -1; //-1이거나 1이거나 
            } 
            else 
            { 
                flagI = 1; 
            } 
            car.transform.Rotate(0.0f, (180.0f * flagI), 0.0f); //차량로테이트 
        } 
        v3.x += f*flagI; //벡터의 x에서의 이동속도만큼 감소... 
        //X * -1 = -X 
        //X * 1 = X  
        car.transform.position = v3; 
         
    } 
} 

 

C# 상속 및 접근 제한자

ParentClass.cs

using UnityEngine; 
using System.Collections; 
 
public class ParentClass : MonoBehaviour { 
 
    public int pubVar; 
    private int priVar; 
    protected int proVar; 
 
    public void PubFunction() 
    { 
        print("I am public"); 
    } 
 
    private void PriFunction() 
    { 
        print("I am private"); 
    } 
 
    protected void ProFunction() 
    { 
        print("I am protected"); 
    } 
} 

ChildClass.cs

using UnityEngine; 
using System.Collections; 
 
public class ChildClass : ParentClass { 
 
    void Start() 
    { 
        PubFunction(); 
 
        PriFunction(); //오류 발생!!! 
 
        ProFunction(); 
    } 
 
}