Search Unity

Mecanim Design: How would I dynamically load animations?

Discussion in 'Animation' started by DaemonTrey, Apr 21, 2015.

  1. DaemonTrey

    DaemonTrey

    Joined:
    Dec 5, 2014
    Posts:
    5
    We have a humanoid avatar that has generic animations (Idle, Walk, Run, etc). We've created an Animator Controller for these animations, and then tied it to our avatar--and this works great. But what happens when we want to dynamically add animations at run-time?

    In our case, we'd like to download character abilities using remote asset bundles. These abilities might have their own set of animations and scripts. Using Mecanim, how can we run a new animation or set of animations that our original state controller is ignorant of?
    1. Can we add states to an animator controller instance at runtime? (I suspect not)
    2. Should we have state controllers for each ability, then swap out controllers during ability use? (Our early test suggest this would work. Is there a better way?)
    3. Can we have multiple animators point to the same avatar? (I don't think so...)
    At the moment we're very tempted to use the legacy system as layering and blend trees are not a priority. However, if we can have our cake and eat it too...

    Most of the research I find on this topic is dated or deals with the Animator Override Controller to replace known animations, which isn't applicable.

    Any help would be appreciated.

    Thanks,
    Trey
     
  2. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Simply create a new controller with your new abilities.

    Swaping a controller for ability use is a little bit too overkill, each time you swap a controller we need to free all resource for the old controller, create new resource for the new controller and compute the binding between animation curve and animated property, this is not a light weight process.
     
  3. komodor

    komodor

    Joined:
    Jan 2, 2013
    Posts:
    36
    actually I don't get it

    1st you advice to create new controller (that includes swapping it, right?)
    2nd you write that it's overkill

    so .. simple answer please .. how would you load dynamically animations to animator?
     
    Last edited: May 19, 2015
  4. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Sorry I wasn't clear enough my bad.

    No, since in your case, you would like to download character abilities using remote asset bundles, I was thinking that you could create a complete new controller in your asset bundle and use this one rather than the old one. In this case you would swap your controller only once when user download the new asset bundle.

    What I mean is swapping a controller at runtime each time a new abilities is used is overkill, from you first post you did test out this possibility.
    Yes it does work, but like I said swapping a controller very often could lead to some performance issue. It really depend on your game play, by example in Diablo 3 changing player abilities is not something you can do while fighting monster so in this case it could work.

    Also why AnimatorOverrideController isn't applicable in your case? It was made explicitly for this workflow. At the moment it the only way to easily replace an animation from a controller.
     
  5. komodor

    komodor

    Joined:
    Jan 2, 2013
    Posts:
    36
    well my problem is bit different, we are using Animator as case for animations only, in early stages of 2D in unity 4 there was problem to attach the native unity animations to Animation component, so we have everything in Mecanim now ... and I am desperatelly looking for reading of the Animator states in runtime, i know i can read clips and it's bit different then states, because you can swap animations in states and keep the same name ... in Animator.Play you put name of state and not the clip

    we have like thousand of animations now and the editor script i made to create class with all the informations doesn't work in unity5 because of changes in internal editor classes ... so is there way how to work with it?

    or do we need to change our approach? (I was told like year and half ago by some unity moderator that Animation component is deprecated and all new features go to Mecanim, which is pretty poor so far ...)

    thank you very much for your answers
     
    Last edited: May 20, 2015
  6. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Unfortunately, you will need to update your script. Before 5.0 everything was in the internal namespace because we felt that it was not ready to be used. Which information do you need at runtime?

    Why do you think that mecanim is pretty poor so far compare to legacy?
     
  7. komodor

    komodor

    Joined:
    Jan 2, 2013
    Posts:
    36
    stateName, clipName, speed, lengthOfClip, sampleRateOfClip, transitionsFrom, transitionsTo

    actually i am not sure if i can get the clip information in runtime in later unity versions as i delegated the filling of static class to animator so it's possible i need only stateName, clipName, speed ... the transitions is something we don't use on characters

    because i have access to animations array and i don't need to use states ... we are using simple Animator.Play("stateName" ....)

    also working with many animations attached to Animator is pretty painfull :D the sorting order of animations miss sense (not alphabetical, i'd bet it's by serialization id) and one popup menu without being able to at least type keyword makes you just use working controllers where you put few animations to be able orient in them

    please check the screenshot in my question http://answers.unity3d.com/questions/696607/animation-window-how-to-deal-with-this.html

    and this is how looks animator controller on our hero character https://dl.dropboxusercontent.com/u/6749738/mecanimHeroSmall.jpg
    and it has 2 layers with enlighted and nonenlighted animations, as the artist wants to have handpainted all images

    while we have this conversation i have to ask another question ... how should we make long animations in unity? we are trying to make cutscenes with few characters but more keyframes we add longer delays we have so when we have animation with 3 characters and some items long like 5 seconds it's impossible to work in animation window ... that's why we separated the animations by character and we have mecanim layer for each animation ... this works more or less well but it's uncomfortable for the animator guy

    edit: it's 2D game we started to make when u2d was in alpha, so i might be wrong on some things ... i am just trying to get the information from you, because you probably know best

    thx again, i really appreciate your help
     
    Last edited: May 20, 2015
  8. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Then in this case make sure that all your state have the same name than your animation clip and at runtime use
    http://docs.unity3d.com/ScriptReference/RuntimeAnimatorController-animationClips.html

    You will have acces to the animation clip array for this controller, then from there you have access to
    stateName, clipName( shoud be the same than stateName), lengthOfClip, sampleRateOfClip.
    Speed is missing.

    This is not an issue only for the animator, you would get the same popup menu with an animation component. The sorting is define by how you did add them to your controller, first added should be first in list becasue animation clip are collected by iterating over each layer, state machine and state.

    Keyframing character in unity is not an easy task so trying to sync 3 different character in a cut scene is a nightmare. Have you try to do all your keyframing in your authoring tools(3ds max, maya, etcc...) and play the animation as a generic animation on one animator component that drive your 3 character?
     
  9. komodor

    komodor

    Joined:
    Jan 2, 2013
    Posts:
    36
    we don't use any 3d software, it's all sprites directly from painter to unity and animated in the animation window

    yes i'ts nightmare - it's all handpainted and every single item is original in the game ... damn artists

    well thank you very much at least i know i am not missing anything important, i am already using same names for clip and state as it's the easiest way how to import many anims to animator, i am also using the animationclip array in animator in the effect manager so animator doesn't need to fill the static class with info

    i am not sure if you are right person ... at least i have to try - if i may suggest a feature - search text field in animation window and column like project window with animation names filtered by the search input .. it would make lot easier navigating in the AController animations now it's just user hostile :D

    something like this

    thank you, good luck
     
  10. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Yes there is definitively something wrong with this popup menu, it doesn't scale up very well.

    You should post your idea on http://feedback.unity3d.com/, I will also talk directly with our dev who taking care of the animation windows
     
  11. komodor

    komodor

    Joined:
    Jan 2, 2013
    Posts:
    36
  12. DaemonTrey

    DaemonTrey

    Joined:
    Dec 5, 2014
    Posts:
    5
    Correct me if I'm wrong, but when I bundle an AnimatorController, it'll bring with it every animation that exists in that controller. If I have 100+ abilities, each with their own animations, and I decided to update 1 animation, won't my entire animation set have to be redownloaded as I update that one bundle?

    Though, I'm learning about nested assets (dependencies), and that may make this approach feasible.

    Random thought: If I have a ton of animations loaded into an animation controller, but I only expect to use 10% of them, won't I be loading all of those animations into memory?

    Interesting example. In Diablo 3, you could essentially equip four or five abilities at once. How would you design this in Mecanim? Based on my understanding you'd have all of your animations in a single animation controller, and not alter it when swapping abilities. (which doesn't strike me as practical, due to my above dilemma.)

    You cannot, however, programmatically generate a controller based on the combination of abilities. A possible solution might be to have multiple animation controllers (one for each ability), then combine them by layer them on top of each other to create a new controller. Though I don't think this is supported. (and I'm probably crazy, and just need to spend more time tinkering)

    The only way an animator override controller seems applicable is if we have N number of equipable abilities. Then we generate a controller layer that has things like Ability1Casting, Ability2Casting, Ability3Casting... AbilityNCasting, then load the animations into that at runtime. Which may be feasible. It just didn't strike me as very agile...as what happens if my ability has more animation states than simply "casting"? Or what if I need a custom state transition?
     
  13. DarkGate

    DarkGate

    Joined:
    Jan 26, 2016
    Posts:
    33
    I am having the same issue. I am creating a game with multiple classes. Some of the common animations (such as falling, sleeping, running, sitting, etc...) are exactly the same, while some are completely different (depends on the classes' abilities).

    I looked into animator override, but like OP, it did not fit my exact needs. I am debating at this point in time to manage animations clips myself, but that is a lot of manual work. The only solution I can think of at the moment is to simply create a controller for each class and use layering to isolate different actions so its easy to copy/paste over.