KybernetikGames / animancer

Documentation for the Animancer Unity Plugin.
67 stars 8 forks source link

AnimationPlayableUtilities.Play is [Obsolete] in Unity 2022.1 #208

Closed KybernetikGames closed 2 years ago

KybernetikGames commented 2 years ago

The Problem

Animancer v7.2 uses AnimationPlayableUtilities.Play to tell an Animator component to play its PlayableGraph but Unity 2022.1 has marked that method as [Obsolete] so it gives a compiler warning.

The Solution

That method actually still works as intended so you just need to suppress the warning. Double click the warning in the Console window to open AnimancerPlayable.cs and put a warning suppressor around the call:

#pragma warning disable CS0618 // Type or member is obsolete.
            AnimationPlayableUtilities.Play(animator, _RootPlayable, _Graph);
#pragma warning restore CS0618 // Type or member is obsolete.

That's Not A Solution

Well the documentation page for AnimationPlayableUtilities.Play gives a solution:

AnimationPlayableOutput playableOutput = AnimationPlayableOutput.Create(graph, "AnimationClip", animator);
playableOutput.SetSourcePlayable(playable, 0);
graph.SetTimeUpdateMode(DirectorUpdateMode.GameTime);
graph.Play();

But if you decompile the AnimationPlayableUtilities.Play method you get this (I used IL Spy):

AnimationPlayableOutput output = AnimationPlayableOutput.Create(graph, "AnimationClip", animator);
output.SetSourcePlayable(playable, 0);
graph.SyncUpdateAndTimeMode(animator);
graph.Play();

See the difference? The new example calls SetTimeUpdateMode(DirectorUpdateMode.GameTime) which makes the graph update using regular game time as you would expect from animations most of the time.

Sometimes you might want to ignore Time.timeScale and there's a DirectorUpdateMode for that too.

But what if you want the graph to update using the fixed timestep (in time with FixedUpdate and physics updates) like if you set the Animator's Update Mode to Animate Physics? Well that's too bad, you can't because there's no mode for that. The only way I know of to get the graph to update using the fixed timestep is to set the Animator's Update Mode and call AnimationPlayableUtilities.Play which calls SyncUpdateAndTimeMode. You can't call SyncUpdateAndTimeMode directly because it's internal and it didn't even seem to work when I tried calling it using reflection.

So yeah, they just deprecated a feature that works 100% as expected and has no alternative. I'll report a Unity Bug and hope they un-deprecate it (or better yet, add DirectorUpdateMode.FixedGameTime).

KybernetikGames commented 2 years ago

I've submitted a bug report: Case 1427366

KybernetikGames commented 2 years ago

Animancer v7.3 is now available and has this fix in it.