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 DiligentGear · May 18, 2014 at 03:12 AM · serializationruntimescriptableobjectbinaryformatter

How to Serialize Scriptable Object at Runtime?

I know how to save them into .asset file in Editor, but how do I do this in the actual build?

BinaryFormatter is not happy about ScriptableObject...

I got this error in my build when I tried to serialize that at runtime:

 SerializationException: Type UnityEngine.ScriptableObject in assembly UnityEngine, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable.

Any thought on this?

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 Loius · May 18, 2014 at 05:49 AM 0
Share

"how do i", not "how to"

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Loius · May 18, 2014 at 05:54 AM

UnitySerializer is worth looking into. I don't know off the top of my head if it handles serializableObjects specifically, but it does normal unity-objects so it may be capable.

Otherwise you'll probably have to look into using Reflection. Essentially you'd have something like this:

 foreach(var field in targetObject.GetFields()) {
   SerializeSomehow(field.name, field.value);
 }
 

You can check to be sure field.value is serializable before messing with it using some reflection stuff as well.

This STILL isn't going to be good enough if you need to serialize Vector3 or other non-serializable Unity structs. If it turns out you need some things like that included in your copied object, I'm at a loss. I have some scripts that literally just copy a certain type of SerializedObject into a copy specifically because I couldn't figure out how to handle those types. :(

Edit: But if you just need to copy from one scripto to another it would look like this:

 public void CopyScriptable(ScriptableObject source, ScriptableObject target) {
   if ( typeof(source) != typeof(target) ) {
     // error message
     return;
   }
   foreach(var field in source.GetType().GetFields()) {
     field.SetValue(target, field.Getvalue(source));
   }
 }
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

22 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

Related Questions

Handle modified variables being temporarily reset at runtime 1 Answer

How to assign a class ID at runtime? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Scriptableobject not saving if i force close the application. 1 Answer

Serializing specialized subclasses of generic classes not working 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