Version 3.3.1.2

OdinValueDrawer<T> class

Namespace: Sirenix.OdinInspector.Editor
Assembly: Sirenix.OdinInspector.Editor
public abstract class OdinValueDrawer<T> : OdinDrawer

Base class for all value drawers. Use this class to create your own custom drawers for any specific type.

Remember to provide your custom drawer with an Sirenix.OdinInspector.Editor.OdinDrawerAttribute in order for it to be located by the .

Odin supports the use of GUILayout and takes care of undo for you. It also takes care of multi-selection in many simple cases. Checkout the manual for more information on handling multi-selection.

Inheritance
Remarks

Checkout the manual for more information.

Example
public class MyCustomBaseType
{

}

public class MyCustomType : MyCustomBaseType
{

}

// Remember to wrap your custom attribute drawer within a #if UNITY_EDITOR condition, or locate the file inside an Editor folder.

public sealed class MyCustomBaseTypeDrawer<T> : OdinValueDrawer<T> where T : MyCustomBaseType
{
    protected override void DrawPropertyLayout(IPropertyValueEntry<T> entry, GUIContent label)
    {
        T value = entry.SmartValue;
        // Draw your custom drawer here using GUILayout and EditorGUILAyout.
    }
}

// Usage:
// Both values will be drawn using the MyCustomBaseTypeDrawer
public class MyComponent : SerializedMonoBehaviour
{
    public MyCustomBaseType A;

    public MyCustomType B;
}
Example

Odin uses multiple drawers to draw any given property, and the order in which these drawers are called are defined using the DrawerPriorityAttribute. Your custom drawer injects itself into this chain of drawers based on its DrawerPriorityAttribute. If no DrawerPriorityAttribute is defined, a priority is generated automatically based on the type of the drawer. Each drawer can ether choose to draw the property or not, or pass on the responsibility to the next drawer by calling CallNextDrawer(). An example of this is provided in the documentation for OdinAttributeDrawer<TAttribute, TValue>.

This means that there is no guarantee that your drawer will be called, sins other drawers could have a higher priority than yours and choose not to call CallNextDrawer().

To avoid this, you can tell Odin, that your drawer is a PrependDecorator or an AppendDecorator drawer (see ) as shown in the example shows below. Prepend and append decorators are always drawn and are also ordered by the .

Note that Odin's have full support for generic class constraints, and if that is not enough, you can also add additional type constraints by overriding CanDrawTypeFilter(Type type).

Also note that all custom property drawers needs to handle cases where the label provided by the DrawPropertyLayout is null, otherwise exceptions will be thrown when in cases where the label is hidden. For instance when [HideLabel] is used, or the property is drawn within a list where labels are also not shown.

// [OdinDrawer(OdinDrawerBehaviour.DrawProperty)] // default
// [OdinDrawer(OdinDrawerBehaviour.AppendDecorator)]
[OdinDrawer(OdinDrawerBehaviour.PrependDecorator)]
[DrawerPriority(DrawerPriorityLevel.AttributePriority)]
public sealed class MyCustomTypeDrawer<T> : OdinValueDrawer<T> where T : MyCustomType
{
    public override bool CanDrawTypeFilter(Type type)
    {
        return type != typeof(SomeType);
    }

    protected override void DrawPropertyLayout(IPropertyValueEntry<T> entry, GUIContent label)
    {
        T value = entry.SmartValue;
        // Draw property here.
    }
}
See Also

Type Parameters

T

Constructors

OdinValueDrawer()
protected OdinValueDrawer()

Properties

ValueEntry
The value entry of the property.
public IPropertyValueEntry<T> ValueEntry { get; }

Methods

CanDrawProperty(InspectorProperty)
Gets a value indicating if the drawer can draw for the specified property.
public override sealed bool CanDrawProperty(InspectorProperty property)
Parameters
InspectorProperty property

The property to test.

Returns
System.Boolean

true if the drawer can draw for the property. Otherwise false.

CanDrawValueProperty(InspectorProperty)
Gets a value indicating if the drawer can draw for the specified property. Override this to implement a custom property filter for your drawer.
protected virtual bool CanDrawValueProperty(InspectorProperty property)
Parameters
InspectorProperty property

The property to test.

Returns
System.Boolean

true if the drawer can draw for the property. Otherwise false.

DrawPropertyLayout(GUIContent)
Draws the property with GUILayout support.
protected override void DrawPropertyLayout(GUIContent label)
Parameters
UnityEngine.GUIContent label

The label. This can be null, so make sure your drawer supports that.

Extension Methods