Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by LeEHil · Mar 04, 2021 at 09:17 AM · custom editorpropertydrawerserializedpropertycustom inspectorundo

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;
     ...
 
 }



alt text

2021-03-04-08-33-51-edag-automationstudio-samplesc.png (6.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LeEHil · Mar 05, 2021 at 06:49 AM 0
Share

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!!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

114 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

SerializedObject.FindProperty returning null 2 Answers

Custom Inspector doesn't show changes immediatley 2 Answers

Custom Editor/Custom Property Drawer combo, breaks Enable button 1 Answer

Property Drawer SerializedProperty is null 2 Answers

Custom inspector: How can I find out if a SerializedProperty will be drawn by a custom property drawer? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges