[유니티] 게임오브젝트의 흐름

YongSeok·2022년 7월 15일
0

📌유니티 생명주기



✏️ 초기화 영역

  • Awake
    • 게임 오브젝트가 생성이 될때, 최초 한번만 실행
  • Start
    • 업데이트 시작 직전, 최초 한번만 실행
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("Awake 함수 실행");
    }
    
    void Start()
    {
        Debug.Log("Start 함수 실행");
    }
}

👇 실행결과


✏️ 활성화 영역

  • OnEnable
    • 게임 오브젝트가 활성화 되었을때
    • Awake보다는 늦게 Start보다는 빠르게 실행된다
    • 켜고 끄고 할때마다 실행된다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void OnEnable()
    {
        Debug.Log("OnEnable 함수 실행");
    }
}

✏️ 물리 영역

  • FixedUpdate
    • 유니티 엔진이 물리연산을 하기 전에 실행되는 업데이트 함수
    • 고정된 실행 주기로 CPU를 많이 사용
    • 물리연산과 관련된 로직을 넣는다
    • 50프레임 고정 실행
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void FixedUpdate()
    {
        Debug.Log("FixedUpdate 함수 실행");
    }
}

👇 실행결과


✏️ 게임 로직 영역

  • Update
    • 환경에 따라 실행 주기가 떨어질 수 있음
    • 일반적으로 60프레임으로 실행
  • LateUpdate
    • 모든 업데이트영역이 끝난 후 실행
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void Update()
    {
        Debug.Log("Update 함수 실행");
    }

    void LateUpdate()
    {
        Debug.Log("LateUpdate 함수 실행");
    }
}

👇 실행결과


✏️ 비활성화 영역

  • OnDisable
    • 게임 오브젝트가 비활성화 되었을때
    • 삭제될때도 실행된다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void OnDisable()
    {
        Debug.Log("OnDisable 함수 실행");
    }
}

✏️ 해체 영역

  • OnDestroy
    • 오브젝트가 삭제가 되기 직전에 무언가 남기고 삭제된다는 느낌
    • Awake와 반대가 되는 개념으로 이해할 수도 있다
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LifeCycle : MonoBehaviour
{
    void OnDestroy()
    {
        Debug.Log("OnDestroy 함수 실행");
    }
} // 하이어라키창에서 오브젝트를 삭제될 경우 로그 내용 실행

0개의 댓글