File size: 1,747 Bytes
18a519f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;
namespace Timeline.Samples
{
// A track mixer behaviour that modifies the timeScale. This affects how fast the game plays back
public class TimeDilationMixerBehaviour : PlayableBehaviour
{
private float m_DefaultTimeScale = 1;
// Called every frame that the timeline is Evaluated.
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
int inputCount = playable.GetInputCount();
float timeScale = 0f;
float totalWeight = 0f;
// blend clips together
for (int i = 0; i < inputCount; i++)
{
float inputWeight = playable.GetInputWeight(i);
ScriptPlayable<TimeDilationBehaviour> playableInput = (ScriptPlayable<TimeDilationBehaviour>)playable.GetInput(i);
TimeDilationBehaviour input = playableInput.GetBehaviour();
timeScale += inputWeight * input.timeScale;
totalWeight += inputWeight;
}
// blend to/from the default timeline
Time.timeScale = Mathf.Max(0.0001f, Mathf.Lerp(m_DefaultTimeScale, timeScale, Mathf.Clamp01(totalWeight)));
}
// Called when the playable graph is created, typically when the timeline is played.
public override void OnPlayableCreate(Playable playable)
{
m_DefaultTimeScale = Time.timeScale;
}
// Called when the playable is destroyed, typically when the timeline stops.
public override void OnPlayableDestroy(Playable playable)
{
Time.timeScale = m_DefaultTimeScale;
}
}
}
|