1- using System . Collections . Generic ;
21using System ;
2+ using System . Collections . Generic ;
33using UnityEditor ;
44using UnityEngine ;
55
66[ Serializable ]
77public abstract class Food
88{
9- public string name ;
9+ public string name ;
1010
11- public float kcal ;
11+ public float kcal ;
1212}
1313
1414[ Serializable ]
1515public class Apple : Food
1616{
17- public Apple ( )
18- {
19- name = "Apple" ;
20- kcal = 100f ;
21- }
17+ public Apple ( )
18+ {
19+ name = "Apple" ;
20+ kcal = 100f ;
21+ }
2222}
2323
2424[ Serializable ]
2525public class Peach : Food
2626{
27- public Peach ( )
28- {
29- name = "Peach" ;
30- kcal = 100f ;
31- }
27+ public Peach ( )
28+ {
29+ name = "Peach" ;
30+ kcal = 100f ;
31+ }
3232}
3333
3434[ Serializable ]
3535public class Grape : Food
3636{
37- public Grape ( )
38- {
39- name = "Grape" ;
40- kcal = 100f ;
41- }
37+ public Grape ( )
38+ {
39+ name = "Grape" ;
40+ kcal = 100f ;
41+ }
4242}
4343
4444[ Serializable ]
4545[ HideInTypeMenu ]
4646public class Banana : Food
4747{
48- public Banana ( )
49- {
50- name = "Banana" ;
51- kcal = 100f ;
52- }
48+ public Banana ( )
49+ {
50+ name = "Banana" ;
51+ kcal = 100f ;
52+ }
5353}
5454
5555public class Example : MonoBehaviour
5656{
5757
58- [ SerializeReference ]
59- public Food food1 = new Apple ( ) ;
58+ [ SerializeReference ]
59+ public Food food1 = new Apple ( ) ;
6060
61- [ SerializeReference ]
62- public Food food2 = new Peach ( ) ;
61+ [ SerializeReference ]
62+ public Food food2 = new Peach ( ) ;
6363
64- [ SerializeReference ]
65- public Food food3 = new Grape ( ) ;
64+ [ SerializeReference ]
65+ public Food food3 = new Grape ( ) ;
6666
67- [ SerializeReference , SubclassSelector ]
68- public Food foodOne = new Apple ( ) ;
67+ [ SerializeReference , SubclassSelector ]
68+ public Food foodOne = new Apple ( ) ;
6969
70- [ SerializeReference , SubclassSelector ]
71- public Food foodTwo = new Peach ( ) ;
70+ [ SerializeReference , SubclassSelector ]
71+ public Food foodTwo = new Peach ( ) ;
7272
73- // UseToStringAsLabel support on UNITY_2021_3_OR_NEWER
74- [ SerializeReference , SubclassSelector ( UseToStringAsLabel = true ) ]
75- public Food foodThree = new Grape ( ) ;
73+ // UseToStringAsLabel support on UNITY_2021_3_OR_NEWER
74+ [ SerializeReference , SubclassSelector ( UseToStringAsLabel = true ) ]
75+ public Food foodThree = new Grape ( ) ;
7676
77- [ SerializeReference ]
78- public List < Food > foodsOne = new List < Food >
79- {
80- new Apple ( ) ,
81- new Peach ( ) ,
82- new Grape ( )
83- } ;
77+ [ SerializeReference ]
78+ public List < Food > foodsOne = new List < Food >
79+ {
80+ new Apple ( ) ,
81+ new Peach ( ) ,
82+ new Grape ( )
83+ } ;
8484
85- [ SerializeReference , SubclassSelector ]
86- public List < Food > foodsTwo = new List < Food >
87- {
88- new Apple ( ) ,
89- new Peach ( ) ,
90- new Grape ( )
91- } ;
85+ [ SerializeReference , SubclassSelector ]
86+ public List < Food > foodsTwo = new List < Food >
87+ {
88+ new Apple ( ) ,
89+ new Peach ( ) ,
90+ new Grape ( )
91+ } ;
9292}
9393
9494#if UNITY_EDITOR
@@ -98,32 +98,32 @@ public class Example : MonoBehaviour
9898[ CustomPropertyDrawer ( typeof ( Peach ) , true ) ]
9999public class PeachDrawer : PropertyDrawer
100100{
101- public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
102- {
103- position . height = EditorGUIUtility . singleLineHeight ;
104- EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "name" ) ) ;
105-
106- position . y += EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing ;
107- EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "kcal" ) ) ;
108- }
109-
110- public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
111- {
112- return EditorGUIUtility . singleLineHeight * 2 + EditorGUIUtility . standardVerticalSpacing * 1 ;
113- }
101+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
102+ {
103+ position . height = EditorGUIUtility . singleLineHeight ;
104+ EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "name" ) ) ;
105+
106+ position . y += EditorGUIUtility . singleLineHeight + EditorGUIUtility . standardVerticalSpacing ;
107+ EditorGUI . PropertyField ( position , property . FindPropertyRelative ( "kcal" ) ) ;
108+ }
109+
110+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
111+ {
112+ return EditorGUIUtility . singleLineHeight * 2 + EditorGUIUtility . standardVerticalSpacing * 1 ;
113+ }
114114}
115115
116116[ CustomPropertyDrawer ( typeof ( Apple ) , true ) ]
117117public class AppleDrawer : PropertyDrawer
118118{
119- public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
120- {
121- EditorGUI . LabelField ( position , "I'm an apple!" ) ;
122- }
123-
124- public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
125- {
126- return EditorGUIUtility . singleLineHeight ;
127- }
119+ public override void OnGUI ( Rect position , SerializedProperty property , GUIContent label )
120+ {
121+ EditorGUI . LabelField ( position , "I'm an apple!" ) ;
122+ }
123+
124+ public override float GetPropertyHeight ( SerializedProperty property , GUIContent label )
125+ {
126+ return EditorGUIUtility . singleLineHeight ;
127+ }
128128}
129129#endif
0 commit comments