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
1
Question by jorgeprodriguez · Apr 01, 2016 at 06:26 AM · editorinspectorserialization

Order of a serializable list not saved correctly using ReorderableList

I'm implementing a custom editor to rearrange in a visual way the elements of a list, where each element is a wrapper object for each value of an enum. To achieve this I use a ReorderableList so I can change the position of each element in the list just dragging and dropping. This works fine, the list is initialized correctly and i'm able to rearrange the elements. The code is as follows.

The enum:

 public enum PlayerAction {
     PlayerAction1, PlayerAction2, PlayerAction3
 }

The MonoBehaviour:

 [Serializable]
 public class PlayerActionPriority {
     public PlayerAction action;
 
     public PlayerActionPriority(PlayerAction action) {
         this.action = action;
     }
 
 }
 
 public class PlayerActionManager : MonoBehaviour {

     [HideInInspector]
     public List<PlayerActionPriority> actionPriorities;

 }

The custom Editor:

 [CustomEditor(typeof(PlayerActionManager))]
 public class PlayerActionManagerEditor : Editor {
 
     private PlayerActionManager _myScript;    
     private ReorderableList _actionPriorityList;
 
     public void OnEnable() {
         _myScript = (ActionManager)target;

         // list initialization only if it's empty (the component just has been added)
         if (_myScript.actionPriorities.Count == 0) {
             PlayerAction[] values = (PlayerAction[]) Enum.GetValues(typeof(PlayerAction));
 
             foreach(PlayerAction value in values) {
                 _myScript.actionPriorities.Add(new PlayerActionPriority(value));
             }
         }

         _actionPriorityList = new ReorderableList(
             _myScript.actionPriorities, typeof(PlayerActionPriority), true, true, false, false);
 
         _actionPriorityList.drawHeaderCallback = (Rect rect) => {
             GUI.Label(rect, "Action priority");
         };

     }
         
     public override void OnInspectorGUI() {
         DrawDefaultInspector();
 
         _actionPriorityList.DoLayoutList();
     }
 
 }


The list elements are serialized correctly and sorted as specified while Unity is open... but the new order is lost after closing and reopening. It works if i modify the list directly (removing the HideInInspector) instead of doing it using the ReorderableList, so i think the problem is related to the custom editor.

Am i missing something?

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
1
Best Answer

Answer by jorgeprodriguez · Apr 02, 2016 at 05:24 AM

I solved the problem following this article. The idea is to modify directly the serialized representation of the list instead of doing it through the object. These are the changes I made to the code:

MonoBehaviour

  [Serializable]
  public struct PlayerActionPriority {
      public PlayerAction action;
  }
  
  public class PlayerActionManager : MonoBehaviour {
      [HideInInspector]
      public List<PlayerActionPriority> actionPriorities = new List<PlayerActionPriority>();
  }

The custom Editor

 [CustomEditor(typeof(PlayerActionManager))]
  public class PlayerActionManagerEditor : Editor {
 
      private ReorderableList _actionPriorityList;
  
      public void OnEnable() {
          _actionPriorityList = new ReorderableList(
              serializedObject, serializedObject.FindProperty("actionPriorities"), 
              true, true, false, false);

          Initialization();
  
          _actionPriorityList.drawHeaderCallback = (Rect rect) => {
              GUI.Label(rect, "Action priority");
          };

         _actionPriorityList.drawElementCallback = 
             (Rect rect, int index, bool isActive, bool isFocused) => {
             SerializedProperty element = 
                 _actionPriorityList.serializedProperty.GetArrayElementAtIndex(index);
             PlayerAction action = 
                 (PlayerAction)element.FindPropertyRelative("action").enumValueIndex;
             GUI.Label(rect, new GUIContent(action.ToString()));
         };
      }
          
      public override void OnInspectorGUI() {
          DrawDefaultInspector();
  
          _actionPriorityList.DoLayoutList();
      }

     void Initialization() {
         PlayerAction[] actions = (PlayerAction[]) Enum.GetValues(typeof(PlayerAction));
 
         foreach(PlayerAction action in actions) {
             if(!IsActionAlreadyAdded(action)) {
                 AddActionItem(action);
             }
         }
     }

     bool IsActionAlreadyAdded(PlayerAction action) {
         for(int i = 0; i < _actionPriorityList.serializedProperty.arraySize; i++) {
             SerializedProperty element = 
                 _actionPriorityList.serializedProperty.GetArrayElementAtIndex(i);
             if (element.FindPropertyRelative("action").enumValueIndex == (int) action) {
                 return true;
             }
         }
 
         return false;
     }

     void AddActionItem(object obj) {
         int index = _actionPriorityList.serializedProperty.arraySize;
         _actionPriorityList.serializedProperty.arraySize++;
         _actionPriorityList.index = index;
 
         SerializedProperty element = 
              _actionPriorityList.serializedProperty.GetArrayElementAtIndex(index);
         element.FindPropertyRelative("action").enumValueIndex = (int)((PlayerAction)obj);
 
         serializedObject.ApplyModifiedProperties();
     }

  }

Everything else stays the same.

Now I have a fancy custom editor that allows me to sort easily the different elements of an enumerator.

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
avatar image
0

Answer by Bunny83 · Apr 02, 2016 at 03:45 AM

Well

The list elements are serialized correctly

It clearly isn't serialized correctly if it isn't saved :). You don't seem to mark your object as dirty, so all your changes won't be saved. If you would use the serialized object that the editor is providing it would handle this automatically when calling ApplyModifiedProperties. It also registers undo actions. The ReorderableList constructor has an overload that takes a serialized object as well as a serialized property.

When using "target" / "targets" manually you have to take care of marking the object as dirty. EditorUtility.SetDirty is kind of deprecated for scene objects as you can read on that page. I still recommend to read through it as it explains the actual problem. It also suggests to use Undo.RecordObject instead which will also provide an undo action for the editor.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Populating list of custom class through inspector 1 Answer

Why can't a MonoBehaviour subtype with generic arguments be serialized in a list since Unity 4.5? 0 Answers

Saving editor-only variables 0 Answers

Dictionary in inspector 4 Answers

How should I serialize data that is also editable in the Inspector? 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