Documentation

Unity Engine

Download Unity

Unity Engine

TimerStateExtensions

Extension methods for TimerState to control playback.
Read time 2 minutesLast updated 3 hours ago

Definition

public static class TimerStateExtensions

Remarks

Call TimerStateExtensions.Play to start a timer, and call TimerStateExtensions.Update once per frame to advance it. A timer advances only while it's playing. The methods modify the timer, so call them on a variable, not on a readonly field or an
in
parameter.
To move a timer to another time without playing through the time in between, use one of these methods:
If the timer advances in the next
Update
after either method, AnimationEventUtility.GatherAnimationEvents includes the animation events at the new time. Both methods apply the TimerState.CycleOffset only when the wrap mode of the timer is WrapMode.Loop.
The following example uses the
Unity.Animations
,
Unity.Entities
,
Unity.IntegerTime
, and
UnityEngine
namespaces. It creates a timer for a clip with Clip.ToTimerState and updates it once per frame.
Additional Resources: TimerState

Examples

// This example plays a clip for a number of frames and returns the state of the timer after the last frame.public static TimerState PlayClip(AnimationClip animationClip, float deltaTime, int frameCount){ using BlobObjectReference<Clip> clipReference = animationClip.ToClipReference(); ref Clip clip = ref clipReference.Value; // Create a playing timer that takes its range, wrap mode, and cycle offset from the clip. TimerState timerState = clip.ToTimerState(DiscreteTime.Zero); // Update the timer once per frame. In a real application, pass the frame delta time. for (int frame = 0; frame < frameCount; ++frame) { timerState.Update(new DiscreteTime(deltaTime)); } // For a one-second clip with Loop Time enabled, three updates of 0.4 seconds // leave the timer at 0.2 seconds, with a loop count of 1. // Without Loop Time, the timer stays at the end of the clip, at 1 second. return timerState;}

Static Methods

Method

Description

JumpToTimeJumps a timer state to a specific time, without advancing the timer from the current time to that time.
PausePauses a TimerState, so that it doesn't advance during following updates of the TimerState. The behavior of a TimerState when paused is the same as stopped, the only difference is how other systems interpret those two states.
PlayStarts playing a TimerState, so that it advances during all following updates of the TimerState.
RewindRewinds a timer state to the start of its range.
StopStops a TimerState, so that it doesn't advance during following updates of the TimerState. The behavior of a TimerState when stopped is the same as paused, the only difference is how other systems interpret those two states.
UpdateAdvances a timer by a time step and applies its wrap mode.