- Home /
CustomPropertyDrawer undoable properties
I'm building a custom property drawer to display a list of a custom serializable class. So far everything is working just fine, except for one little detail. At some point I'm trying to access the transforms position and rotation of a gameobject stored in that custom class and display it with a EditorGUI.Vector3Field. This works well. I can change values and they'll be updated in my scene. But in contrast to editing other fields of that custom class I'm missing the undo option here. So for example let's say I have a public Vector3 in that class and I want to display it in my custom property drawer. Then in the inspector I change the x-value from 1 to 4. If I hit Crtl+Z now its beeing changed back to one. This won't work for the earlier mentioned position and rotation. Here is what I have so far:
[CustomPropertyDrawer(typeof(jointHandle))]
public class jointHandlePropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
...
SerializedProperty jointProp = property.FindPropertyRelative("joint");
GameObject joint = (GameObject)jointProp.objectReferenceValue;
Transform parent = joint.transform.parent;
SerializedProperty jointOrientationProp = property.FindPropertyRelative("jointOrientation");
...
if (property.isExpanded)
{
float offset = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
Rect[] properties = new Rect[size];
for(int i=0; i<properties.Length;i++)
{
properties[i] = new Rect(position.x + 15, position.y + ((i+1)*offset), position.width, EditorGUIUtility.singleLineHeight);
}
joint.transform.localPosition = EditorGUI.Vector3Field(properties[0], "Position", joint.transform.localPosition);
joint.transform.localEulerAngles = EditorGUI.Vector3Field(properties[1], "Rotation", joint.transform.localEulerAngles);
EditorGUI.LabelField(properties[2],"");
jointOrientationProp.vector3Value = EditorGUI.Vector3Field(properties[3],"Orientation", jointOrientationProp.vector3Value);
//GUI.enabled = false;
//EditorGUI.PropertyField(openRect, isOpenedProp);
//GUI.enabled = true;
}
...
}
...
}
My custom class:
[Serializable]
public class jointHandle
{
public GameObject joint;
public Vector3 jointOrientation;
...
}
Answer by CodesCove · Mar 04, 2021 at 04:03 PM
Try using the EditorGUI.PropertyField if you just want simple user input. This should automatically handle the undo recording.
https://docs.unity3d.com/ScriptReference/EditorGUI.PropertyField.html
If this does not suit your purpose then record the change manually by adding Undo.RecordObject before the Vector value setting.
https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html
Hm that solution with the PropertyField isn't working. Reason: my object type here is Vector3 and not SerializedProperty. But he PropertyField will only accept SerializedProperty:
Argument 2: cannot convert from 'UnityEngine.Vector3' to 'UnityEditor.SerializedProperty'
So I tried converting it to SerializedProperty:
SerializedProperty jointProp = property.FindPropertyRelative("joint");
GameObject joint = (GameObject)jointProp.objectReferenceValue;
SerializedObject jointPositionObj = new SerializedObject(joint.transform);
SerializedProperty jointPosition = jointPositionObj.FindProperty("m_LocalPosition");
...
EditorGUI.PropertyField(propertiesRect[pos++], jointPosition);
So far, so good. But if I edit some values now it has no effect, nothing is gonna happen.
Your second solution on the other hand works perfectly. Thank so so much!!