0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Unity Awakeより前にメソッドを呼ぶ

Last updated at Posted at 2020-07-03

【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

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?