Play animation in the editor?

I'm working on support for animation of mouth movements. The animator needs to be able to watch the mouth movement animation while listening to the audio sample. How can I support this in the Unity editor? It's easy to program a GUI button to play the audio sample, but how do I play the animation in the editor?

Here are some approaches I've considered and couldn't make work:

  • Use the playback feature of the Animation Inspector. (Sadly I have no idea how to do this: there seems to be no script interface for this.)

  • Use `AnimationUtility.SetAnimationEvents` to place an event at the start of the animation that calls a function that plays the audio. (But the event does not get called when playing the animation from the inspector.)

and here are some approaches I'm still considering:

  • Make an `ExecuteInEditMode` script. Look at `EditorApplication.timeSinceStartup` in its `Update` method. Call `AnimationUtility.GetAllCurves` to get the list of curves for the animation of interest. For each curve, call `AnimationCurve.Evaluate` to get the value for the current time. Apply this value to the appropriate transform. (Yuck!)

  • Abandon the idea of trying to play back the animation in the editor. Instead, set some variables, set `EditorApplication.isPlaying = true`, and arrange to play the animation and the sample from the game. (Slow to start, and causes an inconvenient window switch in the editor.)

  • Instead of editing the lip-syncing animation in edit mode, edit it in play mode using a special mode of the game. In play mode it's easy to play the animation synchronized with the audio. (This works. However, edits you make during play are lost when you exit unless you remember to "Apply" them to a prefab. Perhaps I could use `EditorUtility.ReplacePrefab` to save changes?)

What's the best approach? Is there a better idea that I've missed?

(A similar question, but no answers: Play animations in editor.)

Your first idea is closest to the answer, but a little bit overcomplicated. Just set appropriate time on animation state(s) and call Animation.Sample.

use AnimationUtility.StartAnimationMode() to emitate the mode of the AnimationWindow

It seems that animation.sample doesn’t work, but if you call gameObject.SampleAnimation(clip, time) then it works fine in editor mode.