C#, Unity

C#, Unity) Activation / Diactivation Functions - SetActive, enabled, activeSelf, OnEnable, OnDisable

나무늘보섬 2024. 8. 22. 14:39

SetActive(true or false) 

  • GameObject 자체의 활성화/ 비활성화 담당
  • gameobject.SetActive(bool value);

 

activeSelf

  • 현재 GameObject의 true, ffalse 값을 bool로 나타낸 것 + read only 전용
  • GameObject.SetActive(true) 면 true, GameObject.SetActive(false) 면 false
  • bool isActive = gameObject.activeSelf;

 

enabled

  • GameObject 내에 있는 Component의 활성화/ 비활성화 담당
  • gameobject.GetComponent<Component>().enabled = true or false;

 

SetActive vs enabled 

  • GameObject 전체를 활성화 / 비활성화 or Gameobject내의 Component를 활성화 / 비활성화 하느냐의 차이

 


 

 

OnEnable

  • 활성화 (= gameobject.SetActive(true)) 될 때마다 초기화 되면서 호출되는 함수 + Start() 함수 전에 실행 
  • Start함수와의 차이
    • Start() : 최초 실행 1번만 초기화
    • OnEnable() : 호출될 때마다 초기화 
  • 실행 순서: OnEnable -> Start -> Update

 

OnDisable

  • 비활성화 (= gameobject.SetActive(false)) 될 때마다 초기화되면서 호출되는 함수

 

※ (OnEnable 과 OnDisable은 Inspector창의 체크박스 담당)