티스토리 뷰
[RuntimeInitializeOnLoadMethod] Unity 런타임 초기화 method 정의를 위한 attribute
progrunman 2022. 10. 20. 23:16개요
게임이 실행될때 초기화를 위한 작업이 필요하다면 `RuntimeInitializeOnLoadMethod`라는 attribute를 사용하여 설정 가능하다. 해당 어트리뷰트로 설정된 static method는 게임이 로드된 후 호출된다.(invoked)
Assets폴더 하위 C# 스크립트의 static method 중 하나에 해당 attribute를 설정해서 정의 가능하다. Scene에 올라가서 동작하는게 아니기 때문에 `MonoBehaviour`와 같은 Unity클래스 외 C#클래스 어느곳에 정의해도 무방하다.
예를들면 Assets root폴더에 AppMain.cs 스크립트를 만들고 아래와 같이 설정 가능하다.
using UnityEngine;
internal class AppMain
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)]
internal static void InitializeAfterSubsystemRegistration()
{
// input, display 같은 subsystem이 등록된 후 호출
// Unity document에 정확한 설명이 없어서 확실하지 않음
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSplashScreen)]
internal static void InitializeBeforeSplashScreen()
{
// 스플래시 화면 직전에 호출(e.g. 유니티 로고화면)
}
}
이외에도 시점을 정할 수 있는 몇가지 `RuntimeInitializeLoadType`옵션이 존재하며 프로젝트 성격에 맞게 시점마다 초기화 로직을 할당해서 사용하면 된다. `RuntimeInitializeLoadType`를 설정하지 않았다면 `Awake()` method가 호출된 후 호출된다. 여러개의 클래스에 `RuntimeInitializeOnLoadMethod` 어트리뷰트를 정의했다면 각각의 실행순서는 보장되지 않는다는점에 유의하도록 한다.
실행순서
실행순서는 아래와 같다.
------------------------------------------------------------------------------------------------------------------------------------
SubsystemRegistration
AfterAssembliesLoaded
BeforeSplashScreen
BeforeSceneLoad
----------------------------------
// Scene에 로드되는 객체들
Awake()
OnEnable()
----------------------------------
RuntimeInitializeLoadType를 설정안한 기본 RuntimeInitializeOnLoadMehotd // 호출순서가 항상 같지 않음에 유의
AfterSceneLoad
----------------------------------
// Scene에 로드되는 객체들
Start()
------------------------------------------------------------------------------------------------------------------------------------
주의할점은 RuntimeinitializeLoadType을 설정안한 method는 호출순서가 항상 정확하지는 않다는 것이다.
왠만하면 RuntimeinitializeLoadType을 명시적으로 설정하고 사용하는것이 좋다.
/*
참고로 해당 내용들은 Library하위에 RuntimeInitializeOnLoads.json으로 생성되는듯하며, 설정된 내용은 해당 파일에서 확인해볼 수 있다.
- e.g. "\Library\BuildPlayerData\Player\RuntimeInitializeOnLoads.json", "\Library\PlayerDataCache\Android\Data")
*/
추가정보
추가로 초기화 단계의 Subsystem과 Assembly가 의미하는바는 아래와 같다.
- Subsystems
- Unity Engine의 특정부분을 담당하는 추상화 및 모듈화된 기능 sets
- Unity Engine에 포함되는 Unity Editor의 built-in패키지(e.g. Rendering, Physics, Input, Audio, UI, Scripting, etc)
- SubsystemRegistration단계에서 Unity는 `ISubsystemDescriptor`를 구현하는 클래스를 검색해서 서브시스템으로 등록
- 이후 등록된 서브시스템들을 Application에서 사용
- Assemblies
- Unity 프로젝트에 포함된 managed assemblies
- 런타임에 동적으로 로드되는 추가 assemblies (e.g. reflection, dynamic code generation, etc)
결론
즉, 앱 초기화 코드는 최소한 AfterAssembliesLoaded 이후에 적용하는것이 좋아보인다(e.g. BeforeSceneLoad)
References
https://docs.unity3d.com/ScriptReference/RuntimeInitializeOnLoadMethodAttribute.html
Unity - Scripting API: RuntimeInitializeOnLoadMethodAttribute
RuntimeInitializeOnLoadMethodAttribute class in UnityEngine / Inherits from:Scripting.PreserveAttribute / Implemented in:UnityEngine.CoreModule Description Allow a runtime class method to be initialized when a game is loaded at runtime without action from
docs.unity3d.com
https://gist.github.com/hybridherbst/36ae70b6520981c8edc7b478423fae5e
[RuntimeInitializeOnLoad] Event Order
[RuntimeInitializeOnLoad] Event Order. GitHub Gist: instantly share code, notes, and snippets.
gist.github.com
'Game > 개발' 카테고리의 다른 글
[Unity] Addressables (v1.19.19 기준 - Editor 2021.3) (0) | 2023.03.23 |
---|---|
[Unity] Assembly Definition (어셈블리 정의) (0) | 2023.03.16 |
[Unity] Preprocessor directives & Conditional Compilation (전처리기 지시문 및 조건부 컴파일) (0) | 2023.03.15 |
[Unity] 엔진의 주요 event 함수 정리 (Awake(), Start(), etc) (2) | 2023.03.10 |
[Unity/C#] Singleton Pattern (싱글톤) (0) | 2023.02.14 |
- Total
- Today
- Yesterday
- await
- coroutine
- 유니티
- selenium
- Debug
- Visual Studio Code
- 코루틴
- gcp
- 환경설정
- RuntimeInitializeOnLoadMethod
- Singleton
- C#
- github
- unity
- logging
- .net
- Scraping
- git
- Custom Package
- Addressables
- 비동기
- firestore
- Python
- framework
- 싱글톤
- VS2022
- initialize
- async
- vscode
- 닷넷
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |