- Home /
Store and Restore an object's state
I'm working on a custom "Cinematic Editor" where I can take objects and apply actions to them based on the time of the cinematic. Like the animation editor, I have it so that play the cinematic forwards, backwards, and frozen at a given time. My actions will store the initial values of the target objects so that it can restore it if the time is set to back before the action took place. This works great for all of the actions I have defined except an action to play a Unity Animation. I haven't been able to find out how to look up the values that an AnimationClip is going to modify, so I can't store and restore the values themselves.
AnimationUtility.StartAnimationMode()
AnimationUtility.StopAnimationMode()
These functions would work, if it weren't for the fact that a cinematic can play multiple animations at any given time, and StopAnimationMode can't restore specific GameObjects only. What i'm looking for is something that can store GameObjects' states and restore them interdependently from each other in any order.
Currently, I'm just creating a prefab via PrefabUtility and reverting the object to the prefab at will, but that causes a hiccup during Prefab creation and is a rather inelegant solution.
You can use Unity Serializer to store and reload a GameObject's settings. Examples of doing this are in OnlyInRange$$anonymous$$anager
Answer by Muuskii · Oct 11, 2012 at 06:05 PM
This is just a guess off the top of my head, and might not even be the "best" solution. But my first impression is that you could take a hint from the way networking in unity works:
When OnSerialize is called, your script serializes all it's data into a stream.
When OnDeserialize is called, you retrieve that information from a stream that is passed to you and set your GameObject's state.
This way you're saving state every couple of frames (or every frame) and can return to whatever frame you need to in whatever order.
When it comes to actual code to give you; I have none at the moment. But I could find it.
Please feel free to use 'answer' ins$$anonymous$$d of comment. Comment when asking for more information or really anything but answering. This is a valid answer. Even if it may not be correct or the best.... but either way, that's why I moved it to 'answer'.
Your answer
Follow this Question
Related Questions
Can't add property to animation 4 Answers
Issue with a Mecanim motion reference in synchronized layers 0 Answers
Animation Inspector is suddenly Unbearably Laggy 1 Answer
Hotkey to play animation 1 Answer
Scale bones of an animated FBX 0 Answers