unity3d Editor attributes


Example

[InitializeOnLoad]
public class AttributesExample : MonoBehaviour
{
    
    static AttributesExample() 
    {
        [...]
    }

    [InitializeOnLoadMethod]
    private static void Foo()
    {
        [...]
    }
}

[InitializeOnLoad]
public class AttributesExample : MonoBehaviour
{
    
    static AttributesExample() 
    {
        [...]
    }

The InitializeOnLoad attribute allows the user to initialize a class without any interaction from the user. This happens whenever the editor launches or on a recompile. The static constructor guarantees that this will be called before any other static functions.

[InitializeOnLoadMethod]
private static void Foo()
{
    [...]
}

The InitializeOnLoad attribute allows the user to initialize a class without any interaction from the user. This happens whenever the editor launches or on a recompile. The order of execution is not guaranteed for methods using this attribute.


[CanEditMultipleObjects]
public class AttributesExample : MonoBehaviour 
{

    public int MyInt;

    private static string prefsText = "";

    [PreferenceItem( "My Prefs" )]
    public static void PreferencesGUI()
    {
        prefsText = EditorGUILayout.TextField( "Prefs Text", prefsText );
    }

    [MenuItem( "Attributes/Foo" )]
    private static void Foo()
    {
        [...]
    }

    [MenuItem( "Attributes/Foo", true )]
    private static bool FooValidate() 
    {
        return false;
    }
}

The result of the [PreferenceItem] attribute

Result 2

The result of the [MenuItem] attribute

Result 3


[CanEditMultipleObjects]
public class AttributesExample : MonoBehaviour 

The CanEditMultipleObjects attribute allows you to edit values from your component over multiple GameObjects. Without this component you won't see your component appear like normal when selecting multiple GameObjects but instead you will see the message "Multi-object editing not supported"

This attribute is for custom editors to support multi editing. Non-custom editors automatically support multi editing.

[PreferenceItem( "My Prefs" )]
public static void PreferencesGUI()

The PreferenceItem attribute allows to you create an extra item in Unity's preferences menu. The receiving method needs to be static for it to be used.

[MenuItem( "Attributes/Foo" )]
private static void Foo()
{
    [...]
}

[MenuItem( "Attributes/Foo", true )]
private static bool FooValidate() 
{
    return false;
}

The MenuItem attribute allows you to create custom menu items to execute functions. This example uses a validator function as well (which always returns false) to prevent execution of the function.


[CustomEditor( typeof( MyComponent ) )]
public class AttributesExample : Editor
{
    [...]
}

The CustomEditor attribute allows you to create custom editors for your components. These editors will be used for drawing your component in the inspector and need to derive from the Editor class.

[CustomPropertyDrawer( typeof( MyClass ) )]
public class AttributesExample : PropertyDrawer 
{
    [...]
}

The CustomPropertyDrawer attribute allows you to create a custom property drawer for in the inspector. You can use these drawers for your custom data types so that they can be seen used in the inspector.

[DrawGizmo( GizmoType.Selected )]
private static void DoGizmo( AttributesExample obj, GizmoType type )
{
    [...]
}

The DrawGizmo attribute allows you to draw custom gizmos for your components. These gizmos will be drawn in the Scene View. You can decide when to draw the gizmo by using the GizmoType parameter in the DrawGizmo attribute.

The receiving method requires two parameters, the first is the component to draw the gizmo for and the second is the state that the object who needs the gizmo drawn is in.