- Home /
[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?
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.
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)
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).
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).
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?