페이지

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 :
에니메이션이 끝날때까지 기다리고 다음 애니메이션으로 전환할지 여부 선택



2017년 6월 12일 월요일

GetComponentInChildren method can get also parent's Component

GetComponentInChildren,GetComponentsInChildren  methods can get all components in parent and childrens. So if the component that you want to get is in both parent and children, this method can get component in parent at first.


 GameObject <ParticleSystem> : Parent
     GameObject <ParticelSystem> : Child


ParticleSystem ptc = GetComponentInChildren<ParticleSystem>();
Debug.Log(ptc.name);  // "Parent"


solution:

foreach(ParticleSystem ptc in GetComponentsInChildren<ParticleSystem>())

{

     if( ptc.name == "Parent"){

       //do something by parent

     } else if (ptc.name == "Child"){

         //do something by Child

     }

}

*
 i think this method naming is wrong. many people get confused.

2017년 5월 30일 화요일

Unity PropertyDrawer ( Custom instpector)






[System.Serializable]
public struct FloatRange{

    //getter,setter방식은 FindPropertyRelative에서 못찾아내서 사용안함.
    public float value,min,max;

    public FloatRange(float pValue, float pMin, float pMax ){
        value = pValue;
        min = pMin;
        max = pMax;
    }
}


[CustomPropertyDrawer(typeof(FloatRange))]
public class FloatRangeDrawer : PropertyDrawer {
 
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label){
        label = EditorGUI.BeginProperty(position, label, property);
        Rect contentPosition = EditorGUI.PrefixLabel(position, label);
        contentPosition.width *= 0.33f;
        EditorGUI.indentLevel = 0;
        EditorGUIUtility.labelWidth = 14f;
        //value
        EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("value"), GUIContent.none);

        //min
        contentPosition.x += contentPosition.width;
        EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("min"), new GUIContent(","));
     
        //max
        contentPosition.x += contentPosition.width;
        EditorGUI.PropertyField(contentPosition, property.FindPropertyRelative("max"), new GUIContent("~"));

        EditorGUI.EndProperty();
    }
}

2017년 5월 17일 수요일

cocoapods install and delete

install pods

$ pod init
//make Podfile
$ sudo nano Podfile
$ pod install


delete pods
$ sudo gem install cocoapods-deintegrate cocoapods-clean
$ pod deintegrate
$ pod clean
$ rm Podfile