Hey guys!
So… I’ve been trying to create a Custom Property Drawer for one of my custom classes, and… well… I have no idea how to fix this height and width issue…
Long story short, it looks like this:
Like, Script is supposed to be a Monobehaviour (which is actually filled up by the way), UI Image is supposed to be a Texture2D (which is ALSO filled up), and when you click on Script it makes the dropdown list appear instead, and ARGH.
I would really like to find out why the width of this property drawer, the height of the property drawer, and all the elements’ positions are all messed up, along with the interactable areas, and how to fix it.
(By the way, the elements actually DO work as intended, just that their click areas are almost all wrong)
Here is my code:
public override float GetPropertyHeight (SerializedProperty property, GUIContent label) {
SerializedObject childObj = new UnityEditor.SerializedObject(property.objectReferenceValue as Command);
SerializedProperty ite = childObj.GetIterator();
float totalHeight = EditorGUI.GetPropertyHeight (property, label);
while (ite.NextVisible(true))
{
totalHeight += EditorGUI.GetPropertyHeight(ite, label);
}
return totalHeight;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) {
allTypeStrings = new List<string>();
allTypes = Methods.GetSubclasses<Command>(true);
foreach(System.Type type in allTypes)
{
allTypeStrings.Add(type.Name);
}
label = EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID (FocusType.Passive), label);
// Don't make child fields be indented
int indent = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
Rect classRect = new Rect(position.x, position.y, position.width, position.height);
Command actualVal = property.objectReferenceValue as Command;
int currIndex = -1;
if (actualVal != null)
{
for (int j = 0; j < allTypeStrings.Count; ++j)
{
if (allTypeStrings[j] == actualVal.GetType().Name)
currIndex = j;
}
}
EditorGUI.BeginChangeCheck ();
int newIndex = EditorGUI.Popup(classRect, currIndex, allTypeStrings.ToArray());
if (EditorGUI.EndChangeCheck())
{
property.objectReferenceValue = ScriptableObject.CreateInstance(allTypes[newIndex]);
//actualVal = ScriptableObject.CreateInstance(allTypes[newIndex]) as Command;
Debug.Log(property.objectReferenceValue);
Debug.Log("Changed to " + (property.objectReferenceValue as System.Object as Command).commandName);
}
Command finalValue = property.objectReferenceValue as Command;
if (finalValue != null)
{
EditorGUI.indentLevel = 1;
SerializedObject childObj = new UnityEditor.SerializedObject(finalValue);
Debug.Log("Child number is " + childObj.GetIterator().displayName);
SerializedProperty ite = childObj.GetIterator();
int i = 1;
while (ite.NextVisible(true))
{
Debug.Log("Child is " + ite.displayName);
Rect newRect = new Rect(position.x, position.y + i * 20, position.width, position.height);
EditorGUI.PropertyField(newRect, ite);
++i;
}
childObj.ApplyModifiedProperties();
}
// Set indent back to what it was
EditorGUI.indentLevel = indent;
EditorGUI.EndProperty();
}