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