unity3d Runtime attributes


Example

[ExecuteInEditMode]
public class AttributesExample : MonoBehaviour 
{

    [RuntimeInitializeOnLoadMethod]
    private static void FooBar() 
    {
        [...]
    }

    [RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.BeforeSceneLoad )]
    private static void Foo() 
    {
        [...]
    }

    [RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.AfterSceneLoad )]
    private static void Bar() 
    {
        [...]
    }

    void Update() 
    {
        if ( Application.isEditor )
        {
            [...]
        } 
        else
        {
            [...]
        }
    }
}

[ExecuteInEditMode]
public class AttributesExample : MonoBehaviour 

The ExecuteInEditMode attribute forces Unity to execute this script's magic methods even while the game is not playing.

The functions are not constantly called like in play mode

  • Update is only called when something in the scene changed.
  • OnGUI is called when the Game View receives an Event.
  • OnRenderObject and the other rendering callback functions are called on every repaint of the Scene View or Game View.
[RuntimeInitializeOnLoadMethod]
private static void FooBar()

[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.BeforeSceneLoad )]
private static void Foo() 

[RuntimeInitializeOnLoadMethod( RuntimeInitializeLoadType.AfterSceneLoad )]
private static void Bar() 

The RuntimeInitializeOnLoadMethod attribute allows a runtime class method to be called when the game loads the runtime, without any interaction from the user.

You can specify if you want the method to be invoked before or after scene load (after is default). The order of execution is not guaranteed for methods using this attribute.