Nested Custom Property Drawers
Hi guys,
Does anybody know whether it is possible to have custom property drawers in custom property drawers, and if so, how?
Because when I tried doing so, the custom property inside the property displays the default version instead of my custom property drawer...
Case in point in attachment In the above variable Command (which is of type Command), I drew that using my own Command CustomPropertyDrawer, so it looks different. But in the below variable Test (which is of type BaseTestClass), which has a Command variable, it uses the default ScriptableObject property drawer...
Edit Here is the snippet of the Command class and property drawer
Command.cs
[System.Serializable]
public class Command : ScriptableObject {
public virtual string commandName{
get { return this.GetType().Name;}
}
public Sprite uiImage;
}
VirtualClassEditor.cs
public abstract class VirtualClassEditor<BaseClass> : PropertyDrawer where BaseClass:ScriptableObject{
List<string> allTypeStrings = new List<string>();
List<Type> allTypes = new List<Type>();
int currIndex = -1;
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
if (property.objectReferenceValue != null)
{
SerializedObject childObj = new UnityEditor.SerializedObject(property.objectReferenceValue);
SerializedProperty ite = childObj.GetIterator();
float totalHeight = EditorGUI.GetPropertyHeight (property, label, true) + EditorGUIUtility.standardVerticalSpacing;
while (ite.NextVisible(true))
{
totalHeight += EditorGUI.GetPropertyHeight(ite, label, true) + EditorGUIUtility.standardVerticalSpacing;
}
return totalHeight;
}
else
{
return EditorGUI.GetPropertyHeight(property, label, true);
}
}
public void UpdateTypeStrings(SerializedProperty property)
{
allTypeStrings = new List<string>();
allTypes = Methods.GetSubclasses<BaseClass>(true);
foreach(System.Type type in allTypes)
{
allTypeStrings.Add(type.Name);
}
BaseClass actualVal = (BaseClass)property.objectReferenceValue;
currIndex = -1;
if (actualVal != null)
{
for (int j = 0; j < allTypeStrings.Count; ++j)
{
if (allTypeStrings[j] == actualVal.GetType().Name)
currIndex = j;
}
}
}
public void SetupClassProperty(Rect position, SerializedProperty property, GUIContent label)
{
label = EditorGUI.BeginProperty(position, label, property);
Rect classRect = new Rect(position.x, position.y, position.width, EditorGUI.GetPropertyHeight(property));
EditorGUI.BeginChangeCheck ();
int newIndex = EditorGUI.Popup(classRect, property.displayName, currIndex, allTypeStrings.ToArray());
if (EditorGUI.EndChangeCheck())
{
property.objectReferenceValue = ScriptableObject.CreateInstance(allTypes[newIndex]);
Debug.Log("Changed to " + property.objectReferenceValue.GetType().Name);
}
EditorGUI.EndProperty();
}
public void SetupProperties(Rect position, SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue != null)
{
EditorGUI.indentLevel += 1;
SerializedObject childObj = new UnityEditor.SerializedObject(property.objectReferenceValue);
SerializedProperty ite = childObj.GetIterator();
float prevHeight = EditorGUI.GetPropertyHeight(property, label, true);
while (ite.NextVisible(true))
{
Rect newRect = new Rect(position.x, position.y + prevHeight + EditorGUIUtility.standardVerticalSpacing, position.width, EditorGUI.GetPropertyHeight(ite, label, true));
prevHeight += newRect.height + EditorGUIUtility.standardVerticalSpacing;
EditorGUI.PropertyField(newRect, ite, true);
}
childObj.ApplyModifiedProperties();
}
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
int oldIndent = EditorGUI.indentLevel;
UpdateTypeStrings(property);
SetupClassProperty(position, property, label);
SetupProperties(position, property, label);
EditorGUI.indentLevel = oldIndent;
}
}
CommandEditor.cs
[CustomPropertyDrawer(typeof(Command))]
public class CommandEditor: VirtualClassEditor<Command>{
}
Another note is that it apparently works when I have an array of Commands, it just doesn't work if it is part of another class...
Can you please share a code snippet of the definition for your Command custom PropertyDrawer? Basically just the class and the attributes on it.
Alright, I updated it with the Command class and the Command Property Drawer!
I'm not able to reproduce this with the following example. What does your property drawer look like for the class that contains Command as fields?
// NewBehaviourScript.cs
using System;
using UnityEngine;
[Serializable]
public class CommandContainer
{
public int someInt;
public Command command;
}
public class NewBehaviourScript : $$anonymous$$onoBehaviour
{
public Command command;
public CommandContainer commandContainer;
}
//CommandContainerDrawer.cs
using UnityEditor;
using UnityEngine;
[CustomPropertyDrawer(typeof(CommandContainer))]
public class CommandContainerDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUI.GetPropertyHeight(property, label);
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorGUI.PropertyField(position, property, true);
}
}
Your answer
Follow this Question
Related Questions
Removing / modifying in Inspector a generic variable derived from a non-generic variable 1 Answer
Property Drawer ArgumentException 1 Answer
Need quick editor scripting help 1 Answer
Change the color of a variable field if it is empty 0 Answers
How to create Generics Property Type Property Drawer 0 Answers