【Unity】ゲームの起動後 Awakeより前にメソッドを実行する
**参考URL:**http://tsubakit1.hateblo.jp/entry/2016/07/29/073000
1.タイプ指定しない(タイプ指定しないとOnEnableの後に呼び出される)
public class BootTest : MonoBehaviour
{
void Awake()
{
Debug.Log("Awake");
}
void OnEnable()
{
Debug.Log("OnEnable");
}
[RuntimeInitializeOnLoadMethod()]
static void Boot()
{
Debug.Log("Boot");
}
void Start()
{
Debug.Log("Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update");
}
}
◆ログ
Awake
OnEnable
Boot
Start
Update
2.タイプ指定する(RuntimeInitializeLoadType.BeforeSceneLoad)
※Awakeより前になる
public class BootTest : MonoBehaviour
{
void Awake()
{
Debug.Log("Awake");
}
void OnEnable()
{
Debug.Log("OnEnable");
}
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void Boot()
{
Debug.Log("Boot");
}
void Start()
{
Debug.Log("Start");
}
// Update is called once per frame
void Update()
{
Debug.Log("Update");
}
}
◆ログ
Boot
Awake
OnEnable
Start
Update
3.タイプ指定するが、Monovihaviorなし(RuntimeInitializeLoadType.BeforeSceneLoad)
public class NonMonobehavior
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
public static void Initial()
{
Debug.LogError("Initial");
}
}
◆ログ
Initial
Boot
Awake
OnEnable
Start
Update
※知らない間に呼び出されるので注意が必要、
知ってないとバグと誤認されるし、この処理にバグがあったら気づくまで大変かも
https://www.urablog.xyz/entry/2018/02/11/164734