페이지

2017년 7월 19일 수요일

GameObject Instatiated Lifecycle

GameObject Instatiated Lifecycle

When you call Instantiate on a prefab, the Awake() function is run immediately, but NOT the Start() function. The Start function is run sometime later before the first call to Update().



public class Cannon : MonoBehaviour{
    public GameObject prefabBullet;
    List<GameObject> list = new List<GameObject>();
    public void ReloadBullet(){
        GameObject bullet = Instantiate( prefabBullet );
        bullet.SetActive(false);
        list.Add(bullet);
    }

    public void Fire (){
        GameObject bullet = list[0];
        bullet.SetActive(true);
    }

}



//GameObject bullet has MyBullet component.

public class MyBullet : MonoBehaviour{

    void Awake(){
        Common.Log("MyBullet:Awake");
    }

    void Start(){
        Common.Log("MyBullet:Start");
    }

    void OnEnable(){
        Common.Log("MyBullet:OnEnable");
    }

    void OnDisable(){
        Common.Log("MyBullet:OnDisable");
    }

}



Cannon c;
c.ReloadBullet();

/*
MyBullet:Awake
MyBullet:OnEnable
MyBullet:OnDisable
 */



c.Fire();
/*
 MyBullet:OnEnable
 MyBullet:Start       // MyBullet:Start() is called before MyBullet:Update() is called
 */


2017년 7월 11일 화요일

Unity Animator



* 가급적 parameter , stateInfo 명은 같게 하자.
Animator parameter name : 
     State1,State2,State3
Animator StateInfo name : 
     ExBtnAnimatorState1, ExBtnAnimatorState2,     ExBtnAnimatorState3

   








    Animator ani;
    // Use this for initialization
    
    void Start () {
        ani = GetComponent<Animator>();
    }
    
    // Update is called once per frame
    void Update () {
        if(Input.GetKey("1")){
            if(!ani.GetCurrentAnimatorStateInfo(0).IsName("ExBtnAnimatorState1")){
                ani.SetTrigger("State1");
            } 
        }

    }






* Has Exit Time :
에니메이션이 끝날때까지 기다리고 다음 애니메이션으로 전환할지 여부 선택