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
0
Question by WeaverX · Apr 19, 2019 at 09:06 PM · scriptableobjectcustom editorserializedpropertycustom inspector

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?

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
0

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.

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 WeaverX · Apr 20, 2019 at 01:36 AM 0
Share

Thanks, but reducing serializedObject.Update and serializedObject.Apply$$anonymous$$odifiedProperties does not seem to change anything.

avatar image
0

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?

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 ZaiusPanda · Sep 03, 2019 at 09:21 PM 0
Share

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;
     }


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

110 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

Related Questions

SerializedObject.FindProperty returning null 2 Answers

Custom inspector: How can I find out if a SerializedProperty will be drawn by a custom property drawer? 0 Answers

CustomPropertyDrawer undoable properties 1 Answer

Why does my scriptable object asset lose items in a list after first playthrough? 1 Answer

Custom editor window does not show prefab overrides 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