ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2023ǯ10·î31Æü(²Ð) 13:44:52ÍúÎò
¤È¤Æ¤âÊØÍø¤Ê½èÍý¤Ç¤¹¤¬¡¢UniTask ¤È¤¤¤¦Ê̤ÎÈóƱ´ü½èÍý¤âÍÑ°Õ¤µ¤ì¤Æ¤Þ¤¹¡£
¡¡Î¾Êý¤Î»È¤¤Êý¤òÍý²ò¤¹¤ë¤³¤È¤Ç¡¢¾å¼ê¤Ë»È¤¤Ê¬¤±¤Æ¤¤¤¯¤³¤È¤¬½ÐÍè¤Þ¤¹¤Î¤Ç¡¢
¤³¤³¤Ç¤Ï¡¢¤³¤ì¤«¤é UniTask ¤ò³Ð¤¨¤Æ¤¤¤¤¿¤¤Êý¸þ¤±¤Ë½èÍý¤òÈæ³Ó¤·¤Æ¡¢ÃÖ¤´¹¤¨¤Ê¤¬¤é UniTask ¤ò³Ø½¬¤·¤Þ¤¹¡£
¡¡¤Ê¤ª¡¢¡Ö¤½¤â¤½¤â UniTask ¤È¤Ï¡©¡×¤È¤¤¤¦Éôʬ¤Ë¤Ä¤¤¤Æ¤Ï¿¨¤ì¤Þ¤»¤ó¡£
¡¡UniTask ¤ò³Ø½¬¤·¤Æ¤ß¤¿¤¤¤±¤É¡¢¤É¤Î¤è¤¦¤Ë¼ÂÁõ¤·¤Æ¤¤¤¤¤Î¤«¡¢Æ³Æþ¤Î»å¸ý¤¬¸«Åö¤¿¤é¤Ê¤¤¤È¤¤¤¦Êý¤òÂоݤȤ·¤Æ¤¤¤Þ¤¹¡£
¡¡¥³¥ë¡¼¥Á¥ó¤ò¥¹¥¿¡¼¥È¤·¡¢»þ´Ö¤Î·Ð²á¤Ë¹ç¤ï¤»¤Æ¥í¥°¤òɽ¼¨¤·¤Þ¤¹¡£
using System.Collections; using UnityEngine; public class CoroutineExample : MonoBehaviour { private void Start() { StartCoroutine(MyCoroutine()); } private IEnumerator MyCoroutine() { Debug.Log("Coroutine Started"); yield return new WaitForSeconds(2.0f); Debug.Log("Coroutine Finished"); } }
¡¡¤³¤³¤Ç¤ÏÀè¤Û¤É¤Î¥³¥ë¡¼¥Á¥ó¤Î½èÍý¤ò UniTask ¤òÍøÍѤ·¤ÆÃÖ¤´¹¤¨¤¿¾ì¹ç¤Î¼ÂÁõÎã¤Ç¤¹¡£
using Cysharp.Threading.Tasks; using UnityEngine; public class UniTaskExample : MonoBehaviour { private async void Start() { Debug.Log("UniTask Started"); await UniTask.Delay(TimeSpan.FromSeconds(2)); Debug.Log("UniTask Finished"); } }
¡¡ÈóƱ´ü½èÍý¤Ë¤Ä¤¤¤Æ¡¢¥³¥ë¡¼¥Á¥ó¤È UniTask ¤ò¡¢½ñ¤Êý¤ÇÈæ³Ó¤·¤Æ¤ß¤Þ¤·¤ç¤¦¡£
Àè¤Ë¥³¥ë¡¼¥Á¥ó¤Î½èÍý¤òÄ󼨤·¡¢¤½¤Î¸å¡¢Æ±¤¸ÆâÍƤò UniTask ¤ÇºîÀ®¤·¤Æ¤¤¤Þ¤¹¡£
using System.Collections; using UnityEngine; public class CoroutineExample : MonoBehaviour { private void Start() { StartCoroutine(MyCoroutine()); } private IEnumerator MyCoroutine() { Debug.Log("Coroutine Started"); yield return new WaitForSeconds(2.0f); Debug.Log("Waited for 2 seconds in Coroutine"); yield return new WaitForEndOfFrame(); // ¸½ºß¤Î¥Õ¥ì¡¼¥à¤Î½ªÎ»¤òÂÔ¤Ä Debug.Log("Waited for EndOfFrame in Coroutine"); yield return new WaitForFixedUpdate(); // ¼¡¤ÎFixedUpdate¤òÂÔ¤Ä Debug.Log("Waited for FixedUpdate in Coroutine"); yield return new WaitUntil(() => Input.GetKeyDown(KeyCode.Space)); // ¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤ë¤Þ¤ÇÂÔ¤Ä Debug.Log("Waited until Space key is pressed in Coroutine"); yield return null; // 1¥Õ¥ì¡¼¥àÂÔµ¡ Debug.Log("Waited for 1 frame in Coroutine"); } }
¡¡¤³¤³¤«¤é¤Ï UniTask ¤Î¾ì¹ç¤Ç¤¹¡£
using Cysharp.Threading.Tasks; using UnityEngine; public class UniTaskExample : MonoBehaviour { private async void Start() { Debug.Log("UniTask Started"); await UniTask.Delay(TimeSpan.FromSeconds(2)); Debug.Log("Waited for 2 seconds in UniTask"); await UniTask.NextFrame(); // ¸½ºß¤Î¥Õ¥ì¡¼¥à¤Î½ªÎ»¤òÂÔ¤Ä Debug.Log("Waited for EndOfFrame in UniTask"); await UniTask.WaitForFixedUpdate(); // ¼¡¤ÎFixedUpdate¤òÂÔ¤Ä Debug.Log("Waited for FixedUpdate in UniTask"); await UniTask.WaitUntil(() => Input.GetKeyDown(KeyCode.Space)); // ¾ò·ï¤¬Ëþ¤¿¤µ¤ì¤ë¤Þ¤ÇÂÔ¤Ä Debug.Log("Waited until Space key is pressed in UniTask"); await UniTask.Yield(PlayerLoopTiming.PostLateUpdate); // 1¥Õ¥ì¡¼¥àÂÔµ¡ Debug.Log("Waited for 1 frame in UniTask"); } }
¡¡¤É¤Á¤é¤âƱ¤¸¤è¤¦¤ËÈóƱ´ü½èÍý¤¬ºîÀ®¤Ç¤¤Þ¤¹¡£
¡¡¥³¥ë¡¼¥Á¥ó¤ÈUniTask¤ÎÈóƱ´ü½èÍý¤Ë¤Ï¡¢½ñ¤Êý¤À¤±¤Ç¤Ê¤¯¡¢¤¤¤¯¤Ä¤«¤Î½ÅÍפʰ㤤¤¬Â¸ºß¤·¤Þ¤¹¡£
¡¡°Ê²¼¤Ë¡¢¤½¤ì¤é¤Î°ã¤¤¤ò¤Þ¤È¤á¤Þ¤¹¡£
¡¡¡ã¥³¥ë¡¼¥Á¥ó¡ä
¡¡¡¡¥³¥ë¡¼¥Á¥ó¤ÏIEnumerator·¿¤òÌá¤êÃͤȤ·¤Æ»ý¤Á¡¢¥³¥ë¡¼¥Á¥óÆâ¤Çyield return¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»ÈÍѤ·¤ÆÈóƱ´ü½èÍý¤òɽ¸½¤·¤Þ¤¹¡£
¡¡¡¡¼ÂºÝ¤Î·ë²Ì¤òÊÖ¤¹¤Î¤ÏÆñ¤·¤¤¤¿¤á¡¢Ä̾ï¤Ï¥³¥ë¡¼¥Á¥ó¼«ÂΤ¬¾õÂÖ¤òÄÉÀפ¹¤ëÌÜŪ¤Ë»ÈÍѤµ¤ì¤Þ¤¹¡£
¡¡¡ãUniTask¡ä
¡¡¡¡¡¡
¡¡¡¡UniTask¤ÏUniTask·¿¤òÌá¤êÃͤȤ·¤Æ»ý¤Á¡¢ÈóƱ´ü½èÍý¤¬´°Î»¤·¤¿·ë²Ì¤ò»ý¤Ä¤³¤È¤¬¤Ç¤¤Þ¤¹¡£¤³¤ì¤Ë¤è¤ê¡¢ÈóƱ´ü½èÍý¤Î·ë²Ì¤ò´Êñ¤Ë¼èÆÀ¤Ç¤¤Þ¤¹¡£
¡¡¡ã¥³¥ë¡¼¥Á¥ó¡ä
¡¡¡¡¥³¥ë¡¼¥Á¥ó¤ÏÌÀ¼¨Åª¤Êyield return¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»ÈÍѤ·¤ÆÂÔµ¡¤òɽ¸½¤·¡¢¥³¡¼¥É¤ÎÆÃÄê¤Î¥Ý¥¤¥ó¥È¤Ç½èÍý¤¬°ì»þÄä»ß¤µ¤ì¤Þ¤¹¡£
¡¡¡¡¥³¥ë¡¼¥Á¥óÆâ¤Î½èÍý¤ÏÄ̾ïÂÔµ¡¥¹¥Æ¡¼¥È¥á¥ó¥È¤Î¼þ¤ê¤ËÁȤ߹þ¤Þ¤ì¤Þ¤¹¡£
¡¡¡ãUniTask¡ä
¡¡¡¡¡¡UniTask¤Ç¤Ï¡¢ÈóƱ´ü¥á¥½¥Ã¥É¤òawait¤¹¤ë¤³¤È¤Ë¤è¤Ã¤ÆÂÔµ¡¤òɽ¸½¤·¤Þ¤¹¡£
¡¡¡¡¡¡ÂÔµ¡¤ÏÈóƱ´ü½èÍý¤Î°ìÉô¤È¤·¤Æ¼«Á³¤Ê·Á¤ÇÁȤ߹þ¤Þ¤ì¡¢¥³¡¼¥É¤ÎľÎ󲽤¬ÍưפǤ¹¡£
¡¡¡ã¥³¥ë¡¼¥Á¥ó¡ä
¡¡¡¡¡¡¥³¥ë¡¼¥Á¥óÆâ¤ÇÎã³°¤¬¥¹¥í¡¼¤µ¤ì¤ë¤È¡¢Unity¥¨¥Ç¥£¥¿Æâ¤Ç¥¨¥é¡¼¥í¥°¤¬É½¼¨¤µ¤ì¤Þ¤¹¤¬¡¢¥¢¥×¥ê¥±¡¼¥·¥ç¥óÁ´ÂΤˤÏÇȵڤ·¤Þ¤»¤ó¡£
¡¡¡ãUniTask¡ä
¡¡¡¡¡¡UniTask¤Ïtry...catch¥Ö¥í¥Ã¥¯¤ò»ÈÍѤ·¤ÆÎã³°½èÍý¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¡¢Îã³°¤ò¥¥ã¥Ã¥Á¤·¤ÆŬÀڤʽèÍý¤ò¼Â¹Ô¤Ç¤¤Þ¤¹¡£
¡¡¡¡¡¡UniTask¤Ï¥¢¥×¥ê¥±¡¼¥·¥ç¥óÁ´ÂΤÎÎã³°¥Ï¥ó¥É¥ê¥ó¥°¤ò¥µ¥Ý¡¼¥È¤·¤ä¤¹¤¤´Ä¶¤Ë¤¢¤ê¤Þ¤¹¡£
¡¡UniTask¤ÏÆâÉôŪ¤Ë¹âÅÙ¤ÊÈóƱ´ü¥é¥¤¥Ö¥é¥ê¤ò»ÈÍѤ·¡¢¥³¥ë¡¼¥Á¥ó¤è¤ê¤â¹âÀǽ¤Ç¸úΨŪ¤Ç¤¹¡£
¡¡UniTask¤Ï¤è¤ê¾¯¤Ê¤¤¥ª¡¼¥Ð¡¼¥Ø¥Ã¥É¤ò»ý¤Á¡¢¥á¥â¥ê¸úΨ¤¬¹â¤¤¤¿¤á¡¢Â¿¤¯¤Î¾ìÌ̤ǥ³¥ë¡¼¥Á¥ó¤è¤ê¤â¹¥¤Þ¤·¤¤ÁªÂò»è¤È¤Ê¤ê¤Þ¤¹¡£
using System.Collections; using UnityEngine; public class CoroutineErrorHandling : MonoBehaviour { private void Start() { StartCoroutine(MyCoroutine()); } private IEnumerator MyCoroutine() { Debug.Log("Coroutine Started"); yield return new WaitForSeconds(2.0f); Debug.LogError("Coroutine Error"); } }
using Cysharp.Threading.Tasks; using UnityEngine; public class UniTaskErrorHandling : MonoBehaviour { private async void Start() { Debug.Log("UniTask Started"); await UniTask.Delay(TimeSpan.FromSeconds(2)); Debug.LogError("UniTask Error"); } }
¡¡UniTask¤Ï¥³¥ë¡¼¥Á¥ó¤è¤ê¤â´Êñ¤Ë¥¥ã¥ó¥»¥ë¤Ç¤¤Þ¤¹¤¬¡¢¥³¥ë¡¼¥Á¥ó¤Ç¤Ï¥¥ã¥ó¥»¥ë¤¬Æñ¤·¤¤¤Ç¤¹¡£
using System.Collections; using UnityEngine; public class CoroutineCancellationExample : MonoBehaviour { private Coroutine myCoroutine; private void Start() { myCoroutine = StartCoroutine(MyCoroutine()); // 2Éøå¤Ë¥³¥ë¡¼¥Á¥ó¤ò¥¥ã¥ó¥»¥ë StartCoroutine(CancelCoroutineAfterDelay(2.0f)); } private IEnumerator MyCoroutine() { Debug.Log("Coroutine Started"); while (true) { yield return null; } } private IEnumerator CancelCoroutineAfterDelay(float delay) { yield return new WaitForSeconds(delay); StopCoroutine(myCoroutine); // ¥³¥ë¡¼¥Á¥ó¤ò¥¥ã¥ó¥»¥ë Debug.Log("Coroutine Canceled"); } }
using Cysharp.Threading.Tasks; using UnityEngine; public class UniTaskCancellationExample : MonoBehaviour { private async void Start() { // UniTask¤ò»ÈÍѤ·¤ÆÈóƱ´ü¥¿¥¹¥¯¤ò³«»Ï var task = MyUniTask(); // 2Éøå¤Ë¥¿¥¹¥¯¤ò¥¥ã¥ó¥»¥ë await UniTask.Delay(2000); task.Cancel(); Debug.Log("UniTask Canceled"); } private async UniTask MyUniTask() { Debug.Log("UniTask Started"); while (true) { await UniTask.Yield(); } } }
¡¡¤³¤Î¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤Ï¡¢¥³¥ë¡¼¥Á¥ó¤ÈUniTask¤Î¥¥ã¥ó¥»¥ë²Äǽ¤Ê¥¿¥¹¥¯¤Î°ã¤¤¤ò¼¨¤·¤Æ¤¤¤Þ¤¹¡£
¡¡¥³¥ë¡¼¥Á¥ó¤Î¾ì¹ç¡¢¥¥ã¥ó¥»¥ë¤ò¼ÂÁõ¤¹¤ë¤Î¤ËÄÉ²Ã¤Î¥í¥¸¥Ã¥¯¤¬É¬Íפǡ¢StopCoroutine¤ò»ÈÍѤ·¤Æ¥³¥ë¡¼¥Á¥ó¤ò¥¥ã¥ó¥»¥ë¤·¤Þ¤¹¡£
¡¡°ìÊý¡¢UniTask¤Ç¤Ïawait¤ÎÂå¤ï¤ê¤Ëtask.Cancel()¤ò»ÈÍѤ·¤Æ´Êñ¤Ë¥¿¥¹¥¯¤ò¥¥ã¥ó¥»¥ë¤Ç¤¤Þ¤¹¡£
¡¡¤½¤¦¤¤¤Ã¤¿ÅÀ¤Ë¤ª¤¤¤Æ¤â¡¢UniTask¤Ï¥¥ã¥ó¥»¥ë²Äǽ¤Ê¥¿¥¹¥¯¤ò´ÉÍý¤·¤ä¤¹¤¯¡¢¥³¡¼¥É¤ò¤è¤ê¥·¥ó¥×¥ë¤ËÊݤÁ¤Þ¤¹¡£
using System.Collections; using UnityEngine; public class CoroutineMultipleTasks : MonoBehaviour { private void Start() { StartCoroutine(DoMultipleTasks()); } private IEnumerator DoMultipleTasks() { Debug.Log("Coroutine Started"); yield return StartCoroutine(Task1()); yield return StartCoroutine(Task2()); Debug.Log("All Coroutine Tasks Finished"); } private IEnumerator Task1() { yield return new WaitForSeconds(2.0f); Debug.Log("Coroutine Task 1 Finished"); } private IEnumerator Task2() { yield return new WaitForSeconds(1.0f); Debug.Log("Coroutine Task 2 Finished"); } }
using Cysharp.Threading.Tasks; using UnityEngine; public class UniTaskMultipleTasks : MonoBehaviour { private async void Start() { Debug.Log("UniTask Started"); await Task1().ToUniTask(); await Task2().ToUniTask(); Debug.Log("All UniTask Tasks Finished"); } private async UniTaskVoid Task1() { await UniTask.Delay(TimeSpan.FromSeconds(2)); Debug.Log("UniTask Task 1 Finished"); } private async UniTaskVoid Task2() { await UniTask.Delay(TimeSpan.FromSeconds(1)); Debug.Log("UniTask Task 2 Finished"); } }
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯