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
0
Question by chuckrussell · Apr 12, 2012 at 06:38 PM · serializationscriptableobjectassetdatabase

Using System.Object in ScriptableObject

Is there any way to use a System.Object class in a class which extends ScriptableObject for use in AssetDatabase.CreateAsset? Try as I might, unity does not want to serialize the System.Object and anything with an System.Object type gets thrown away on serialization.

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 rutter · Apr 12, 2012 at 06:59 PM

All classes inherit from System.Object (usually indirectly).

You can certainly create a class which inherits directly from System.Object or UnityEngine.ScriptableObject, but not both at once (you can only directly inherit from one class). I'm not sure if that's the question you're asking, though.

Ignoring questions of namespace and scope, an object can certainly contain references to objects of any other type. That says nothing about whether or not those references will be serialized, of course, which seems to be what you're after.

I've had some luck getting Unity to serialize custom classes like so:

  • Create some class which inherits from MonoBehaviour

  • Create some data class which doesn't explicitly inherit from anything, and give the class the System.Serializable attribute

  • Your behaviour script should contain a list of data nodes, exposed to the inspector (`public`, SerializeField, etc)

  • Your behaviour script should only use new to create the list if it doesn't already exist (from serialization)

I assume it's possible to do more than that, but I haven't had many chances to explore this in depth. ;)

One important caveat with Unity serialization: it is not polymorphic. If the list you're serializing has mixed types, Unity will serialize every node as the list's generic type.

In my experience, this is one of the trickier topics with Unity dev. You're sort of "going off the map".

Comment
Add comment · Show 4 · 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 chuckrussell · Apr 12, 2012 at 08:29 PM 0
Share

"Off the map" is the perfect explanation of where I am currently.

For some reason, though I can serialize things like ints, and even gameObjects, I can't serialize a System.Object, and since I don't have the type until runtime and it can be different each time it has to be a System.Object.

avatar image chuckrussell · Apr 12, 2012 at 08:36 PM 0
Share

Thanks for the response. If I explicitly strongly type my fields, such as a data type containing strings and ints etc. Unity happily serializes everything, and I can perform an AssetDatabase.CreateAsset, and LoadAssetAtPath to restore that object later. Unfortunately, this topic isn't quite so simple. I am using reflection to pull the data out of all public properties of a script which extends monobehaviour at runtime, so it has to be absolutely dynamic, as there is no telling what public properties the other developers will add. Hence I must create an array of name value pairs, and the value has to be an object. What is weirder, is that Unity will chug along just fine on datatypes that are not an object, it absolutely will not serialize something with a data type of object.

avatar image rutter chuckrussell · Apr 12, 2012 at 09:00 PM 0
Share

Hmm. Tricky.

If you're willing to do some legwork, Unity should be albe to serialize an array of bytes (whether on an object or to some file), which you could read using BitConverter to restore state from your objects. I've considered using that method a few times, but never actually gone far enough down the rabbit hole to need it.

avatar image chuckrussell chuckrussell · Apr 13, 2012 at 01:33 PM 0
Share

Unfortunately I have pseudo solved this problem and ran into another. Since unity won't serialize System.Objects and BinaryFormatter won't serialize UnityEngine.Objects, I am doing two serializations: one for the base data types, and one for the Unity data types: ($$anonymous$$onoBehaviours, GameObjects etc) and I ran into your polymorphism caveat. Now that the array that is to be serialized through Unity is made of UnityEngine.Objects, Unity can serialize them but when they come out they have lost all semblance of originating type. Any suggestions for that?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Serialization issue with ScriptableObject 0 Answers

Saving nested data with AssetDatabase.CreateAsset 1 Answer

Serializing a collection of ScriptableObjects to an ASSET file? 1 Answer

Creating ScriptableObject Asset "Failed to write meta file" 1 Answer

Unity loses association to ScriptableObject after exiting playmode 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