1
0

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時のAwaitableを制御する

Posted at

Disable/Enable時のCoroutineを制御する
Disable/Enable時のUniTaskを制御する

Coroutine/UniTaskでのDisable/Enable時の挙動を見てきました。
ではAwaitableではどうでしょうか?

AwaitableはUnity2023 から実装された機能です。
Unity Documentation

試してみたところ、Coroutine同様、Disableしても動き続けてしまうようです。
なのでCoroutine同様、Disable時は次の処理を待つようにしてみました。

using UnityEngine;

namespace Elix
{
    public class AwaitableCheck : MonoBehaviour
    {
        [SerializeField] TMPro.TextMeshProUGUI m_text = null;

        // Start is called once before the first execution of Update after the MonoBehaviour is created
        void Start()
        {
            //Debug.Log("Start");
            Awaitable awaitable = DoTaskAsync();
        }

        // Update is called once per frame
        void Update()
        {
            //Debug.Log("Update");
        }

        async Awaitable DoTaskAsync()
        {
            int count = 0;
            while (true)
            {
                m_text.text = $"{count}";
                count++;
                await Awaitable.WaitForSecondsAsync(1f);

                while(!gameObject.activeInHierarchy)
                {
                    await Awaitable.NextFrameAsync();
                }
                //Debug.Log("DoTaskAsync");
            }
        }
    }
}

Coroutineよりは挙動が素直で扱いやすそうです。

1
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?