diff --git a/src/LitMotion/Assets/LitMotion/Runtime/MotionHandleExtensions.cs b/src/LitMotion/Assets/LitMotion/Runtime/MotionHandleExtensions.cs
index 1e5a3010..d1a99b86 100644
--- a/src/LitMotion/Assets/LitMotion/Runtime/MotionHandleExtensions.cs
+++ b/src/LitMotion/Assets/LitMotion/Runtime/MotionHandleExtensions.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections;
+using Cysharp.Threading.Tasks;
using UnityEngine;
namespace LitMotion
@@ -70,6 +71,22 @@ public static MotionHandle AddTo(this MotionHandle handle, Component target)
return handle;
}
+#if UNITY_2022_2_OR_NEWER
+ ///
+ /// Link the motion lifecycle to the target object.
+ ///
+ /// This motion handle
+ /// Target object
+ public static MotionHandle AddTo(this MotionHandle handle, MonoBehaviour target)
+ {
+ target.destroyCancellationToken.Register(() =>
+ {
+ if (handle.IsActive()) handle.Cancel();
+ }, false);
+ return handle;
+ }
+#endif
+
static TComponent GetOrAddComponent(GameObject target) where TComponent : Component
{
if (!target.TryGetComponent(out var component))
diff --git a/src/LitMotion/Assets/LitMotion/Tests/Runtime/AddToTest.cs b/src/LitMotion/Assets/LitMotion/Tests/Runtime/AddToTest.cs
index cf8a64a5..0724dfb8 100644
--- a/src/LitMotion/Assets/LitMotion/Tests/Runtime/AddToTest.cs
+++ b/src/LitMotion/Assets/LitMotion/Tests/Runtime/AddToTest.cs
@@ -20,5 +20,22 @@ public IEnumerator Test_AddTo()
Object.DestroyImmediate(obj);
Assert.IsTrue(canceled);
}
+
+ [UnityTest]
+ public IEnumerator Test_AddTo_MonoBehaviour()
+ {
+ var canceled = false;
+ var obj = new GameObject("Target");
+ var behaviour = obj.AddComponent();
+ var handle = LMotion.Create(0f, 1f, 2f)
+ .WithOnCancel(() => canceled = true)
+ .RunWithoutBinding()
+ .AddTo(behaviour);
+ yield return new WaitForSeconds(0.1f);
+ Object.DestroyImmediate(obj);
+ Assert.IsTrue(canceled);
+ }
+
+ public sealed class TestComponent : MonoBehaviour { }
}
}
\ No newline at end of file