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 IceTrooper · Jul 05, 2017 at 03:55 PM · serializationtargetcustom-inspectornestedlist of lists

Custom Inspector: nested lists not serializing with Editor.target

Hello. I'm trying to make a custom inspector, which let me show Lists of Lists on my way. I have a problem with serialization those, because when I hit play or exit Unity I lost my inpected data. I know that Unity doesn't serialize nested Lists, so I've tried many solutions posted in other questions or forums (like this: http://answers.unity3d.com/questions/784507/add-list-to-serialized-list.html). Unfortunately, any of those solutions doesn't work and I don't have any more ideas how to fix it. Can you help me?

I don't want to support multi-editing, so I didn't use SerializedObject/Property and using those is difficult to work with Lists (even more with nested lists). To watch if something was serialized or not I got a counter .Count.

Here's my code (you can copy-paste it, it will work): Main class:

 [CustomEditor(typeof(GeneratorData))]
 public class GeneratorDataEditor : Editor
 {
 
     GeneratorData generatorData;
     int selectedLevel;
 
     private void OnEnable()
     {
         generatorData = (GeneratorData)target;
     }
 
     public override void OnInspectorGUI()
     {
         EditorGUILayout.BeginVertical();
         {
             DrawGeneretableDataStatesOnLevels();
         }
         EditorGUILayout.EndVertical();
         EditorUtility.SetDirty(generatorData);
     }
 
     private void DrawGeneretableDataStatesOnLevels()
     {
         EditorGUILayout.BeginHorizontal();
         {
             selectedLevel = EditorGUILayout.IntField("Selected level:", selectedLevel);
             if (selectedLevel < 0)
             {
                 selectedLevel = 0;
             }
             else if (generatorData.generetableDataStates.Count > 0 && selectedLevel > generatorData.generetableDataStates.Count)
             {
                 selectedLevel = generatorData.generetableDataStates.Count;
             }
             else if (generatorData.generetableDataStates == null || generatorData.generetableDataStates.Count == 0)
             {
                 selectedLevel = 0;
             }
 
             if (GUILayout.Button(new GUIContent("Add level", "Add new level")))
             {
                 generatorData.generetableDataStates.Insert(selectedLevel, new GeneratableStates());
                 //generatorData.generetableDataStates.Add(new List<GeneratorData.GeneratableState>());
             }
             if (GUILayout.Button(new GUIContent("Remove level", "Remove selected level")))
             {
                 if (selectedLevel >= generatorData.generetableDataStates.Count)
                 {
                     selectedLevel = generatorData.generetableDataStates.Count - 1;
                 }
                 if (generatorData.generetableDataStates[selectedLevel] != null && generatorData.generetableDataStates.Count != 0)
                 {
                     generatorData.generetableDataStates.RemoveAt(selectedLevel);
                 }
             }
         }
         EditorGUILayout.EndHorizontal();
         EditorGUILayout.TextArea("Count levels:" + generatorData.generetableDataStates.Count);
     }
 }


And other classes:

 [CreateAssetMenu(fileName = "New GeneratorData", menuName = "Generator/GeneratorData", order = 1011)]
 public class GeneratorData : ScriptableObject
 {
     public List<GeneratableStates> generetableDataStates = new List<GeneratableStates>();
 }
 
 
 public class GeneratableStates
 {
     public List<GeneratableState> generatableStatesList = new List<GeneratableState>();
     public GeneratableStates()
     {
         generatableStatesList = new List<GeneratableState>();
     }
 }
 
 public class GeneratableState
 {
     public GeneratableData gd;
     public float perc;
 }
 
 [System.Serializable]
 public class GeneratableData {
 
     public GeneratableData(GameObject prefab, float minScale, float maxScale, Rect boundingRect)
     {
         this.prefab = prefab;
         this.minScale = minScale;
         this.maxScale = maxScale;
         this.boundingRect = boundingRect;
     }
 
     public GameObject prefab;
     public float minScale;
     public float maxScale;
     public Rect boundingRect;
 
     public float GetAverageArea()
     {
         return (boundingRect.height * boundingRect.width) * ((minScale + maxScale) / 2);
     }
 }

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

0 Replies

· Add your reply
  • Sort: 

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

69 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

Related Questions

Custom Editor: Weird reset when clicking Play 1 Answer

how can I create a new Object from custom Editor 0 Answers

Custom inspector variable resets automatically 1 Answer

Nested Dictionary data lost after playmode 0 Answers

Is it possible to display a List of GameObject List in a Custom Inspector? 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