Skip to content

Code API

rmfandyplayz edited this page Sep 30, 2026 · 2 revisions

You don't need code to use this tool: a button's On Click can play an animation directly (see From UnityEvents below). This page is for when you do want it.

using rmf_claude.DOTweenUI;

public class Panel : MonoBehaviour
{
    [SerializeField] private UIAnimationPlayer anim;

    public void Open()  => anim.Play("Show");
    public void Close() => anim.Play("Hide", () => gameObject.SetActive(false));
}

Everything on UIAnimationPlayer

// Playing
Sequence Play(string name);
Sequence Play(string name, Action onComplete);                    // only when it finishes naturally
Sequence Play(string name, Action<UIAnimationEndReason> onEnd);   // always, exactly once
void PlayAnimation(string name);                                  // same as Play, for UnityEvents

// Stopping
void Stop(string name, bool complete = false);
void StopAll(bool complete = false);
void StopAnimation(string name);                                  // same as Stop, for UnityEvents

// Checking
bool IsPlaying(string name);
bool IsAnyPlaying { get; }
bool Has(string name);

// Setting up
void ApplyFromState(string name);
void CaptureBaseline();

// Changing values at runtime
bool SetTo(string name, int step, float / Vector3 / Color / string value);
bool SetFrom(string name, int step, float / Vector3 / Color / string value);
void ClearOverrides(string name);
Call What it does
Play(name) Plays the animation. Returns DOTween's Sequence, so you can yield return anim.Play("Show").WaitForCompletion(); in a coroutine.
Play(name, onComplete) Plays it, and runs onComplete only if it finishes on its own. If something interrupts it, onComplete never runs.
Play(name, onEnd) Plays it, and runs onEnd exactly once, however it ends, telling you how. See Knowing how an animation ended.
Stop(name) Stops it where it is. On Complete doesn't fire.
Stop(name, complete: true) Jumps it to its end instead, as if it had finished: SetActive steps still ahead run, sounds don't, and On Complete fires.
StopAll() / StopAll(true) The same, for every animation on this player.
IsPlaying(name) / IsAnyPlaying Whether that animation, or any animation on this player, is running.
Has(name) Whether this player has an animation with that name, including ones from its Animation Set.
ApplyFromState(name) Jumps straight to the animation's starting values without playing it. Handy for starting hidden: void Awake() => anim.ApplyFromState("Show");
CaptureBaseline() Re-reads every "resting value" on this player (see Baseline). Call it after you deliberately move or change an element at runtime. Not while one of its animations is running.
SetTo / SetFrom / ClearOverrides Replace a step's To or From value from code. See below.

Setting From and To from code

Some values are only known while the game runs: the score a counter counts up to, the line of dialogue to type out, the colour of the winning team. Build the step in the Inspector as usual, then hand it the real value before playing:

anim.SetTo("CountUp", 0, score);    // step 0 = the top step in the Inspector
anim.Play("CountUp");

anim.SetFrom("CountUp", 0, oldScore);
anim.SetTo("CountUp", 0, newScore);
anim.Play("CountUp");
  • Steps are counted from 0, top to bottom, as the Inspector lists them. If you reorder the steps, the numbers change, so keep a step you set from code where it is.
  • The value means the same as typing it into the step. If the step's To is set to Baseline, your value is added to the resting value. For "count to exactly this number", set the step's To to Absolute.
  • Pass the kind of value the step animates: a number for a fade, a material value, an int/float or a counter; a Vector3 for a move, size, scale or rotation (a Vector2 works too); a Color; a string for typed text. Punch and shake take their strength as their To. The wrong kind (a colour for a move, anything for a SetActive or sound step) is ignored with a warning, and the call returns false.
  • SetFrom needs the step's From turned on (its button reads FROM). Otherwise it's refused with a warning, because the step has no From to replace.
  • It sticks for every later Play of that animation, until ClearOverrides(name) puts the Inspector's values back. An animation that's already running keeps the value it started with.
  • Only this player is affected. Nothing is written into the Inspector, and other players using the same Animation Set don't see it.

Tip

Keep the real value in your own code. Playing an animation that's already running stops it where it is and starts again from there. So if a coin counter is spammed, the number on screen may be half-counted. Keep the true total in a variable and SetTo that each time, rather than reading the number back off the screen.

Knowing how an animation ended

The plain Play(name, onComplete) only calls back on a natural finish. That's usually fine, but it can bite:

anim.Play("Hide", () => Destroy(gameObject));   // if anything interrupts the Hide, the object is never destroyed

The onEnd version always calls back, exactly once, and tells you why:

anim.Play("Hide", reason =>
{
    if (reason == UIAnimationEndReason.Completed) Destroy(gameObject);
    else                                          gameObject.SetActive(false);
});
UIAnimationEndReason When
Completed It reached its end: naturally, through Stop(name, complete: true), or because the animation interrupting it has Interrupt Others set to COMPLETE REPORT. An animation with nothing in it also counts. This is the only reason that also fires On Complete.
Interrupted Another Play cut it short: the same animation restarting, or a different one with Interrupt Others on. That includes COMPLETE SILENT, which leaves it at its end without counting as a finish.
Stopped Stop, StopAll, or something outside the player killing it, like DOTween.KillAll().
Disabled The object or the player component was disabled while it ran.
Destroyed The player was destroyed while the animation was running.
NotFound There's no animation with that name, so nothing played. A typo still gets you a callback rather than an endless wait.

Note

Destroying an active object reports Disabled, not Destroyed. Unity always disables an object before destroying it, and there's no way to tell in advance which is happening. If you only care whether it finished, compare against Completed and ignore the rest.

When a callback and the animation's own On Complete both fire, On Complete runs first.

Under the hood
  • The "fires exactly once" guarantee holds even when onEnd plays the same animation again: the fire-once flag belongs to each individual play, not to the animation.
  • A kill from outside the player (DOTween.KillAll(), DOTween.Clear()) is caught by the sequence's own OnKill, so it resolves as Stopped rather than leaving the callback waiting. The player has no Update loop polling for this.
  • Destroyed is what you get when Kill On Disable is off, or the object was already inactive when destroyed.
  • Play(name, null) doesn't compile: it's ambiguous between the two callback overloads. Cast it, or just call Play(name).

From UnityEvents

A button's On Click, or an animation's own On Complete, can drive a player with no script at all. Drag the player's object in, pick UIAnimationPlayer, then:

Entry Does
PlayAnimation (string) Plays the animation whose name you type in the box
StopAnimation (string) Stops it where it is
StopAll (bool) Stops everything on this player. Tick the box to finish each one instead (like COMPLETE REPORT, see Interrupting and Chaining)
ApplyFromState (string) Jumps to an animation's starting values without playing
CaptureBaseline () Re-reads the resting values

Putting PlayAnimation in one animation's On Complete plays the next one when it finishes: chaining without code. See Interrupting and Chaining.

Under the hood

Unity's event dropdown only lists methods that return void and take at most one argument. Play returns a Sequence, and Stop's optional parameter still counts as a second argument, so neither appears. PlayAnimation and StopAnimation exist to fill that gap. They can't simply be a void Play(string) overload, because C# doesn't allow overloads that differ only in return type.

Other classes

UIAnimationAudio.SetShared(AudioSource source);   // route every sound step without its own source through yours

Use this once at startup to send UI sounds through your mixer. See Sound.

Clone this wiki locally