• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by FM-Productions · May 21, 2017 at 03:16 PM · c#animationeditoranimatoreditor-scripting

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

Comment
luislodosm
ga5p0d3
A66ac
leandrovtd

People who like this

4 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

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.

Comment
luislodosm
clerkchiang
ga5p0d3

People who like this

3 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image UH4631 · Jan 30, 2019 at 05:35 AM 0
Share

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.

avatar image

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);
     }
Comment
Dustin_00

People who like this

1 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

376 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to tell if an animation clip is playing in the editor(NOT in playmode) with an editor script 1 Answer

State Machine Behaviour public variable not assignable from editor? 2 Answers

Running animation 1 Answer

Is there a way to see namespaces in Editor ? (complete class name) 0 Answers

Bind to OnWillSaveAssets and force to save my scene 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges