Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
2
Question by MaxKarpinsky · Jan 17, 2018 at 12:43 AM · editorserializedobject

Why `SerializedProperty.Update` is called in the beginning?

From this answer: https://answers.unity.com/questions/641212/what-does-serializedpropertyupdate-exactly-do.html - I got that SerializedProperty.Update updates visual representation in inspector to actual state of the object.

Then why do some scripts do that:

 serializedObject.Update();
 
 .... Changes of the actual object
 
 serializedObject.ApplyModifiedProperties();

Why not:

 .... Changes of the actual object
 
 serializedObject.ApplyModifiedProperties(); // Apply changes 
 serializedObject.Update(); // Show these changes
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

2 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

Answer by Bunny83 · Jan 17, 2018 at 01:00 AM

No, SerialitedObject is just a representation of the serialized data of the object. The Update method actually reads and copies the data into an internal structure (SerializedProperty). That means once "updated" a SerializedObject and it's SerializedProperties represents a copy of the serialized data in memory. When you call "ApplyModifiedProperties" you actually write the copied data back to disk.


When you first create a SerializedObject based on an asset / object in the scene it automatically updates itself for the first time. However there could be more than one SerializedObject editing the same object(s) at the same time. Calling Update before you apply any changes ensures it actually represents the most recent version of the data.


This has nothing to do with any visual representation. The SerializedObject is a pure data object.


The most simplest analogy would be that a SerializedObject is just a data array. The Update method actually reads the content of a file on the disk into the array. Now you can edit the data in the array and when done you call "ApplyModifiedProperties" which takes the data array and writes it back to the file on disk.

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 MaxKarpinsky · Jan 18, 2018 at 12:39 AM 0
Share

Thanks! Clear and simple.

avatar image
0

Answer by ImTheOne · Oct 28, 2021 at 09:43 AM

I found a werid behaviour.

using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; using System;

[CustomEditor(typeof(CustomScript))] public class CustomEditorTest : Editor { SerializedProperty targetObjectProp; SerializedProperty nameProp; SerializedProperty hpProp; SerializedProperty mpProp;

 private void OnEnable()
 {
     SceneView.duringSceneGui += OnSceneGUI;

     targetObjectProp = serializedObject.FindProperty($"{nameof(CustomScript.targetObject)}");
     nameProp = serializedObject.FindProperty($"{nameof(CustomScript.myName)}");
     hpProp = serializedObject.FindProperty($"{nameof(CustomScript.myHP)}");
     mpProp = serializedObject.FindProperty($"{nameof(CustomScript.myMP)}");
 }

 private void OnDisable()
 {
     SceneView.duringSceneGui -= OnSceneGUI;
 }

 public override void OnInspectorGUI()
 {
     // base.OnInspectorGUI();

   //  serializedObject.Update();

     EditorGUILayout.TextField(nameProp.stringValue);
     EditorGUILayout.IntField("MyHP ", hpProp.intValue);
     EditorGUILayout.IntField("MyMP ", mpProp.intValue);

     EditorGUILayout.PropertyField(mpProp);
     EditorGUILayout.PropertyField(nameProp);
     EditorGUILayout.PropertyField(targetObjectProp);

     serializedObject.ApplyModifiedProperties();
 }

 private void OnSceneGUI(SceneView obj)
 {
     Handles.BeginGUI();
     {
     }
     Handles.EndGUI();
 }

}

and on my EditorWindow i do this .

   foreach (var pair in Targets)
     {
         EditorGUI.BeginChangeCheck();
         {
             foreach (var prop in pair.Value)
             {
                 EditorGUILayout.PropertyField(prop);
             }
         }
         if (EditorGUI.EndChangeCheck())
         {
             pair.Key.ApplyModifiedProperties();
         }
     }

as you can see i am not updating on CustomEditorTest::OnInspectorGUI() for test but as i modify on EditorWindow, it automatically changes the values on the Inspector view .

I mean without Update() !!

How does it get updated ? That is what i'm wondering .

Comment
Add comment · 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

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

100 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

Related Questions

What does SerializedProperty.Update exactly do? 1 Answer

Editor: How to do PropertyField for List elements? 4 Answers

Manually creating Editor sometimes leads to NullReferenceException in SerializedObject constructor 0 Answers

SerializedObject IntValue Multi Editing only applies to first object. 0 Answers

Changing Inspector's Serialized Property Label (With Code Sample) 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