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 ushadow · Jan 15, 2013 at 10:29 AM · serialization

[c#] Add components from de-serialized instance

Hi everyone, I'm writing a sort of save-state script in order to allow me to switch between scenes and revert to the previous scene in the (almost) same state I left it. In order to do this I created a script which stores the transform informations and serializes components of each gameobject with a given tag. I don't know if this is a right approach but even if it is I encountered a trouble at almost ONE line to end:

 Component tmp;
 foreach(MemoryStream stream in this.components){
   tmp = (Component)formatter.Deserialize(stream);
   // re-attach the component to the object instance
 }
 go = Instantiate(instance, this.position, this.orientation) as GameObject;

(instance is a new GameObject("name") and formatter is a BinaryFormatter)

ook... that damned commented line... how do I achieve this?! Unity GameObject's APIs give the possibility to add a "new" component via instance.AddComponent(); but that's not nrearly what I want to do. In fact I have the de-serialized component with all its fields saved in a precise state and I want to add THAT to my gameobject instance!! Can anyone help me or giving at least a hint to address my efforts?

Comment
Add comment · Show 5
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 whydoidoit · Jan 15, 2013 at 10:35 AM 0
Share

Welcome to serialization :) No you can't attach a component without AddComponent - In Unity Serializer I deserialize into an instance of a component that has just been added to a game object - I can do that because I also wrote all of the logic surrounding writing a formatter - possible to do that with reflection too.

avatar image ushadow · Jan 15, 2013 at 12:29 PM 0
Share

ok, actually I wanted to write my own script (I'm a DIY fanatic xD). And which would be the reflection way, if I can ask? (I watched your plugin's video yesterday, very nice! but as I already told you, I want to learn doing it on my own)

avatar image whydoidoit · Jan 15, 2013 at 12:36 PM 0
Share

Of course ($$anonymous$$d you it's taken me more then 2 years to perfect it - so you are at the start of a long road :)

You need to reflect over the public and private properties of the component when you are saving - so Look at GetProperties() and GetFields() from theComponent.GetType() - store these away and then play them back onto a newly created instance.

You would probably store the fully assembly qualified name of the component, then all of the public and private properties and fields that are not readonly (though you'd want to be able to mark individual items to be ignored with an attribute) would need to be serialized. You would also need to handle the cases where these components are references to other project or scene items. (Very difficult).

To rebuild it you would get a new instance of the component, by resolving the type name you saved and using AddComponent on whatever game object. Then you would replay the settings you saved onto the properties and fields of the new instance using Invoke, SetValue etc.

Be aware that Unity on IOS cannot JIT compile and that this causes you to have to set properties by invoking the GetSet$$anonymous$$ethod() of the property and get values using the GetGet$$anonymous$$ethod() of the property rather than the more obivious SetValue and GetValue which cause a JIT with some combinations of .NET setting on the project. Fortunately you can use SetValue and GetValue on fields (otherwise it would never work).

avatar image whydoidoit · Jan 15, 2013 at 12:41 PM 0
Share

You best route if you want a DIY save format is to write your own serializers and deserializers for your classes - this is practical and lets you write the most efficient code, though it is long winded and prone to error. You pretty much can't rely on the .NET formatters to save anything apart from starndard .NET objects (with no references to Unity objects).

avatar image ushadow · Jan 15, 2013 at 12:45 PM 0
Share

VERY nice! I like it! I'll work on it, great explanation, thank you very much!! And about iOS... I kinda HATE it, so it's not my problem I dont want to deal with it but do you know if on android there is the same problem?

0 Replies

· Add your reply
  • Sort: 

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

ISerialization madness 2 Answers

Can I serialize data in an Editor class? 1 Answer

Custom Editor - Prevent Serialization of a specific variable 1 Answer

Any way to expose (like [SerializeField] does) a function to inspector, while having it remain private??? 3 Answers

C# Serialization is working fine in Unity but not on Android 2 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