- Home /
How to play mecanim animations in Editor mode?
Hi,
is it possible to play Mecanim animations of an Animator component in the Unity Editor or do I have to run the game for the animations to work? I thought it should be possible in the Editor but apparently I can't get it to work.
My specific problem:
I am currently writing a tool for the editor to simplify the workflow for some features of my game. What I need is to jump to a specific frame of a character animation in the Editor mode. The anmations work fine in the Play Mode, however I cannot get them to work in the Editor mode. Let's say I have a script that is executed every frame in the Editor mode. Currently, I have my code in an OnDrawGizmos()
function (of a MonoBehavior script in the scene) for test purposes (since it is called in the editor). It looks something like this:
private PlayerCharacter player;
public string animName = "Attack01";
public int animLayer = 0;
public float normalizedAnimTime = 0.2f;
void OnDrawGizmos() {
try
{
initPlayer();
setAnimationFrame(animName, animLayer, normalizedAnimTime);
}
catch (Exception e) {
Debug.LogError(e);
}
}
I have the character gameObject with a finished Animator component already inside the scene. PlayerCharacter is the class of the script that is attached to my character. It also holds the Animator component of the character. In initPlayer()
I find my character in the scene and store it, then I am initializing some values of the character. Basically only the animator component right now (I have removed the code of my script that was irrelevant to the question - initializing other components etc.). In setAnimationFrame()
I try to tell my character to jump to the specific animation frame and freeze (animation speed = 0f). However, it does not work for me. I also could not find errors while debugging that would point me in the right direction.
private void initPlayer()
{
if (player == null)
{
player = FindObjectOfType<PlayerCharacter>();
player._animator = player.GetComponent<Animator>();
}
}
private void setAnimationFrame(string animationName, int layer, float normalizedAnimationTime)
{
if (player != null && player._animator != null) {
player._animator.speed = 0f;
player._animator.Play(animationName, layer, normalizedTime);
}
}
Does anyone of you have an idea or maybe another approach on how to solve this?
Kind regards
Answer by FM-Productions · May 21, 2017 at 06:10 PM
Someone on another forum has provided me with an answer to this question. It seems like you have to update the Animator component manually if you are not in Play Mode. In my setAnimationFrame() function, after the existent update logic, I simply update the Animator component and it works now:
private void setAnimationFrame(string animationName, int layer, float normalizedAnimationTime)
{
if (player != null && player._animator != null) {
player._animator.speed = 0f;
player._animator.Play(animationName, layer, normalizedTime);
player._animator.Update(Time.deltaTime);
}
}
For me this works, because I only need a freeze frame of an animation, but it seems that the Editor only updates if there is an interaction by the user (Mouse movement, Keyboard input etc.). So if you want to fluidly play animations in the Editor, you may have to alter the update logic.
I think you may be able to get the Scene view to update by calling SceneView.RepaintAll(). This should tell unity to update the sceneview.
Answer by luislodosm · Sep 13, 2018 at 03:22 PM
For example, for a door:
public bool open;
void OnDrawGizmosSelected()
{
if (open)
{
animador.Play("Opened");
}
else
{
animador.Play("Closed");
}
animador.Update(Time.deltaTime);
}
Your answer
Follow this Question
Related Questions
How to tell if an animation clip is playing in the editor(NOT in playmode) with an editor script 1 Answer
Animations out of sync? 0 Answers
2D Animation does not start 1 Answer
Tri-Stage Animation 1 Answer
Playing animation at the start 1 Answer