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.)

more ▼

asked Mar 17, 2011 at 08:20 PM

Gareth Rees gravatar image

Gareth Rees
76 1 2 8

I was just running into this same issue today. Would REALLY like to preview a) the raw animation from FBX and b) the individual 'splits' I create from them

Mar 17, 2011 at 11:00 PM DaveA

If you just want to preview the animations, then you can play them in the animation inspector. (But if you want to do something more complicated that needs scripting, then I sympathize.)

Mar 18, 2011 at 10:13 AM Gareth Rees
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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.

more ▼

answered Mar 21, 2011 at 09:00 AM

Paulius Liekis gravatar image

Paulius Liekis
8.5k 68 76 104

I tried that first. Animation.Sample has no effect in edit mode for me.

Mar 23, 2011 at 11:07 AM Gareth Rees

Really? I tried it in AssetPostprocessor and definetelly works there. If Animation.Sample doesn't work, then you should submit a bug with mini repro (and post case number here).

Mar 23, 2011 at 12:34 PM Paulius Liekis

This only works for me if I add a call to Animation.Play() after setting the time on the AnimationState (before caling Animation.Sample())

Nov 22, 2011 at 10:01 PM jahroy
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jul 12, 2011 at 02:32 PM

neonfire gravatar image

1 21 14 14

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Dec 20, 2011 at 11:28 PM

breakeren gravatar image


(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 17, 2011 at 08:20 PM

Seen: 4962 times

Last Updated: Dec 20, 2011 at 11:28 PM