Search Unity

Prevent Animator from auto updating?

Discussion in 'Animation' started by From-Soy-Sauce, Jul 20, 2016.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hello, I have been looking around all day to see if I could find a way to manually update the animator by a controlled amount of time by script, but I've failed to find anything to that effect.

    I have tried to do this, but it both prints read "GameTime"...

    anim=GetComponent<Animator>();
    print(anim.GetTimeUpdateMode());
    anim.SetTimeUpdateMode(UnityEngine.Experimental.Director.DirectorUpdateMode.Manual);
    print(anim.GetTimeUpdateMode());
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    283
    This is/will be used for something else (and is experimental, as evidenced by the enum name), and at the moment, doesn't do anything, as you saw.

    You can either disable the component, and update it manually.
    animator.enabled = false;
    then
    animator.Update(deltaTime);

    Or you can set the speed to zero, and do something like this when you want to update:
    animator.speed = 1.0f;
    animator.Update(deltaTime);
    animator.speed = 0.0f;

    Both will give slightly different results, and it depends what you want to do.
    The second one will animate every frame, but time will not advance.
    This also works with the state machine visual debugging, whereas disabling the animator will break the visual debugging.

    It's also worth noting that using any of those approaches means your animators will run single-threaded, so there may be performance issues.
     
    fuoink, petey and forestrf like this.
  3. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    95
    Could you elaborate a bit more on the Animator.Update being single threaded? Is the normal Animator Update method multithreaded? Would trying to optimize by using Animator.Update not be a good idea? Please help.
     
    futurlab_peterh likes this.
  4. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    283
    The normal Animator Update does one pass where it updates all the Animators, splitting these animators in batches that will be evaluated using all available threads.

    Calling Animator.Update will run the update for that Animator right away, so if you do this for all your Animators, then the other threads will sit idle in the meantime.

    If you are developing for PC, PS4, Xbox One or any platform that has multiple cores, it's unlikely that you'll be seeing any gains by manually updating the Animator.
     
    chadfranklin47 likes this.
  5. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    95
    Alright, thank you for clearing that up. I was hoping to save on some performance by changing the update rate of each Animator depending on the distance from the camera. I guess it would be useful if there was a method to add the Animator to a list of animators that are to update with the batch.
     
  6. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    283
    There's a clever user who figured out that you can put multiple animators in one PlayableGraph, connect an AnimatorControllerPlayable to each Animator, and then manually update the PlayableGraph.

    Doing this will execute the Animators as a batch, which will take advantage of all threads. It requires a bit more work than just dealing with the Animators directly, but it might address your use case.
     
    PedroCoriAG and futurlab_peterh like this.
  7. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    95
    I have never heard of this. Thanks for letting me know, I will give it a shot.
     
    futurlab_peterh likes this.
  8. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    84
    I implemented this technique as well and it feels like an abuse of the playables system. Is there any roadmap item for supporting batched update of a set of animator playables?
     
  9. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    518
    Did that user post about it somewhere publicly available? I have an entirely playables based system so I'm wondering if it would be worth changing my implementation. In particular, I'm wondering if Unity will multithread graphs containing PlayableBehaviours given that they don't seem to get multithreaded at the moment.
     
  10. Justin_Larrabee

    Justin_Larrabee

    Joined:
    Apr 24, 2018
    Posts:
    84
    Not that I'm aware of, I discovered it myself after some experimentation. I should note that I do not use any MonoBehaviours in my implementation as I am using the ECS and needed to avoid them.