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 sckyj · Apr 06, 2016 at 01:01 PM · listserializationserialize

Question about Unity serialize system

Hi, i have a question about unity serialization system. these is pseudo code about problem.

 [System.Serializable]
 public class Parent
 {
     public int a;
     public Parent()
     {
         a = 111;
     }
 }
 
 [System.Serializable]
 public class Child : Parent
 {
     public int b;
     public Child()
     {
         b = 222;
     }
 }
 
 [System.Serializable]
 public class SOTest : ScriptableObject
 {
     public List<Parent> _database = new List<Parent>();
 }

SOTest class has List field and i add few instances that is child instance as below.

 public class test : MonoBehaviour
 {
     SOTest _database;
 
     void OnGUI()
     {
         if (GUILayout.Button("open"))
         {
             // load ScriptableObject
         }
         if (GUILayout.Button("create"))
         {
             // save ScriptableObject
         }
         if (GUILayout.Button("add"))
         {
             Parent test = new Child();
             _database._database.Add(test);
         }
         if (GUILayout.Button("show"))
         {
             for (int i = 0; i < _database._database.Count; i++)
             {
                 Child child = _database._database[i] as Child;
                 if (child != null)
                 {
                     Debug.Log(child.a + "  " + child.b);
                 }
                 else
                 {
                     Debug.Log("there is no child");
                 }
             }
         }
     }
 }

Problem is

  1. i try to add child instances to database scriptableobject by push add button on screen.

  2. then if i push show button, log shows me "111 222" that is exactly i expected.

  3. then i save database object on disk.(serialization)

  4. restart unity.

  5. Load database object from disk.(deserialization)

  6. push "show" button. BUT, Logs are "there is no child".

i think when i use generic list, deserialization from disk will retrieve exactly type of List. so if i add some kind of derived instance to list then serialization will lose derived class information. Am i right?

if i am wrong, how can fix this problem

thank you.

Comment
Add comment · Show 1
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 _Yash_ · Apr 06, 2016 at 01:35 PM 0
Share

How do you serializing and deserializing? what is the procedure?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sckyj · Apr 07, 2016 at 12:52 AM

This is full code i tested.

 [System.Serializable]
 public class Parent
 {
     public int a;
     
     public Parent()
     {
         a = 111;
     }
 }
 
 [System.Serializable]
 public class Child : Parent
 {
     public int b;
 
     public Child()
     {
         b = 222;
     }
 }
 
 [System.Serializable]
 public class SOTest : ScriptableObject
 {
     public List<Parent> _database = new List<Parent>();
 
     public static ScriptableObject Create(string folder, string file)
     {
         string fullPath = folder + "/" + file;
 
         SOTest database = AssetDatabase.LoadAssetAtPath(fullPath, typeof(SOTest)) as SOTest;
         if (database == null)
         {
             if (!AssetDatabase.IsValidFolder(folder))
             {
                 AssetDatabase.CreateFolder(null, folder);
             }
 
             database = ScriptableObject.CreateInstance<SOTest>() as SOTest;
             AssetDatabase.CreateAsset(database, fullPath);
             AssetDatabase.SaveAssets();
         }
 
         return database;
     }
 }
 
 public class test : MonoBehaviour
 {
     string _assetFullPath;
     string _assetFolder;
     string _assetFile;
     SOTest _database;
 
     void OnGUI()
     {
         if(GUILayout.Button("open"))
         {
             Open();
         }
         if (GUILayout.Button("create"))
         {
             Save();
         }
         if (GUILayout.Button("add"))
         {
             Parent aa = new Child();
             _database._database.Add(aa);
         }
         if(GUILayout.Button("show"))
         {
             for(int i=0; i<_database._database.Count; i++)
             {
                 Child child = _database._database[i] as Child;
                 if(child != null)
                 {
                     Debug.Log(child.a + "  " + child.b);
                 }
                 else
                 {
                     Debug.Log("there is no child");
                 }  
             }
         }
     }
 
 
     public void Open()
     {
         _assetFullPath = EditorUtility.OpenFilePanel("Open Database", Constants._item_Db_FullPath, "asset");
         if (_assetFullPath != "")
         {
             LoadDatabase();
         }
     }
 
 
     public void Save()
     {
         _assetFullPath = EditorUtility.SaveFilePanel("Create Database", Constants._item_Db_FullPath, "New Database", "asset");
         if (_assetFullPath != "")
         {
             LoadDatabase();
         }
     }
 
     public void LoadDatabase()
     {
         if (_assetFullPath != null)
         {
             string absolutePath = Path.GetDirectoryName(_assetFullPath);
 
             int index = absolutePath.IndexOf("Assets");
 
             _assetFolder = absolutePath.Substring(index);
             _assetFile = Path.GetFileName(_assetFullPath);
 
             // DB Load            
             if (_assetFolder != "" && _assetFile != "")
             {
                 _database = SOTest.Create(_assetFolder, _assetFile) as SOTest;
             }
         }
     }
 }
 

Comment
Add comment · Show 7 · 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 _Yash_ · Apr 07, 2016 at 04:36 AM 0
Share

try marking it dirty after you alter the database with SetDirty in this case

 EditorUtility.SetDirty(_database);

now when you save scene with Ctrl+S it will be saved.

avatar image sckyj _Yash_ · Apr 07, 2016 at 05:56 AM 0
Share

Thanks for your reply. I try to follow your comment but it doesn't work.

 if (GUILayout.Button("add"))
         {
             Parent aa = new Child();
             _database._database.Add(aa);
             EditorUtility.SetDirty(_database);
         }

avatar image sckyj sckyj · Apr 07, 2016 at 05:56 AM 0
Share

i even try this but doesn't work too.

 if (GUILayout.Button("add"))
         {
             //Parent aa = new Child();
             Child aa = new Child();
             _database._database.Add(aa);
             EditorUtility.SetDirty(_database);
         }

avatar image _Yash_ · Apr 07, 2016 at 07:47 AM 0
Share

Try Debuging "database " object after this line:

 SOTest database = AssetDatabase.LoadAssetAtPath(fullPath, typeof(SOTest)) as SOTest;

It is possible that it might not find the asset and create a new one each time.

avatar image sckyj _Yash_ · Apr 07, 2016 at 07:57 AM 0
Share

Unfortunatly database load correctly.

avatar image _Yash_ · Apr 07, 2016 at 08:18 AM 0
Share

Everything looks fine... please try Debugging "_database._database[i]" before casting to Child. and debug it after casting it into child.

avatar image sckyj _Yash_ · Apr 07, 2016 at 12:07 PM 0
Share

I'm sorry for late for testing. i tried debug before & after _database._database[i]. When i create database both instance show me that is Child. but After restart unity and load database, both instance is Parent. ah.... i'm so confuse..

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

58 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

Related Questions

How to Serialize or Save a List of GameObjects 1 Answer

List Data not persisting after being added in Start() 1 Answer

Serialize Lists with multiple inherited classes or interfaces ? 1 Answer

Arrays not serializing 1 Answer

Serialization Placeholder Value for Different Fields? 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