-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from mackysoft/fix/custom-property-drawer-not-…
…applied WIP:Fix `CustomPropertyDrawer` not applied
- Loading branch information
Showing
10 changed files
with
694 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
#if UNITY_2019_3_OR_NEWER | ||
using System.Collections.Generic; | ||
using System; | ||
using UnityEditor; | ||
using UnityEngine; | ||
|
||
[Serializable] | ||
public abstract class Food | ||
{ | ||
public string name; | ||
|
||
public float kcal; | ||
} | ||
|
||
[Serializable] | ||
public class Apple : Food | ||
{ | ||
public Apple () | ||
{ | ||
name = "Apple"; | ||
kcal = 100f; | ||
} | ||
} | ||
|
||
[Serializable] | ||
public class Peach : Food | ||
{ | ||
public Peach () | ||
{ | ||
name = "Peach"; | ||
kcal = 100f; | ||
} | ||
} | ||
|
||
[Serializable] | ||
public class Grape : Food | ||
{ | ||
public Grape () | ||
{ | ||
name = "Grape"; | ||
kcal = 100f; | ||
} | ||
} | ||
|
||
public class Example : MonoBehaviour | ||
{ | ||
|
||
[SerializeReference] | ||
public Food food1 = new Apple(); | ||
|
||
[SerializeReference] | ||
public Food food2 = new Peach(); | ||
|
||
[SerializeReference] | ||
public Food food3 = new Grape(); | ||
|
||
[SerializeReference, SubclassSelector] | ||
public Food foodOne = new Apple(); | ||
|
||
[SerializeReference, SubclassSelector] | ||
public Food foodTwo = new Peach(); | ||
|
||
[SerializeReference, SubclassSelector] | ||
public Food foodThree = new Grape(); | ||
|
||
[SerializeReference] | ||
public List<Food> foodsOne = new List<Food> | ||
{ | ||
new Apple(), | ||
new Peach(), | ||
new Grape() | ||
}; | ||
|
||
[SerializeReference, SubclassSelector] | ||
public List<Food> foodsTwo = new List<Food> | ||
{ | ||
new Apple(), | ||
new Peach(), | ||
new Grape() | ||
}; | ||
} | ||
|
||
#if UNITY_EDITOR | ||
|
||
/// These classes are in a folder named "Editor" in the project | ||
|
||
[CustomPropertyDrawer(typeof(Peach), true)] | ||
public class PeachDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
position.height = EditorGUIUtility.singleLineHeight; | ||
EditorGUI.PropertyField(position, property.FindPropertyRelative("name")); | ||
|
||
position.y += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing; | ||
EditorGUI.PropertyField(position, property.FindPropertyRelative("kcal")); | ||
} | ||
|
||
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) | ||
{ | ||
return EditorGUIUtility.singleLineHeight * 2 + EditorGUIUtility.standardVerticalSpacing * 1; | ||
} | ||
} | ||
|
||
[CustomPropertyDrawer(typeof(Apple), true)] | ||
public class AppleDrawer : PropertyDrawer | ||
{ | ||
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label) | ||
{ | ||
EditorGUI.LabelField(position, "I'm an apple!"); | ||
} | ||
|
||
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) | ||
{ | ||
return EditorGUIUtility.singleLineHeight; | ||
} | ||
} | ||
#endif | ||
|
||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.