コガネブログ

平日更新を目標に Unity や C#、Visual Studio、ReSharper などのゲーム開発アレコレを書いていきます

【Unity】各スクリプトの実行順(Script Execution Order)をプログラムで設定できる「Simple Ordering」紹介(無料)

f:id:baba_s:20171107210210p:plain

概要

2017/4/11 に「Simple Ordering」がリリースされました

「Simple Ordering」を導入すると、
各スクリプトの実行順(Script Execution Order)を
プログラムで設定できるようになります

検証環境

  • Unity 2017.1.1f1
  • Simple Ordering 1.3

使用例

using com.kupio.declarativeorder;
using UnityEngine;

[RunFirst]
public class Example1 : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log( "Example1" );
    }
}

RunFirst 属性を適用したスクリプトは一番最初に実行されます

using com.kupio.declarativeorder;
using UnityEngine;

[RunBefore( typeof( Example3 ) )]
public class Example2 : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log( "Example2" );
    }
}

RunBefore 属性を適用したスクリプトは、指定したスクリプトの前に実行されます

using com.kupio.declarativeorder;
using UnityEngine;

[RunAfter( typeof( Example2 ) )]
public class Example3 : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log( "Example3" );
    }
}

RunAfter 属性を適用したスクリプトは、指定したスクリプトの後に実行されます

using com.kupio.declarativeorder;
using UnityEngine;

[RunLast]
public class Example4 : MonoBehaviour
{
    private void Awake()
    {
        Debug.Log( "Example4" );
    }
}

RunLast 属性を適用したスクリプトは一番最後に実行されます

f:id:baba_s:20171107211010p:plain

コンパイル完了後に Unity メニューの
「Edit>Project Settings>Script Execution Order」を確認すると
設定した属性を元にスクリプトの実行順が反映されていることがわかります

関連記事