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
6
Question by ninjanoel · Feb 08, 2015 at 11:40 AM · c#

Saving/Loading ScriptableObject during runtime

According to the live training video, ScritableObjects are serialized using Unity's serilization techniques, and are better for saving dynamic lists etc (c# serialization cant do them). but all the tutorials online are talking about editor scripting! :(

I know I need to use CreateInstance to constuct my object, but if I use the technique from the live training video linked above, will the ScritableObject still use Unity's serialization? Save and load code is below, once saved, I'd presume 'CreateInstance' is not used to deserialize,

 string fName = Application.persistentDataPath + "/" + FileName;
 BinaryFormatter bf = new BinaryFormatter();
 
 FileStream file = File.Create(fName);
 bf.Serialize(file, Data);

 FileStream file = File.Open(fName, FileMode.Open);
 object DataFromFile = bf.Deserialize(file);

I'd like each 'level' to have an 'Add object' feature, so whenever I'm saving I will have a list of unknown length to save to file. I could probably have some '.ToArray()' (linq perhaps?) method to avoid dynamic list, but I've heard good things about ScritableObjects

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 ninjanoel · Feb 08, 2015 at 03:23 PM 0
Share

please be aware code above only has the 'vital' parts shown, FileStream needs to be properly closed and disposed for one, try catch'es should be used where appropriate as well.

2 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by ninjanoel · Feb 08, 2015 at 04:03 PM

Other similar questions, with good answers...

Can you use a ScriptableObject to save game state?

Save game using scriptable object derived custom asset

So basically, ScritableObjects are for Editor use only, like a "configuration file" of data that can be treated like an asset (*.asset, can be source-controlled etc) while in the Unity Editor, and can be used in script similar to GameObject, always created with CreateInstance(), supporting DontDestroyOnLoad()

C# native serilization does not play well with UnityEngine.Object's in general, so using the BinaryFormatter as specified in the question, with a "plain" class (class not extending UnityEngine.Object), is a good option.

Apologies for answering my own question, changed my search term and actually found what I needed

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
4

Answer by Firemaw · Jun 11, 2018 at 03:19 PM

In Unity 5.3+, the JsonUtility can convert any MonoBehaviour, ScriptableObject, or plain class/struct with the Serializable attribute to a JSON string.

https://docs.unity3d.com/ScriptReference/JsonUtility.html

You can create an instance of a ScriptableObject at runtime using ScriptableObject.CreateInstance and edit its properties via script. You can use JsonUtility to convert the ScriptableObject instance to a JSON string, which can be saved and loaded from disk using PlayerPrefs. After retrieving a JSON string from disk, the values can be copied over an instance of a ScriptableObject using JsonUtility.FromJsonOverwrite.

Comment
Add comment · Show 1 · 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 Leith_Ketchell · Oct 14, 2018 at 06:00 AM 0
Share

Just marking a class with Serializable attribute does not guarantee that it is indeed serializable. JsonUtility will fail to serialize classes which are not entirely serializable, in the same way as BinaryFormatter.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Making a bubble level (not a game but work tool) 1 Answer

An OS design issue: File types associated with their appropriate programs 1 Answer

Renderer on object disabled after level reload 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