Skip to content

Instantly share code, notes, and snippets.

@thsbrown
Created August 17, 2022 19:47
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thsbrown/da77c535adaf8cfb7309b0b06dd85df5 to your computer and use it in GitHub Desktop.
Save thsbrown/da77c535adaf8cfb7309b0b06dd85df5 to your computer and use it in GitHub Desktop.
DOTween + Unity Timeline Example
public class DOTweenBehavior : PlayableBehaviour
{
public bool tweenPosition;
public Vector3 targetPosition;
public bool tweenRotation;
public Vector3 targetRotation;
public Ease ease;
private bool firstFrameProcessed;
private Sequence sequence;
private Transform transform;
private Vector3 defaultPosition;
private Vector3 defaultRotation;
public override void ProcessFrame(Playable playable, FrameData info, object playerData)
{
transform = playerData as Transform;
if (transform == null)
{
return;
}
//if the first frame hasnt been processed, we know we are starting a new clip play / scrub
//so make sure to save any defaults before we start animating things, so we can easily rest them later
if (!firstFrameProcessed)
{
defaultPosition = transform.position;
defaultRotation = transform.eulerAngles;
firstFrameProcessed = true;
}
//kill pre-existing tween if any and create a new one
sequence?.Kill();
sequence = DOTween.Sequence();
//if tweenPosition is enabled, append it to our sequence in order to tween it
var myPosition = defaultPosition;
if (tweenPosition)
{
sequence.Append(DOTween.To(
() => myPosition,
x => myPosition = x,
targetPosition,
(float)playable.GetDuration()));
}
//if tweenRotation is enabled, append it to our sequence in order to tween it
var myRotation = defaultRotation;
if (tweenRotation)
{
sequence.Insert(0,DOTween.To(
() => myRotation,
x => myRotation = x,
targetRotation,
(float)playable.GetDuration()));
}
//set any global sequence params
sequence.SetEase(ease);
//move to the point in time of our sequence and set our transform accordingly
sequence.Goto((float)playable.GetTime());
transform.position = myPosition;
transform.eulerAngles = myRotation;
}
public override void OnBehaviourPause(Playable playable, FrameData info)
{
//clip is no longer being played / scrubbed so reset all our defaults back
//see https://youtu.be/LSrcQJHDUT4?t=422
firstFrameProcessed = false;
if (transform == null)
{
return;
}
transform.position = defaultPosition;
transform.eulerAngles = defaultRotation;
base.OnBehaviourPause(playable, info);
}
}
/// <summary>
/// The clip that contains the data that we will use when animating our DOTween
/// </summary>
[Serializable]
public class DOTweenClip : PlayableAsset, ITimelineClipAsset
{
/// <summary>
/// If true will allow us to tween our current position to a target position.
/// </summary>
[Tooltip("If true will allow us to tween our current position to a target position.")]
public bool tweenPosition;
/// <summary>
/// The position we will tween to, if tweenPosition is enabled.
/// </summary>
[Tooltip("The position we will tween to, if tweenPosition is enabled.")]
[ShowIf("tweenPosition")]
public Vector3 targetPosition;
/// <summary>
/// If true will allow us to tween our current rotation to a target rotation.
/// </summary>
[Tooltip("If true will allow us to tween our current rotation to a target rotation.")]
public bool tweenRotation;
/// <summary>
/// The rotation we will tween to, if tweenRotation is enabled.
/// </summary>
[Tooltip("The rotation we will tween to, if tweenRotation is enabled. ")]
[ShowIf("tweenRotation")]
public Vector3 targetRotation;
/// <summary>
/// The ease that we will use for both our tweenPosition and tweenRotation
/// </summary>
[Tooltip("The ease that we will use for both our tweenPosition and tweenRotation")]
public Ease ease;
public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
{
var playable = ScriptPlayable<DOTweenBehavior>.Create(graph);
var tween = playable.GetBehaviour();
tween.tweenPosition = tweenPosition;
tween.targetPosition = targetPosition;
tween.tweenRotation = tweenRotation;
tween.targetRotation = targetRotation;
tween.ease = ease;
return playable;
}
public ClipCaps clipCaps => ClipCaps.None;
}
/// <summary>
/// The track that we will be able to manipulate in modify our DOTween transform tween
/// </summary>
[TrackColor(148/255f,222/255f,89/255f)]
[TrackBindingType(typeof(Transform))]
[TrackClipType(typeof(DOTweenClip))]
public class DOTweenTrack : TrackAsset
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment