1
+ #if UNITY_2019_3_OR_NEWER
2
+ using System . Collections . Generic ;
3
+ using System ;
4
+ using UnityEditor ;
5
+ using UnityEngine ;
6
+
7
+ [ Serializable ]
8
+ public abstract class Food
9
+ {
10
+ public string name ;
11
+
12
+ public float kcal ;
13
+ }
14
+
15
+ [ Serializable ]
16
+ public class Apple : Food
17
+ {
18
+ public Apple ( )
19
+ {
20
+ name = "Apple" ;
21
+ kcal = 100f ;
22
+ }
23
+ }
24
+
25
+ [ Serializable ]
26
+ public class Peach : Food
27
+ {
28
+ public Peach ( )
29
+ {
30
+ name = "Peach" ;
31
+ kcal = 100f ;
32
+ }
33
+ }
34
+
35
+ [ Serializable ]
36
+ public class Grape : Food
37
+ {
38
+ public Grape ( )
39
+ {
40
+ name = "Grape" ;
41
+ kcal = 100f ;
42
+ }
43
+ }
44
+
45
+ public class Example : MonoBehaviour
46
+ {
47
+
48
+ [ SerializeReference ]
49
+ public Food food1 = new Apple ( ) ;
50
+
51
+ [ SerializeReference ]
52
+ public Food food2 = new Peach ( ) ;
53
+
54
+ [ SerializeReference ]
55
+ public Food food3 = new Grape ( ) ;
56
+
57
+ [ SerializeReference , SubclassSelector ]
58
+ public Food foodOne = new Apple ( ) ;
59
+
60
+ [ SerializeReference , SubclassSelector ]
61
+ public Food foodTwo = new Peach ( ) ;
62
+
63
+ [ SerializeReference , SubclassSelector ]
64
+ public Food foodThree = new Grape ( ) ;
65
+
66
+ [ SerializeReference ]
67
+ public List < Food > foodsOne = new List < Food >
68
+ {
69
+ new Apple ( ) ,
70
+ new Peach ( ) ,
71
+ new Grape ( )
72
+ } ;
73
+
74
+ [ SerializeReference , SubclassSelector ]
75
+ public List < Food > foodsTwo = new List < Food >
76
+ {
77
+ new Apple ( ) ,
78
+ new Peach ( ) ,
79
+ new Grape ( )
80
+ } ;
81
+ }
82
+
83
+ #if UNITY_EDITOR
84
+
85
+ /// These classes are in a folder named "Editor" in the project
86
+
87
+ [ CustomPropertyDrawer ( typeof ( Peach ) , true ) ]
88
+ public class PeachDrawer : PropertyDrawer
89
+ {
90
+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
91
+ {
92
+ position . height = EditorGUIUtility . singleLineHeight ;
93
+ EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "name" ) ) ;
94
+
95
+ position . y += EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing ;
96
+ EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "kcal" ) ) ;
97
+ }
98
+
99
+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
100
+ {
101
+ return EditorGUIUtility . singleLineHeight * 2 + EditorGUIUtility . standardVerticalSpacing * 1 ;
102
+ }
103
+ }
104
+
105
+ [ CustomPropertyDrawer ( typeof ( Apple ) , true ) ]
106
+ public class AppleDrawer : PropertyDrawer
107
+ {
108
+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
109
+ {
110
+ EditorGUI . LabelField ( position , "I'm an apple!" ) ;
111
+ }
112
+
113
+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
114
+ {
115
+ return EditorGUIUtility . singleLineHeight ;
116
+ }
117
+ }
118
+ #endif
119
+
120
+ #endif
0 commit comments