1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Disable/Enable時のUniTaskを制御する

Last updated at Posted at 2024-06-21

UniTaskは便利ですが、スクリプトをアタッチしているGameObject や 自身のComponentがDisabledになったとき、またGameObjectがDeleteされた時もタスクが残ってしまいます。

(1)Active all (2)Disable GameObject (3)Disable Component (4)Delete GameObject
image.png image.png image.png image.png
動き続ける 動き続ける 動き続ける 動き続ける

Coroutineの時とは違い、常に動き続けているので、(1)-(3)の対応は
await UniTask.WaitUntil(() => this.isActiveAndEnabled)
のみでよさそうです。

(4)については、System.Threading.CancellationTokenをタスクに渡すことで自動的にタスクを削除してくれるようになります。

using UnityEngine;
using System.Threading.Tasks;
using Cysharp.Threading.Tasks;
using System.Threading;

[HelpURL("https://cysharp.github.io/UniTask/api/Cysharp.Threading.Tasks.html")]
public class UniTaskActiveCheck : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        Debug.Log("Start");
        System.Threading.CancellationToken ct = this.GetCancellationTokenOnDestroy();
        Task0(ct).Forget();
    }

    // Update is called once per frame
    void Update()
    {
    }

    async UniTask Task0(CancellationToken ct)
    {
        int m_count = 0;
        while (true)
        {
            await UniTask.WaitUntil(() => this.isActiveAndEnabled, cancellationToken: ct);
            Debug.Log($"Task0: {m_count}");
            m_count++;
            await Task.Delay(1000, ct);
        }
    }

    private void OnDisable()
    {
        Debug.Log("OnDisable");
    }
    private void OnEnable()
    {
        Debug.Log("OnEnable");
    }
    private void OnDestroy()
    {
        Debug.Log("[OnDestroy]");
    }
}

await UniTask.WaitUntil()の期間中はタスクが一つ増えます
image.png

コルーチンの制御についてはこちら
Awaitableの制御についてはこちら

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?