Search Unity

Performance of separate Animators vs single multilayer Animator

Discussion in 'Animation' started by Tetsubo, Aug 28, 2016.

  1. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Hello Unity Community!

    I am making a 2D game with Unity 5.4 and want to animate my 2d human character. He has different sprites for it's body parts - leg(s), hand(s), head, eye(s), nose, mouth. It's eyes, hands, legs move independently from each other - meaning their animations and transitions are not realted in any way. My question is, which alternative is the better choice performancewise when animating:
    1) for legs | hands | eyes - create separate Animator and animate them separately or
    2) for all animations use single Animator (attached on body's "container" GameObject) and create for legs | hands | eyes different Layers in the Animator.

    Whan is the better choice? Any advice, opinion, idea are highly appreciated!

    Thank you!
     
  2. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
  3. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    You can implement both methods and then place 1000 humans in your scene once with implementation a and then with b.
    Then check your needed power with the Profiler and compare both.
     
  4. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    I hoped that someone with more experiense, a Mecanim ninja/jedi/veteran knows the answer straight and can save me that amount of time and efford .. and I never used the profiler. Maybe I had to mention that I consider myself a Unity beginner with some non-advanced experience..

    Also there might be a "best practice" kind of design pattern when animating multipart 2d models.
     
  5. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    I use one Animator for everything. You can just attach one Animator to the Game Object, so you would have to cut your mesh as far as i know.
    If you want to control the eyes via script to follow something for example you can use a mask and disable the eyes. For upper and lower body use layers.
    That would be the "best practice" ;)
    Where did you get the idea with two Animators?
     
  6. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    I "invented" it myself, when tinkering on how can I simplily a multilayer/single Animator solution. Masking is not possible, since I am working in 2D (=sprites) and avatar is not available. UNFORTUNATELY, avatar for 2D games is not yet implemented! Also the example I give is with a human, in fact I animate a monster with that human proportions (and 4 legs).. Also multilayer approch adds quite a bit complexity, since for each layer I add at least 2 parameters in the Animator..

    Also thinking if I should put the script for eye control in the body/container object or attached on the "eyes container". That means the eye container references the Animator of it's parent (which is the body container).. is that a bad approach? Or does it matter at all?
     
  7. GrischaG

    GrischaG

    Joined:
    Apr 26, 2013
    Posts:
    40
    I´m working in 3D and didn´t know the difference. So i can´t help you
     
  8. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Try to use only one animator per hierarchy. There is a memory cost and performance cost associate with each animator, they need to allocate buffer to blend animation, they need to spawn multi-threaded job.

    There is a few thread on this currently
    http://forum.unity3d.com/threads/creating-an-equipment-system-on-a-2d-pixel-game.315600/
    http://forum.unity3d.com/threads/2d-puppet-rigging-tips-tricks.245564/
    as pointed by @dustinandrew one of the approach taken by a lot of user is to use transform and animator layers to draw a complete character with equipement.
     
    Tetsubo likes this.
  9. Tetsubo

    Tetsubo

    Joined:
    Jun 26, 2014
    Posts:
    40
    Spot on, that is the answer I was looking for! Thank you, @Mecanim-Dev !

    Also thanks @GrischaG, your reply supports that of Mecanim-Dev.
     
  10. arnoob

    arnoob

    Joined:
    May 16, 2014
    Posts:
    155
    Hello Mecanim dev!

    Sorry to get back to such an old thread, but this advice kind of confused me.
    Are you saying that it's better for performances to never have a gameobject with an animator component as a child (or grand child etc...) of an other gameobject with also an animator component?

    I am making a fps where most the weapons are independently animated (they have their own animators) and they have to be set a child of the "hand bone" on my characters to stay in their hand. Even if the rigs are "optimized" in the rig tab this will make a performance deterioration? Will getting the transform of the hand bone and making my gun "match target" everyframe without it being a child of it be better performance wise?

    Also, some sides question since we're at it : does having a lot of layers in an animator and keeping only 3 active (weight at 1) will be the same performance wise than having only 3 layers? (all active of course). Does having a lot of parameters impact performance? And does having a lot of animations in an animator have an impact of performance?

    Thank you in advance for your response, and good day to everyone! :)
     
  11. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    No, i'm saying try to reduce the number of animator in your scene to improve performance, each animator has a base cost associate with it.
    By example if you have 1000 animator and each one animating only 1 property vs 1 animator animating 1000 properties, the end result is the same but you will get better performance with 1 animator animating 1000 properties.

    Each layer that you add will consume some memory regardless if the layer weight is 0 or 1, so try to only add layer that you will use.

    yes of course, more parameters mean that we need to iterate over more memory so you can have more cache miss, having many animation also mean that we need to load more animation clip on start up so the loading time can increase.

    Depending on the type of project and what platforms you are targeting you need to tune the performance accordingly.

    Just to give you an example the game ReCore which run on a xboxone, they are using over 100 parameters and have over 400 animationclips in their controller for corebot companions and they had a few issue with loading time.
     
  12. arnoob

    arnoob

    Joined:
    May 16, 2014
    Posts:
    155
    Thank you a LOT. You just gave me more information in a simple reply than what I've been able to gather through the manual during a year.

    If you don't mind, I'd like to ask a few more question while we are at it (I'm sure a lot of people where asking themselves these questions as well).

    Does it also apply to synced layers? Since they are simply copying an original layer?

    If there is multiple time the same animation in an animator (for example in different substate machines) will the animation be loaded multiple time in the memory or only once? As well, if multiple animated objects have the same animator applied to them, will it be loaded multiple time in the ram?

    Personally, as I am targeting powerful hardware (consoles and PC, not phones or tablets), my main concern is about CPU performance. For example, you said that having a lot of parameters or a lot of layer, even set to 0 will take some memory and take some time to be loaded. But once it is done, will it have an impact on the CPU itself? I'm keen to have some loading times, if it permits to have a better frame rate after...

    Again, thank you a lot for your answers!
     
  13. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    the answer is not trivial depending on the sync layer parameter but roughly the evaluation of the statemachine will be done only once since they are both sharing the same statemachine but we still need to evaluate animation clip on the sync layer too. So you save a few cpu cycle on statemachine evaluation but that about it.

    Only once. Animation clip and animator controller are asset that are shared so the data is loaded only once.

    yes they will have an impact, each time that you set a parameter we need to find it in the list of parameters, if the list is bigger the search will take longer.

    But honestly you shouldn't bother with this for now, we didn't get any report from our user about poor performance when having a lot of parameters or too many layer.
     
    arnoob likes this.
  14. arnoob

    arnoob

    Joined:
    May 16, 2014
    Posts:
    155
    Thank you a lot for these answers @Mecanim-Dev !

    Now I know a bit more what I should bother with or not.
    So next step, removing all minor animators that can be replaced by basic scripts :p
     
  15. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Be careful because that not necessary better, you should profile your game with both scenario to see if there is any improvement.

    The animation system is multithreaded, so all the evaluation is done on worker thread.
    MonoBehaviour script are called by the main thread, so if you replace all your animator by script you are transfering the evaluation from worker thread to main thread, depending on the complexity of the script that may not give you a perf improvement.
     
    arnoob likes this.
  16. Buka

    Buka

    Joined:
    Feb 22, 2013
    Posts:
    48
    I also have one question that I didn't find answer so far but it is hard to test so I can ask @Mecanim-Dev about it, sure he or someone else knows the answer:
    I am making a MMO game where I will have many animation transitions, which means there are different combos, skills.... and want to do it in right way so can you explain and tell me which solution would be best to achieve best performance and flow:
    1. Make multiple animator controllers for each weapon type that each class can use and then just swap them when equip different weapon (personally not a fan of this and think it is bad solution since I have skills that can be cast regardless of weapon so I would have to copy all skills into every animator which takes more memory and it is cumbersome)
    2. Make 1 animator with many substate machines for each weapon type then connect them and make transitions between all states (cumbersome I guess) but I had problems to use enter / exit states properly and using normal transitions really is cumbersome
    3. Make ONE BIG ANY STATE that connects to all other animations and transition between them based on index parameters for each weapon (similar to old animation system). This works for me since I want fast transitions and don't have animation blends so my animations can actually come from any state (idle or run + cast skills, attack, die, get hit) and I have priority so if you are dead you can't get hit and so on. For me this is best solution but I am not sure how this will affect performance since I have many any state transitions, let's say I have 100 animations for each class, is it going to be too hungry?

    My question about performance, what should I do?

    Or do you guys have better solution that I can use? Thanks everyone
     
  17. Mecanim-Dev

    Mecanim-Dev

    Joined:
    Nov 26, 2012
    Posts:
    1,675
    Hi

    so like you said I'm not a big fan of #1 too as your maintenance cost would be very high, if you need to change one basic skill shared amoung all controller you would need to repeat the operation on all your controller. You can of course generate your controller with a script but still not ideal.

    Try to avoid #3 because ANY STATE transition is really costly if you have many transition, since they are transition that can occur from any state on each update the engine need to test each condition on all your transitions from ANY STATE.
    This can take a lot of cpu cycle if you have a lot of transition and condition

    My solution would be something like your #2 but I would use additionnal layer to control weapon.

    Take a look at william blog post for Recore
    https://blogs.unity3d.com/2016/10/11/building-for-reuse-mecanim-on-recore/
    and also his video at unity


    that should give you some hint on how to do this with a controller.
     
  18. Buka

    Buka

    Joined:
    Feb 22, 2013
    Posts:
    48
    Thanks a lot, did watch that video but before that I did go with option 2, removed any state transitions since it is not good to have too many of them as you described, so there is what I did: Create few different BlendTrees (Idle,Run, Normal Attack, Heavy Attack...) and make create proper transitions between them based on player Action, weapon type and animation index, then make transitions from AnyState to Death and Knockdown so they can happen regardless of animation with different conditions for each. Then I will make 2 override layers (one for upper body and one for entire body) for casting skills since some skills can be cast while running and some will be cast when player stops and then cast and above that maybe one layer that will do GetHit animations since they can be addition to any animation. Overall maybe I will add some modifications and changes if this is good approach, for our game I guess it is, later there will be probably some more complex things that need to happen but for now I think this is pretty much what I wanted :) Thanks and cheers!!!
     
  19. LightStriker

    LightStriker

    Joined:
    Aug 3, 2013
    Posts:
    2,717
    Holy node horror... Why are you even presenting that as a good use case?
    You really need to learn how to inject animation and keep an animator slim.
     
    eightbitbrainpower and LudiKha like this.
  20. Weendie-Games

    Weendie-Games

    Joined:
    Feb 17, 2015
    Posts:
    75
    Any reference on how to "inject animation" ?

    Sorry to revive this old topic.
     
  21. clownhunter

    clownhunter

    Joined:
    May 26, 2013
    Posts:
    59
    Weendie-Games likes this.