Skip to content

Commit

Permalink
Merge pull request #33 from mackysoft/fix/custom-property-drawer-not-…
Browse files Browse the repository at this point in the history
…applied

WIP:Fix `CustomPropertyDrawer` not applied
  • Loading branch information
mackysoft committed Aug 19, 2023
2 parents 5a3dd69 + 9a319ea commit 23cb26b
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 5 deletions.
8 changes: 8 additions & 0 deletions Assets/Example.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 120 additions & 0 deletions Assets/Example/Example.cs
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
11 changes: 11 additions & 0 deletions Assets/Example/Example.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 23cb26b

Please sign in to comment.