Wednesday, June 10, 2015

Unity3d Reveal Animation View and Animation Mode

Following Will’s  tutorial about Mecanim, when I tried to control parameters synced with animation, especially collider size,  I found myself puzzled with the question, should ultimate game engine gives you ultimate visual and automatization developing experience? 

Previously working with Animations was, hunting keyframes on a blind, and with Unity 4 and nothing changed in Unity 5, although you can bind parameter to curve, still you can’t see how that affects it. So blind approach is still kept, making frustration bigger, when your relieve is, play-adjust-stop loop.

It is hard to understand, why run is done. What is the purpose of that Unity push new and unfinished things??? 


I wanted to be able to bind any float property of any GameObject component to curve.  Also I wanted to see how curve’s values change/affects property value in time. Ofcourse I don't want to hit Play and compile everything which is lengthy even on best CPUs.(There are diff story about how compiler should be improve.....)

But, why not unleash even more unused power from internal API classes of UnityEngine. 
They are buried under the thin layer of sand. Still Reflection can uncover them, although that will be hard to find any help, tutorial or some forum post. YOU are on your own.Btw  Animation window in your Unity editor is heavily using them. 

First you need to start animation mode.

if (!AnimationMode.InAnimationMode ()) {
                            
                                        AnimationMode.StartAnimationMode ();
                                       

                                      
                                }


Then you can Sample animation clip of some game object. You need to have Animator component on the game object and RuntimeController( can be empty Dummy one):

Undo.FlushUndoRecordObjects ();


                    
                    AnimationMode.BeginSampling ();
                    
                 
                    
                    for (int i=0i<leni++) {

                        
                        AnimationMode.SampleAnimationClip (gameObjectanimationCliptime);
                        
                        
                    }
                    
                    
                    
                    AnimationMode.EndSampling ();
                    
                    SceneView.RepaintAll ();

This moves AnimaitionClip to point in "time" and apply AnimationCurves values to properties of gameObject.[AnimationClip

Main trick is registering event handler to PropertyModifications:

 Undo.postprocessModifications += PostprocessAnimationRecordingModifications;

This event returns to handler array of PropertyModification[] .(Properties of the game objects become red in editor when you change them). Now you need to process those properties( properties that have been changed in game object like position,rotation, height...) and create or change the values of AnimationCurve keyframes accordingly.

/// <summary>
                /// Postprocesses the animation recording and property modifications.
                /// </summary>
                /// <returns>The animation recording modifications.</returns>
                /// <param name="modifications">Modifications.</param>
                private static UndoPropertyModification[] PostprocessAnimationRecordingModifications (UndoPropertyModification[] modifications)
                {

                   

                                modifications.Concat (AnimationModeUtility.Process (node.channel.targetnode.source as AnimationClipmodifications, (float)time));


                        }
                    
                    
                        return modifications;
                }



OK OK. You lichees. :). I've done work 4u.  AnimationModeUtility.Process




No comments:

Post a Comment