#if UNITY_EDITOR namespace Sirenix.OdinInspector.Demos { using UnityEngine; using Sirenix.OdinInspector; using Sirenix.Serialization; using System.Collections.Generic; #if UNITY_EDITOR using Sirenix.OdinInspector.Editor; using Sirenix.Utilities.Editor; using UnityEditor; #endif // Example demonstrating how drawers can be implemented with generic constraints. [TypeInfoBox( "This examples demonstates how a custom drawer can defined to be generic." + "\nThis allows a single drawer implementation, to deal with a wide array of values.")] public class GenericDrawerExample : SerializedMonoBehaviour { [OdinSerialize] public MyGenericClass A = new MyGenericClass(); // Drawn with struct drawer [OdinSerialize] public MyGenericClass B = new MyGenericClass(); // Drawn with struct drawer [OdinSerialize] public MyGenericClass C = new MyGenericClass(); // Drawn with generic parameter extraction drawer [OdinSerialize] public MyGenericClass> D = new MyGenericClass>(); // Drawn with strong list drawer [OdinSerialize] public MyGenericClass E = new MyGenericClass(); // Drawn with default drawers, as none of the generic drawers beneath apply public List F = new List(); // Drawn with the custom list drawer } // Generic class with any two generic types. public class MyGenericClass { public T1 First; public T2 Second; } #if UNITY_EDITOR public class MyGenericClassDrawer_Struct : OdinValueDrawer> where T1 : struct where T2 : struct { protected override void DrawPropertyLayout(GUIContent label) { SirenixEditorGUI.DrawSolidRect(EditorGUILayout.GetControlRect(), Color.red); this.CallNextDrawer(label); } } public class MyGenericClassDrawer_StrongList : OdinValueDrawer> where TList : IList where TElement : class { protected override void DrawPropertyLayout(GUIContent label) { SirenixEditorGUI.DrawSolidRect(EditorGUILayout.GetControlRect(), Color.blue); this.CallNextDrawer(label); } } // Note how it is possible to give a generic parameter as the drawn type; Odin will look at the constraints on the parameter to determine where it applies public class MyGenericClassDrawer_GenericParameterExtraction : OdinValueDrawer where TValue : MyGenericClass where TUnityObject : UnityEngine.Object { protected override void DrawPropertyLayout(GUIContent label) { SirenixEditorGUI.DrawSolidRect(EditorGUILayout.GetControlRect(), Color.green); this.CallNextDrawer(label); } } [DrawerPriority(0, 0, 2)] public class MyClassListDrawer : OdinValueDrawer where TList : IList where TElement : MyClass { protected override void DrawPropertyLayout(GUIContent label) { SirenixEditorGUI.DrawSolidRect(EditorGUILayout.GetControlRect(), new Color(1, 0.5f, 0)); this.CallNextDrawer(label); } } #endif } #endif