diff --git a/src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs b/src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs index 970963c8..246b97b2 100644 --- a/src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs +++ b/src/LitMotion/Assets/LitMotion/Runtime/MotionBuilder.cs @@ -36,6 +36,7 @@ public static void Return(MotionBuilderBuffer buffer) buffer.AnimationCurve = default; buffer.Scheduler = default; buffer.IsPreserved = default; + buffer.BindOnSchedule = default; if (buffer.Version != ushort.MaxValue) { @@ -47,6 +48,7 @@ public static void Return(MotionBuilderBuffer buffer) public ushort Version; public MotionBuilderBuffer NextNode; public bool IsPreserved; + public bool BindOnSchedule; public MotionData Data = MotionData.Default; public MotionCallbackData CallbackData = MotionCallbackData.Default; @@ -187,6 +189,19 @@ public readonly MotionBuilder WithCancelOnError(bool return this; } + /// + /// Bind values when scheduling the motion. + /// + /// Whether to bind on sheduling + /// This builder to allow chaining multiple method calls. + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public readonly MotionBuilder WithBindOnSchedule(bool bindOnSchedule = true) + { + CheckBuffer(); + buffer.BindOnSchedule = bindOnSchedule; + return this; + } + /// /// Specifies the scheduler that schedule the motion. /// @@ -299,6 +314,22 @@ public readonly MotionBuilder Preserve() [MethodImpl(MethodImplOptions.AggressiveInlining)] internal MotionHandle Schedule(IMotionScheduler scheduler, ref MotionData data, ref MotionCallbackData callbackData) { + if (buffer.BindOnSchedule && callbackData.UpdateAction != null) + { + callbackData.InvokeUnsafe( + default(TAdapter).Evaluate( + ref data.StartValue, + ref data.EndValue, + ref data.Options, + new() { Progress = data.Core.Ease switch + { + Ease.CustomAnimationCurve => data.Core.AnimationCurve.Evaluate(0f), + _ => EaseUtility.Evaluate(0f, data.Core.Ease) + } + } + )); + } + MotionHandle handle; if (scheduler == null) diff --git a/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs b/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs new file mode 100644 index 00000000..10458197 --- /dev/null +++ b/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs @@ -0,0 +1,47 @@ +using NUnit.Framework; +using UnityEngine; + +namespace LitMotion.Tests.Runtime +{ + public class BindOnScheduleTest + { + [Test] + public void Test_BindOnSchedule() + { + var value = 0f; + + var motion = LMotion.Create(1f, 0f, 1f) + .Bind(x => value = x); + + motion.Cancel(); + + Assert.That(value, Is.EqualTo(0f)); + + value = 0f; + + motion = LMotion.Create(1f, 0f, 1f) + .WithBindOnSchedule() + .Bind(x => value = x); + + motion.Cancel(); + + Assert.That(value, Is.EqualTo(1f)); + } + + [Test] + public void Test_BindOnSchedule_AnimationCurve() + { + var curve = AnimationCurve.EaseInOut(0f, 1f, 1f, 0f); + + var value = 0f; + var motion = LMotion.Create(0f, 1f, 1f) + .WithEase(curve) + .WithBindOnSchedule() + .Bind(x => value = x); + + motion.Cancel(); + + Assert.That(value, Is.EqualTo(1f)); + } + } +} diff --git a/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs.meta b/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs.meta new file mode 100644 index 00000000..ce9f9725 --- /dev/null +++ b/src/LitMotion/Assets/LitMotion/Tests/Runtime/BindOnScheduleTest.cs.meta @@ -0,0 +1,2 @@ +fileFormatVersion: 2 +guid: c0b80c588b36d4181be814f112e42bc0 \ No newline at end of file