- Home /
PropertyDrawer and EditorWindow
I am creating an editor script for our designers to use to configure some complex behavior. I am using a package that adds a node editor window to the unity UI. I am running into an issue with the CustomPropertyDrawers that I have created. The drawers work in the Inspector panel, but not in the EditorWindow class.
Does override for OnGUI in property draw get executed in a different place than the regular OnGUI that is used for EditorWindow?
The package is available here: https://github.com/Seneral/Node_Editor_Framework
Here is the object test code:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using UnityEngine;
using UnityEditor;
[System.Serializable]
public class SomeBaseObject {
[SerializeField]
private int someBaseProperty;
public SomeBaseObject(){
}
public SomeBaseObject(int _value){
someBaseProperty = _value;
}
}
[System.Serializable]
public class SomeChildObject : SomeBaseObject {
[SerializeField]
private bool someChildProperty;
public SomeChildObject(){
}
public SomeChildObject(bool _value){
someChildProperty = _value;
}
}
[CustomPropertyDrawer(typeof(SomeBaseObject))]
public class SomeBaseObjectDrawer : PropertyDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label){
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Calculate rects
Rect someBaseRect = new Rect(position.x, position.y, 30, position.height);
EditorGUI.PropertyField(someBaseRect, property.FindPropertyRelative("someBaseProperty"), GUIContent.none);
EditorGUI.EndProperty();
}
}
[CustomPropertyDrawer(typeof(SomeChildObject))]
public class SomeChildObjectDrawer : SomeBaseObjectDrawer {
public override void OnGUI (Rect position, SerializedProperty property, GUIContent label){
base.OnGUI (position, property, label);
// Using BeginProperty / EndProperty on the parent property means that
// prefab override logic works on the entire property.
EditorGUI.BeginProperty(position, label, property);
// Draw label
position = EditorGUI.PrefixLabel(position, GUIUtility.GetControlID(FocusType.Passive), label);
// Calculate rects
Rect someChildRect = new Rect(position.x + 30, position.y, 30, position.height);
EditorGUI.PropertyField(someChildRect, property.FindPropertyRelative("someChildProperty"), GUIContent.none);
EditorGUI.EndProperty();
}
}
The first two images show that it works in the Inspector area of the Editor. The 3rd image shows the CustomEditorWindow. As you can see the Node is blank, but should be running the same OnGUI code the inspector does. https://imgur.com/a/4SY3z
Answer by StunAustralia · Aug 23, 2018 at 06:45 AM
@wolftamer389 hey! Did you ever get this resolved? I have a similar issue I've reproduced with the PropertyDrawer example on the Unity site.
Basically, if I try and edit the thing from a custom editor on a mono behaviour, like so:
[CustomEditor(typeof(RecipeWithEditor))]
public class RecipeWithEditorEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
SerializedProperty myIngredientProp = serializedObject.FindProperty("ingredient");
EditorGUILayout.PropertyField(myIngredientProp, new GUIContent("Ingredient"));
serializedObject.ApplyModifiedProperties();
}
}
I get this: Which is correct.
However, when I do it from an EditorWindow with:
private void OnGUI()
{
SerializedObject serialisedObject = new SerializedObject(this);
SerializedProperty myIngredientProp = serialisedObject.FindProperty("myIngredient");
EditorGUILayout.PropertyField(myIngredientProp, true);
serialisedObject.ApplyModifiedProperties();
}
I get this: Which is not correct.
It's driven me nuts for hours now!
Anyone! What am I missing?
Cheers :)
$$anonymous$$an this post is old... I'll have to check my code on that to see if it was ever resolved. It got handed off to another developer that has more experience with editor scripts.
Is the issue that you are having that the parts are out of order and not displayed as a row?
Sorry for the late reply - the issue is that it's not displayed as a row, ie the PropertyDrawer is ignored. $$anonymous$$y actual object is a fair amount more complex than this example, but this it it boiled down :)
This seems to be a solution to the issue you are having. I will have to try it out on my own when I can make the time. https://forum.unity.com/threads/how-to-use-make-editorwindow-use-custompropertydrawer.484020/