# MotionHandleExtensions

Link a handle to an object's lifetime, yield it in a coroutine or wait asynchronously. Direct await and ToValueTask have different cancellation semantics.

Namespace: MeatMotion

Assembly: MeatMotion

```csharp
public static class MotionHandleExtensions
```

[Runtime/Lifecycle/MotionHandleExtensions.cs · L69](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L69)

## Wait for completion via ValueTask

Add `using MeatMotion;`. In this example completion resolves the ValueTask before its result is read. In an async method you can await the task.

```csharp
using var world = new MotionWorld();
var handle = MMotion.Create(0f, 1f, 1f).WithScheduler(world).RunWithoutBinding();
var wait = handle.ToValueTask();
handle.Complete();
wait.GetAwaiter().GetResult();
```

## IsValid

Check whether the handle still resolves to its original slot. This can differ from IsActive immediately after completion.

```csharp
public static bool IsValid(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `bool`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L71](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L71)

## IsActive

Check whether the motion is active. A preserved motion can remain active at its endpoint.

```csharp
public static bool IsActive(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `bool`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L72](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L72)

## IsPlaying

Return true for an active motion that has not ended. A playback speed of zero alone does not make this false.

```csharp
public static bool IsPlaying(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `bool`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L73](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L73)

## Preserve

Retain a root motion so the handle can seek it again after it completes.

```csharp
public static MotionHandle Preserve(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L74](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L74)

## Complete

Seek a finite root motion to its end and complete it. Infinite motions and sequence-owned children cannot be completed directly.

```csharp
public static void Complete(this MotionHandle handle)
```

Parameters

- `handle` (`this MotionHandle`) Handle to control or link.

[Runtime/Lifecycle/MotionHandleExtensions.cs · L75](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L75)

## TryComplete

Complete a playing, finite root and report whether the request succeeded.

```csharp
public static bool TryComplete(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `bool`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L76](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L76)

## Cancel

Stop a root motion at its current value and notify cancellation.

```csharp
public static void Cancel(this MotionHandle handle)
```

Parameters

- `handle` (`this MotionHandle`) Handle to control or link.

[Runtime/Lifecycle/MotionHandleExtensions.cs · L77](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L77)

## TryCancel

Cancel an active root and return true. Invalid handles and sequence-owned children return false.

```csharp
public static bool TryCancel(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `bool`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L78](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L78)

## GetDebugName

In the Editor and Development Builds, return the recorded name or handle string. Invalid handles throw there. Release Builds always return the handle string.

```csharp
public static string GetDebugName(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `string`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L79](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L79)

## AddTo

Link the motion to a GameObject or component, or add it to a collection. The MonoBehaviour overload uses that component's own destruction token.

```csharp
public static MotionHandle AddTo(this MotionHandle handle, CompositeMotionHandle compositeMotionHandle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `compositeMotionHandle` (`CompositeMotionHandle`) Collection used to manage the handles together.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L88](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L88)

```csharp
public static MotionHandle AddTo(this MotionHandle handle, GameObject target)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `target` (`GameObject`) Object to which the motion lifetime is linked.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L94](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L94)

```csharp
public static MotionHandle AddTo(this MotionHandle handle, Component target)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `target` (`Component`) Object to which the motion lifetime is linked.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L95](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L95)

```csharp
public static MotionHandle AddTo(this MotionHandle handle, Component target, LinkBehavior linkBehaviour)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `target` (`Component`) Object to which the motion lifetime is linked.
- `linkBehaviour` (`LinkBehavior`) Action taken on destruction or disable.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L96](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L96)

```csharp
public static MotionHandle AddTo(this MotionHandle handle, GameObject target, LinkBehavior linkBehaviour)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `target` (`GameObject`) Object to which the motion lifetime is linked.
- `linkBehaviour` (`LinkBehavior`) Action taken on destruction or disable.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L97](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L97)

```csharp
public static MotionHandle AddTo(this MotionHandle handle, MonoBehaviour target)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `target` (`MonoBehaviour`) Object to which the motion lifetime is linked.
- Returns: `MotionHandle`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L103](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L103)

## ToDisposable

Return an IDisposable that applies the selected action to the handle when disposed.

```csharp
public static IDisposable ToDisposable(this MotionHandle handle, DisposeBehavior disposeBehavior = DisposeBehavior.Cancel)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `disposeBehavior = DisposeBehavior.Cancel` (`DisposeBehavior`) Action to apply when the registered IDisposable is disposed.
- Returns: `IDisposable`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L139](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L139)

## ToYieldInstruction

Return an IEnumerator for waiting on the motion from a Unity coroutine.

```csharp
public static IEnumerator ToYieldInstruction(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `IEnumerator`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L141](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L141)

## GetAwaiter

Return the awaiter used by await handle to wait for completion or cancellation.

```csharp
public static MotionAwaiter GetAwaiter(this MotionHandle handle)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- Returns: `MotionAwaiter`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L145](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L145)

## ToValueTask

Wait via ValueTask. By default canceling the motion also cancels the wait.

```csharp
public static ValueTask ToValueTask(this MotionHandle handle, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `ValueTask`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L147](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L147)

```csharp
public static ValueTask ToValueTask(this MotionHandle handle, CancelBehavior cancelBehavior, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancelBehavior` (`CancelBehavior`) Action applied to the tween when the wait token is canceled.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `ValueTask`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L149](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L149)

```csharp
public static ValueTask ToValueTask(this MotionHandle handle, CancelBehavior cancelBehavior, bool cancelAwaitOnMotionCanceled, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancelBehavior` (`CancelBehavior`) Action applied to the tween when the wait token is canceled.
- `cancelAwaitOnMotionCanceled` (`bool`) Whether canceling the motion itself also cancels the wait.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `ValueTask`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L151](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L151)

## ToAwaitable

Wait via Unity Awaitable. Select how token cancellation affects the underlying tween.

```csharp
public static Awaitable ToAwaitable(this MotionHandle handle, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `Awaitable`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L162](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L162)

```csharp
public static Awaitable ToAwaitable(this MotionHandle handle, CancelBehavior cancelBehavior, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancelBehavior` (`CancelBehavior`) Action applied to the tween when the wait token is canceled.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `Awaitable`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L164](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L164)

```csharp
public static Awaitable ToAwaitable(this MotionHandle handle, CancelBehavior cancelBehavior, bool cancelAwaitOnMotionCanceled, CancellationToken cancellationToken = default)
```

Parameters and return value

- `handle` (`this MotionHandle`) Handle to control or link.
- `cancelBehavior` (`CancelBehavior`) Action applied to the tween when the wait token is canceled.
- `cancelAwaitOnMotionCanceled` (`bool`) Whether canceling the motion itself also cancels the wait.
- `cancellationToken = default` (`CancellationToken`) Token used to cancel the wait.
- Returns: `Awaitable`

[Runtime/Lifecycle/MotionHandleExtensions.cs · L166](../../../source/Runtime/Lifecycle/MotionHandleExtensions.cs.html#L166)
