# MotionHandleExtensions

ハンドルをGameObjectの寿命、コルーチンや非同期処理に結び付けます。直接awaitする場合とToValueTaskではキャンセル時の結果が異なります。

Namespace: MeatMotion

Assembly: MeatMotion

```csharp
public static class MotionHandleExtensions
```

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

## ValueTaskで完了を受け取る

`using MeatMotion;` を追加します。完了済みのハンドルを使うと、この例のValueTaskは同期的に終了します。通常のasyncメソッドではawaitできます。

```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

ハンドルが現在も元のスロットを指しているか確認します。完了直後はIsActiveと結果が異なる場合があります。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `bool`

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

## IsActive

ハンドルに対応するTweenが有効か返します。保持したTweenは終端に到達した後も有効な場合があります。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `bool`

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

## IsPlaying

有効で終端に達していないTweenならtrueを返します。PlaybackSpeedが0でもこの条件で判定します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `bool`

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

## Preserve

完了後もハンドルから再シークできるようにルートTweenを保持します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `MotionHandle`

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

## Complete

有限のルートTweenを終端まで進めて完了させます。無限ループやシーケンスが所有する子には使用できません。

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

引数

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。

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

## TryComplete

再生中の有限のルートなら完了してtrueを返します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `bool`

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

## Cancel

現在の値でルートTweenを停止し、キャンセル通知を発行します。

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

引数

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。

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

## TryCancel

キャンセル可能なルートなら停止してtrueを返します。無効なハンドルや親が所有する子ではfalseになります。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `bool`

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

## GetDebugName

Editor・Development Buildでデバッグ名またはハンドルの文字列表現を返します。そこで無効なハンドルは例外になります。Release Buildでは常に文字列表現を返します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `string`

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

## AddTo

GameObject・Componentに寿命を結び付けるか、ハンドルのコレクションに追加します。MonoBehaviourを直接渡す形式はコンポーネント自身の破棄トークンを使います。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `compositeMotionHandle` (`CompositeMotionHandle`) ハンドルをまとめて管理するコレクション。
- 戻り値: `MotionHandle`

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

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `target` (`GameObject`) Tweenの寿命を結び付けるオブジェクト。
- 戻り値: `MotionHandle`

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

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `target` (`Component`) Tweenの寿命を結び付けるオブジェクト。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `target` (`Component`) Tweenの寿命を結び付けるオブジェクト。
- `linkBehaviour` (`LinkBehavior`) オブジェクトの破棄・無効化時に適用する操作。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `target` (`GameObject`) Tweenの寿命を結び付けるオブジェクト。
- `linkBehaviour` (`LinkBehavior`) オブジェクトの破棄・無効化時に適用する操作。
- 戻り値: `MotionHandle`

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

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `target` (`MonoBehaviour`) Tweenの寿命を結び付けるオブジェクト。
- 戻り値: `MotionHandle`

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

## ToDisposable

Dispose時に指定した操作をハンドルへ適用するIDisposableを返します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `disposeBehavior = DisposeBehavior.Cancel` (`DisposeBehavior`) 登録したIDisposableを破棄したときに適用する操作。
- 戻り値: `IDisposable`

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

## ToYieldInstruction

Unityのコルーチンがyield returnで終了を待てるIEnumeratorを返します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `IEnumerator`

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

## GetAwaiter

await handleで完了またはキャンセルまで待つためのawaiterを返します。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- 戻り値: `MotionAwaiter`

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

## ToValueTask

ValueTaskで終了を待ちます。既定ではTween自体のキャンセルで待機もキャンセルされます。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancelBehavior` (`CancelBehavior`) トークンがキャンセルされたときにTweenへ適用する操作。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancelBehavior` (`CancelBehavior`) トークンがキャンセルされたときにTweenへ適用する操作。
- `cancelAwaitOnMotionCanceled` (`bool`) Tween自体のキャンセルで待機もキャンセルするかを指定します。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `ValueTask`

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

## ToAwaitable

UnityのAwaitableで終了を待ちます。待機トークンがキャンセルされたときのTweenの扱いを指定できます。

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

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancelBehavior` (`CancelBehavior`) トークンがキャンセルされたときにTweenへ適用する操作。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `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)
```

引数と戻り値

- `handle` (`this MotionHandle`) 操作・関連付けの対象となるハンドル。
- `cancelBehavior` (`CancelBehavior`) トークンがキャンセルされたときにTweenへ適用する操作。
- `cancelAwaitOnMotionCanceled` (`bool`) Tween自体のキャンセルで待機もキャンセルするかを指定します。
- `cancellationToken = default` (`CancellationToken`) 待機をキャンセルするためのトークン。
- 戻り値: `Awaitable`

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