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.)
EditorApplication.isPlaying = true
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.)
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
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.)
Answer by Paulius Liekis
Mar 21, 2011 at 09:00 AM
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.
I tried that first. Animation.Sample has no effect in edit mode for me.
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).
This only works for me if I add a call to Animation.Play() after setting the time on the AnimationState (before caling Animation.Sample())
Answer by neonfire
Jul 12, 2011 at 02:32 PM
It seems that animation.sample doesn't work, but if you call gameObject.SampleAnimation(clip, time) then it works fine in editor mode.
Answer by breakeren
Dec 20, 2011 at 11:28 PM
use AnimationUtility.StartAnimationMode() to emitate the mode of the AnimationWindow
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
3 People are following this question.
Can I make animations snap to a frame?
How to add Animation Curves on Imported Clips by Script
How to select an animation clip by index number?
Add animtion clip through editor
Get a list of clips in an animation component from script.