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 KinanGH · Nov 04, 2021 at 07:18 PM · custom editorserializedproperty

Confirm serialized property before changing it in a custom editor

I want to confirm the changing of the serialized property before applying it to the required field in this editor script:

 using UnityEngine;
 using UnityEditor;
 
 public class SetFields : EditorWindow
 {
     /// <summary>
     /// The component that has the serialized object the we want to set.
     /// </summary>
     private SerializedObject myTarget;
     /// <summary>
     /// The serialized object the we want to set.
     /// </summary>
     private SerializedProperty fieldNewValue;
     
     [MenuItem("Tools/Set Fields")]
     public static void ShowWindow() => GetWindow<SetFields>("Set Fields");
 
     private void OnGUI()
     {
         try
         {
             myTarget = new SerializedObject("some component");
     fieldNewValue = myTarget.FindProperty("some field");
 
             EditorGUILayout.PropertyField(fieldNewValue, new GUIContent("Value", "The field new value."), true);
 
             myTarget.ApplyModifiedProperties();
         }
         catch (System.Exception) { }
     }
 }


The thing is this updates the field in real time, and I don't want that, I want to apply the value to the field when the user clicks a button in the custom editor window, but if I put "myTarget.ApplyModifiedProperties();" inside a button like this :

 if (GUILayout.Button("Set Field Value"))
 {
 myTarget.ApplyModifiedProperties();
 }

The value in the custom editor window will always revert back to its original.

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 Bunny83 · Nov 05, 2021 at 10:34 AM

Yes, this wont work. The SerializedObject and its SerializedProperties are just wrapper classes that represent the state of the serialized data. When you create the SerializedObject, it automatically calls Update to actually read the serialized state into its properties. Now you can modify those properties and when done you call ApplyModifiedProperties to actually write those changes back to the serialized data.


However your code is executed for every UI interaction. That means if you don't apply the changes you made through the PropertyField, those changes are lost since the next time this method is called for example when you click on the button, the changes are no longer there since you have a new SerializedObject / SerializedProperty.


It's not clear how the exact flow of your editor window should look like and where you get that "some component" from. However usually you would create the SerializedObject only once when you select that object.


Though even if you did create the SerializedObject only once and keep reusing it, you run into other issues. When the object is modified elsewhere (the inspector or at runtime from the runtime code itself) the state of your SerializedObject would not update. That's why you usually call Update at the beginning of OnGUI and ApplyModifiedProperties at the end.


You talked about a verification process but you haven't gone into any details what this is actually about. We don't even know the type of your property. Verification is usually done right in place when the assignment happens. Of course when you use a PropertyField this is actually delegated to a PropertyDrawer. So you have to do the verification after the method returns. If you need to revert invalid changes, you would need to store the previous value before you call the PropertyField in a local variable so you can revert the changes after the method. Though if the verification is just some sort of clamping you can just do it afterwards.


If you really want some sort of apply button, you would also need a seperate button to "Update" the state manually. I haven't looked into details how Unity's TextureInspector does it, but I guess it's something like this:

 void OnGUI()
 {
     if (!SerializedObject.hasModifiedProperties)
         serializedObject.Update();
     
     // [ ... ]
     // the actual GUI goes here.
     // [ ... ]
     
     using (new EditorGUI.DisabledScope(!SerializedObject.hasModifiedProperties))
     {
         if (GUILayout.Button("Revert"))
             serializedObject.Update();
         if (GUILayout.Button("Apply"))
             serializedObject.ApplyModifiedProperties();
     }
 }

So as long as nothing got modified we automatically update the serialized object so it reflects the actual current state of the object and if something is changed elsewhere, when your editor window is redrawn it will reflect the current state. However when you actually change any property in your SerializedObject it will no longer update itself but instead enables the two buttons at the bottom to either revert the canges (by manually updating the serializedObject) or to apply the changes.


Of course this still requires you to clear up the actual information flow of your editor window which includes where and how it actually gets the object you want to "inspect".

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 KinanGH · Nov 06, 2021 at 02:59 PM 0
Share

Thank you very much for the detailed answer,


updating the serialized object manually like you said did the trick, for clarification I'm making an editor window that changes the value of a field for a multiple component instances, I know this is something unity do by default, however this is not the case with my component, changing a field value of multiple instances changes only the first selected one, so I created this editor window. link text


"You talked about a verification process but you haven't gone into any details what this is actually about" the verification is not for integrity of data or something, it is for stopping the editor window from updating the serialized property in real time, so you can call it "Apply" instead of "Verification".


"We don't even know the type of your property" and that is the point, I don't know the type of the field that I like to modify, so I needed a generic way for displaying the appropriate editor field type UI, and you can't assign an "object" type as a value for the SerializedProperty nor get it's value as an "object" type, so here is where I used this guy's extension methods link textl for getting and setting the value of SerializedProperty as "object" type and that did the trick number 2 []~( ̄▽ ̄)~*.


If anyone wished to use my script, feel free to.

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

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

ReorderableList Custom Editor Count -> NullReferenceExeception 1 Answer

How to insert an element of specific type to a list as serializedProperty? 1 Answer

Custom Inspector doesn't show changes immediatley 2 Answers

serializedObject with EditorGUILayout.Popup in C# 1 Answer

CustomPropertyDrawer undoable properties 1 Answer


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