Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 valyard · May 20, 2013 at 02:59 PM · serializationscriptableobjectassetdatabase

Saving nested data with AssetDatabase.CreateAsset

Hi.

I got a nested structure of ScriptableObjects which I want to save and load from an .asset file.

So, I basically do this:

 var bla = ScriptableObject.CreateInstance<Bla>();
 bla.TheList.Add(ScriptableObject.CreateInstance<Bla2>());
 bla.TheList.Add(ScriptableObject.CreateInstance<Bla2>());
 
 var fileName = EditorUtility.SaveFilePanelInProject("Save effect", "1.asset", "asset", "");
 AssetDatabase.CreateAsset(bla, fileName);

Where Bla and Bla2 are the following simple classes:

 [Serializable]
 class Bla : ScriptableObject
 {
     public float A = 1;
 
     [SerializeField]
     public List<Bla2> TheList = new List<Bla2>();
 }
 
 [Serializable]
 class Bla2 : ScriptableObject
 {
     public float B = 3;
 }

But the problem is that the list of Bla2 doesn't get serialized automatically. Trying to find a workaround I did the following:

 foreach (var bla2 in bla.TheList)
 {
     AssetDatabase.AddObjectToAsset(bla2, fileName);
 }

And now data from the list gets serialized. But this was just an example. In real life I got lots of nested stuff inside the objects of the list which must be serialized too. So I'll have to loop over all of them recursively using something like a visitor pattern or whatever.

What am I doing wrong? Is there a simpler way to do this?

Thanks.

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

Answer by Jodon · May 20, 2013 at 04:56 PM

Read http://www.codingjargames.com/blog/2012/11/30/advanced-unity-serialization/. ScriptableObjects can only live in the scene or in an asset. The asset cannot reference ones created in the scene. You probably should ditch ScriptableObject and go with MonoBehaviours/Prefabs.

Otherwise you're going to have to go through some more difficult stuff. You can write an Asset Post Processor that detects when something is saved. When an asset is saved, you can use reflection to loop through the fields, detect a reference to a ScriptableObject, and then AddObjectToAsset() on that reference.

Comment
Add comment · Show 2 · 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 valyard · May 23, 2013 at 02:15 PM 1
Share

I don't really get the "ScriptableObjects can only live in the scene or in an asset" part. I always thought that *.asset files are just saved copies of data, like text files or whatever. You load them to memory and save when needed.

avatar image BraidenMaggia · Feb 20, 2020 at 06:51 PM 0
Share

This website linked is NOT what it used to be. It is a very NSFW site now.

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

15 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

Related Questions

AddObjectToAsset (reference) becomes null 1 Answer

Scriptable Object references if not saved in AssetDatabase? 1 Answer

Custom assets give Missing (Mono Script) 0 Answers

Serialization issue with ScriptableObject 0 Answers

Serializing a collection of ScriptableObjects to an ASSET file? 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