- Home /
Custom Inspector doesn't show changes immediatley
Hi,
I'm using the custom editor for managing reactions from this tutorial: https://unity3d.com/de/learn/tutorials/projects/adventure-game-tutorial/reactions
The reactions are ScriptableObjects collected in an array.
I wanted to add the functionality to move the Reaction's position in the array. I tried to realize this with the use of two buttons (up/down) per reaction (similar to how a Reaction is deleted from the Array; tutorial video at 23:10).
The methods are called from OnInspectorGUI:
public override void OnInspectorGUI ()
{
serializedObject.Update ();
//Move element
if (GUILayout.Button("^", GUILayout.Width(buttonWidth)))
{
reactionsProperty.MoveElementArray(reaction, false);
}
if (GUILayout.Button("v", GUILayout.Width(buttonWidth)))
{
reactionsProperty.MoveElementArray(reaction, true);
}
//Delete Element
if (GUILayout.Button ("-", GUILayout.Width (buttonWidth)))
{
reactionsProperty.RemoveFromObjectArray (reaction);
}
serializedObject.ApplyModifiedProperties();
}
This is the method that moves the elements:
public static void MoveElementArray<T> (this SerializedProperty arrayProperty, T elementToMove, bool direction)
where T: Object
{
arrayProperty.serializedObject.Update();
for (int i = 0; i < arrayProperty.arraySize; i++)
{
SerializedProperty elementProperty = arrayProperty.GetArrayElementAtIndex(i);
if (elementProperty.objectReferenceValue == elementToMove)
{
//if direction is false the element is moved to index -1
if (!direction)
{
arrayProperty.MoveArrayElement(i, i - 1);
}
//if direction is false the element is moved to index + 1
else
{
arrayProperty.MoveArrayElement(i, i + 1);
}
arrayProperty.serializedObject.ApplyModifiedProperties();
return;
}
}
}
The method works, but the changes are only shown when I leave and re-enter the inspector (when I click somewhere else and then again on the object that contains the Reactions). When using the Delete-Button (Delete Array-Element) the information in the inspector changes instantly, but when the Move-Buttons are pushed it doesn't. I don't understand why this would work differently.
Any thoughts?
Answer by Bunny83 · Apr 19, 2019 at 09:35 PM
You have two serializedObject.Update and two serializedObject.ApplyModifiedProperties in your code (actually 3 but only two are called the same iteration.)
Update should be called once at the beginning of your custom inspector and ApplyModifiedProperties once at the end.
Thanks, but reducing serializedObject.Update and serializedObject.Apply$$anonymous$$odifiedProperties does not seem to change anything.
Answer by ZaiusPanda · Sep 02, 2019 at 04:19 AM
Having the same issue with almost the same code, but no inmediate change at all. I have to change GO and return to the reaction to see the changes. I expected it to work the same way as removing objects, but wasn't the case.
Any new ideas on this regard?
I also tried this without any result. The problem seems to be having two properties changed.
public static void $$anonymous$$oveObjectInArray(this SerializedProperty arrayProperty, int srcIndex, int dstIndex)
{
if (!arrayProperty.isArray)
throw new UnityException("SerializedProperty " + arrayProperty.name + " is not an array.");
arrayProperty.serializedObject.Update();
Reaction reaction = (Reaction) arrayProperty.GetArrayElementAtIndex(dstIndex).objectReferenceValue;
arrayProperty.GetArrayElementAtIndex(dstIndex).objectReferenceValue = arrayProperty.GetArrayElementAtIndex(srcIndex).objectReferenceValue;
arrayProperty.GetArrayElementAtIndex(srcIndex).objectReferenceValue = reaction;
arrayProperty.serializedObject.Apply$$anonymous$$odifiedProperties();
return;
}