- Home /
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
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.
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 .
Your answer
Follow this Question
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