: Unity script가 기본적으로 상속 받는 base class.
MonoBehaviour | Scripting APIl
Order of execution for event functions | Unity Manual
Initialization
Physics
Game Logic
Decommissioning
위는 약식으로 표현한 순서이므로 자세한건 Manual 참고 必
실행 순서를 알아보기 위해 다음과 같이 script를 구성하였다.
using UnityEngine;
public class UnityEventTest : MonoBehaviour
{
private void Awake() => Debug.Log($"{name} : {nameof(Awake)}");
private void OnEnable() => Debug.Log($"{name} : {nameof(OnEnable)}");
private void Start() => Debug.Log($"{name} : {nameof(Start)}");
private void FixedUpdate() => Debug.Log($"{name} : {nameof(FixedUpdate)}");
void Update()
{
Debug.Log($"Current Frame : {Time.frameCount}");
Debug.Log($"{name} : {nameof(Update)}");
}
private void LateUpdate() => Debug.Log($"{name} : {nameof(LateUpdate)}");
private void OnDisable()
{
Debug.Log($"{name} : {nameof(OnDisable)}");
}
private void OnDestroy()
{
Debug.Log($"{name} : {nameof(OnDestroy)}");
}
}
Frame 1
Frame 2
Frame 3
Frame 4
Component를 여러 개 삽입 후 각 이벤트가 어떻게 동작하는지 알아보았다. test code는 다음과 같다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UnityEventTest : MonoBehaviour
{
public int Id;
private void Awake() => Debug.Log($"{name} {Id} : {nameof(Awake)}");
private void OnEnable() => Debug.Log($"{name} {Id} : {nameof(OnEnable)}");
private void Start() => Debug.Log($"{name} {Id} : {nameof(Start)}");
private void FixedUpdate() => Debug.Log($"{name} {Id}: {nameof(FixedUpdate)}");
void Update()
{
Debug.Log($"Current Frame : {Time.frameCount}");
Debug.Log($"{name} {Id} : {nameof(Update)}");
}
private void LateUpdate() => Debug.Log($"{name} {Id} : {nameof(LateUpdate)}");
private void OnDisable()
{
Debug.Log($"{name} {Id} : {nameof(OnDisable)}");
}
private void OnDestroy()
{
Debug.Log($"{name} {Id} : {nameof(OnDestroy)}");
}
}
Awake, OnEnable : Awake, OnEnable이 순차적으로 진행된 다음 다른 Component가 실행되는 것을 확인할 수가 있다.
Start 또한 Initialization 단계에서 실행되는 event 함수이지만 Awake와 OnEnable과 함께 순차적으로 call 되지는 않았다.
Start 이후에도 각 component 단위로 이벤트 함수가 호출되는 것을 알 수가 있다.
OnDisable, OnDestroy 또한 연달아 불리는 것이 아니라 OnDisable이 끝나고 OnDestroy가 불려지는 것을 알 수가 있다.
Component 간의 호출 순서는 정해져 있지 않다. 이는 GameObject도 마찬가지이다. 순서를 명시하고 싶으면 스크립트로 동적으로 할당 하는 것이 일반적이다.