Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Omti1990 · Feb 06, 2021 at 09:09 PM · custom editorcustom-inspectorundo

Custom Inspector Undo Functionality when directly changing SerializedProperties

Hello, I'm relatively new at working with custom Inspector/Editor. The serialized properties have been very helpful for me since they include the automatic undo functionality. But this doesn't seem to work when you edit the value of the serialized property directly. (in this case .boolValue)

 public override void OnInspectorGUI()
     {
         
         EditorGUILayout.LabelField("Set Selection on Activation", EditorStyles.boldLabel);
         EditorGUI.BeginChangeCheck();
         bool _tryFind = tryFindSelectableInChildren.boolValue;
         if(_tryFind) _selected =1;
         else _selected = 0;
         serializedObject.Update();
 
         _selected = EditorGUILayout.Popup(new GUIContent
                  ("Select on Activation", "shortened"),_selected,popupContent);
         EditorGUILayout.BeginVertical(EditorStyles.objectField);
         if(_selected==0)
         {
             _tryFind = false;
             EditorGUILayout.PropertyField(preSelected);
         }
         else if(_selected==1)
         {
             _tryFind = true;
             EditorGUILayout.PropertyField(parentOfSelectables,new GUIContent("Set Parent"));
         }
         tryFindSelectableInChildren.boolValue = _tryFind;
         EditorGUILayout.EndVertical();
         serializedObject.ApplyModifiedProperties();
 }


How do I record the changes I'm doing to "tryFindSelectableInChildren" in my undo function?

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
Best Answer

Answer by grimprophecy · Feb 06, 2021 at 09:39 PM

Assign the bool to a temporary variable, then in your EndChangeCheck, record and set _tryFind like this example:

https://docs.unity3d.com/ScriptReference/Undo.RecordObject.html

That should work, but you might not need Undo.RecordObject for value types like bools, I can't remember off the top of my head.

Comment
Add comment · Show 2 · 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 Omti1990 · Feb 06, 2021 at 10:33 PM 0
Share

That doesn't work.

If I put my serialized property in the Undo.Record Object I get an error:

Argument "1": Conversion of "UnityEditor.SerializedProperty" in "UnityEngine.Object" not possible. [Assembly-CSharp-Editor]

The problem here is that I'm not accessing some public property of my script directly, but I'm using the SerializedProperties, so recording the script is kinda pointless since I can't actually change the value of the property that way. (I think)

avatar image Omti1990 · Feb 06, 2021 at 10:37 PM 0
Share

Actually I tried it for the hell of it and it seems to work. I don't understand why, but it works...

 public override void OnInspectorGUI()
     {
         //base.OnInspectorGUI();
         
         EscapeFromEarth.UI.$$anonymous$$enu _menu = (EscapeFromEarth.UI.$$anonymous$$enu)target;
         EditorGUILayout.LabelField("Set Selection on Activation", EditorStyles.boldLabel);
 
         EditorGUI.BeginChangeCheck();
         bool _tryFind = tryFindSelectableInChildren.boolValue;
         if(_tryFind) _selected =1;
         else _selected = 0;
         serializedObject.Update();
 
         _selected = EditorGUILayout.Popup(new GUIContent("Select on Activation", "shortened"),_selected,popupContent);
         EditorGUILayout.BeginVertical(EditorStyles.objectField);
         if(_selected==0)
         {
             _tryFind = false;
             EditorGUILayout.PropertyField(preSelected);
         }
         else if(_selected==1)
         {
             _tryFind = true;
             EditorGUILayout.PropertyField(parentOfSelectables,new GUIContent("Set Parent"));
         }
         //tryFindSelectableInChildren.boolValue = _tryFind;
         EditorGUILayout.EndVertical();
         if (EditorGUI.EndChangeCheck()) 
         {
  
 
             Undo.RecordObject(_menu, "Set Field Value");
     
             tryFindSelectableInChildren.boolValue = _tryFind;
         }
      serializedObject.Apply$$anonymous$$odifiedProperties();

}

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

115 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 avatar image

Related Questions

CustomEditor Not Working or Visible 2 Answers

Can I add my own project settings? 1 Answer

Object field not working in custom editor for scriptable object? 0 Answers

How to Call the default Editor of a ScriptableObject From the other ScriptableObject's OnInspectorGUI()? 1 Answer

Dictionary becomes null unexpectedly when using a custom inspector 2 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