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 /
  • Help Room /
avatar image
0
Question by ThomLaurent · Sep 13, 2016 at 06:05 PM · c#assetscriptableobjectissuesaving

SetDirty won't work on Nested List

Hi, usually I find my answer by myself but this time - after spending two days on this problem - I give up ^^

So I try to save a List<List<string>> into a ScriptableObject but each time I make a change on it and restart Unity changes are lost/reset.

I've already did it with a List<string> and it worked perfectly, but this nested class got me.

So here's the code:

- MyScriptableObject.cs

 using UnityEngine;
 using System.Collections.Generic;
 
 public class MyScriptableObject : ScriptableObject {
     [System.Serializable]
     public class StringList: List<string> { }
 
     [System.Serializable]
     public class Container : List<StringList> { }
 
     [SerializeField]
     private Container cont;
 
     public void Fill () {
         // Fill with some random datas
         cont.Add(new StringList { "1A", "1B", "1C" });
         cont.Add(new StringList { "2A", "2B", "2C" });
         cont.Add(new StringList { "3A", "3B", "3C" });
     }
 }

- MyCustomEditor.cs

 using UnityEditor;
 using UnityEngine;
 
 [CustomEditor(typeof(MyScriptableObject))]
 public class MyCustomEditor : Editor {
 
     private MyScriptableObject script;
 
     void OnEnable () {
         script = target as MyScriptableObject;
     }
 
     public override void OnInspectorGUI () {
         DrawDefaultInspector();
 
         if (GUILayout.Button("Fill")) {
             script.Fill();
             EditorUtility.SetDirty(script);
         }
     }
 }

Since I use EditorUtility.SetDirty(script); each time I change the asset I would have thought changes were saved but nothing.

Note: When opening the .asset file after a change was made ( Fill() + SetDirty() + File > Save Project) I notice it doesn't contain my random datas.

So basically changes are not saved into the .asset (i.e SetDirty() doesn't work) but the inspector keeps track of it until Unity restart (i.e Serialization works).

Any idea how to figure this out?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by ThomLaurent · Sep 15, 2016 at 12:04 AM

I finally found my answer by myself ^^

The Problem

Unity doesn't support inheritance for serialization.

Since I saw my script's list items within the inspector in debug mode (only) I supposed serialization was done correctly but it was not.

The Solution

A wrapper: a class containing the nested List<string> :

MyScriptableObject.cs

 using UnityEngine;
 using System.Collections.Generic;
 
 public class MyScriptableObject : ScriptableObject {
         [System.Serializable]
         public class StringList{
             public List<string> list = new List<string>();
         }
     
         [SerializeField]
         private List<StringList> cont = new List<StringList>();
     
         public void Fill () {
             StringList stringList = new StringList();
             stringList.list.AddRange(new string[] { "1A", "1B", "1C" });
     
                 cont.Add(stringList);
         }
 }

(MyCustomEditor.cs uses the same script as in the question.)


Note

EditorUtility.SetDirty() works as intended: it saves the asset (non-scene object) without any Undo management.

Actually it effectively saves your asset at Unity close, since no scene is involved you won't see an asterisk in your Unity window caption.

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

235 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 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 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

Saving data from multiple game objects 1 Answer

Serialize a list of class containing ScriptableObject assets 0 Answers

Static function not working in between screen 2 Answers

How do I assign a prefab's Instance ID, that was converted to string, as a GameObject variable in a c# script? 1 Answer

BinaryFormatter - Save and Load lists of data? 0 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