ºÇ½ª¹¹¿·¡§
orika_ex_miyako 2024ǯ06·î09Æü(Æü) 17:32:43ÍúÎò
¤³¤Î¤¿¤á¡¢using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»È¤¦¤³¤È¤Ç¡¢¥¹¥³¡¼¥×½ªÎ»»þ¤Ë¼«Æ°Åª¤Ë¥ê¥½¡¼¥¹¤ò²òÊü¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡¤½¤Î·ë²Ì¡¢¥³¡¼¥É¤Î¿®ÍêÀ¤È¥ê¥½¡¼¥¹´ÉÍý¤¬¸þ¾å¤·¡¢¤è¤ê¥á¥ó¥Æ¥Ê¥Ö¥ë¤Ç¸úΨŪ¤Ê¥¥ã¥ó¥»¥ë½èÍý¤¬¼Â¸½¤µ¤ì¤Þ¤¹¡£
¡¡using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤È CancellationToken.Register ¥á¥½¥Ã¥É¤òÁȤ߹ç¤ï¤»¤ë¤³¤È¤Ç¡¢°Ê²¼¤Î¤è¤¦¤Ê¥á¥ê¥Ã¥È¤¬ÆÀ¤é¤ì¤Þ¤¹¡£
¡¡CancellationToken.Register ¥á¥½¥Ã¥É¤ò»È¤Ã¤Æ¥¥ã¥ó¥»¥ë¤¬È¯À¸¤·¤¿¤È¤¤Ë¼Â¹Ô¤¹¤ë¥Ç¥ê¥²¡¼¥È¤òÅÐÏ¿¤Ç¤¤Þ¤¹¡£
¤³¤Î¥Ç¥ê¥²¡¼¥È¤Ï¡¢using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤Î¥¹¥³¡¼¥×Æâ¤Ç´ÉÍý¤µ¤ì¤Þ¤¹¡£
¥¹¥³¡¼¥×³°¤Ë¥ê¥½¡¼¥¹¤¬»Ä¤ë¤³¤È¤òËɤ°¤¿¤á¡¢ÅÐÏ¿¤µ¤ì¤¿¥Ç¥ê¥²¡¼¥È¤ÎÀ¸Â¸»þ´Ö¤òusing ¥¹¥³¡¼¥×Æâ¤Ë¸ÂÄê¤Ç¤¤Þ¤¹¡£
¡¡using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»ÈÍѤ¹¤ë¤³¤È¤Ç¡¢¥¹¥³¡¼¥×¤¬½ªÎ»¤¹¤ë¥¿¥¤¥ß¥ó¥°¤ÇDispose ¥á¥½¥Ã¥É¤¬¼«Æ°Åª¤Ë¸Æ¤Ó½Ð¤µ¤ì¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢CancellationTokenRegistration ¤¬ÇË´þ¤µ¤ì¡¢ÉÔÍפʥǥꥲ¡¼¥È¤ÎÅÐÏ¿¤ò²ò½ü¤·¤Þ¤¹¡£
¥¹¥³¡¼¥×½ªÎ»»þ¤Ë¥ê¥½¡¼¥¹¤¬³Î¼Â¤Ë²òÊü¤µ¤ì¤ë¤¿¤á¡¢¥ê¥½¡¼¥¹¥ê¡¼¥¯¤òËɻߤ·¤Þ¤¹¡£
¡¡¥¥ã¥ó¥»¥ëÁ°¡§Ä̾ïÄ̤êÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¥Ç¥ê¥²¡¼¥È¤¬¼Â¹Ô¤µ¤ì¤Ê¤¤¡£ ¡¡¥¥ã¥ó¥»¥ë»þ¡§ÅÐÏ¿¤µ¤ì¤Æ¤¤¤ë¥Ç¥ê¥²¡¼¥È¤¬³Î¼Â¤Ë¸Æ¤Ó½Ð¤µ¤ì¡¢Å¬Àڤʥ¥ã¥ó¥»¥ë½èÍý¤¬¹Ô¤ï¤ì¤ë¡£ ¡¡¥¥ã¥ó¥»¥ë¸å¡§using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤òÈ´¤±¤ë¤È¥Ç¥ê¥²¡¼¥È¤¬²ò½ü¤µ¤ì¡¢ÉÔÍפʥǥꥲ¡¼¥È¤¬»Ä¤é¤Ê¤¤¡£
¡¡¤³¤ì¤Ë¤è¤ê¡¢¥¥ã¥ó¥»¥ë½èÍý¤Î¿®ÍêÀ¤¬¸þ¾å¤·¡¢ÉÔÍפʥǥꥲ¡¼¥È¤¬¥ª¥Ö¥¸¥§¥¯¥È¤Ë»Ä¤é¤Ê¤¤¤è¤¦¤Ë¤¹¤ë¤³¤È¤Ç¡¢¥á¥â¥ê¥ê¡¼¥¯¤äͽ´ü¤·¤Ê¤¤Æ°ºî¤òËɤ°¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡CancellationToken.Register ¥á¥½¥Ã¥É¤Ï¡¢»ØÄꤵ¤ì¤¿¥Ç¥ê¥²¡¼¥È¡Ê¥¥ã¥ó¥»¥ë¤µ¤ì¤ë¤È¤¤Ë¼Â¹Ô¤µ¤ì¤ë½èÍý¡Ë¤ò CancellationToken ¤ËÅÐÏ¿¤·¤Þ¤¹¡£
¤³¤Î¥á¥½¥Ã¥É¤Ï CancellationTokenRegistration ·¿¤Î¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤·¤Þ¤¹¡£¤³¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ï¡¢ÅÐÏ¿¤ò²ò½ü¤¹¤ë¤¿¤á¤Î IDisposable ¥¤¥ó¥¿¡¼¥Õ¥§¡¼¥¹¤ò¼ÂÁõ¤·¤Æ¤¤¤Þ¤¹¡£
¡¡using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»ÈÍѤ·¤Æ IDisposable ¥ª¥Ö¥¸¥§¥¯¥È¡Ê¤³¤³¤Ç¤Ï CancellationTokenRegistration¡Ë¤ò´ÉÍý¤¹¤ë¤È¡¢
using ¥Ö¥í¥Ã¥¯¤¬½ªÎ»¤·¤¿¥¿¥¤¥ß¥ó¥°¤Ç Dispose ¥á¥½¥Ã¥É¤¬¼«Æ°Åª¤Ë¸Æ¤Ó½Ð¤µ¤ì¡¢¥ê¥½¡¼¥¹¤¬²òÊü¤µ¤ì¤Þ¤¹¡£
¤³¤Î¾ì¹ç¡¢ÅÐÏ¿¤µ¤ì¤¿¥Ç¥ê¥²¡¼¥È¡Ê¥¥ã¥ó¥»¥ë¥Ï¥ó¥É¥é¡¼¡Ë¤Ï Dispose ¥á¥½¥Ã¥É¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¤È¤¤Ë²ò½ü¤µ¤ì¤Þ¤¹¡£
¤Ä¤Þ¤ê¡¢using ¥Ö¥í¥Ã¥¯¤òÈ´¤±¤ë¤È¥Ç¥ê¥²¡¼¥È¤¬ÇË´þ¤µ¤ì¡¢CancellationToken ¤Î¥¥ã¥ó¥»¥ë»þ¤Ë¤Ï¸Æ¤Ó½Ð¤µ¤ì¤Þ¤»¤ó¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Cysharp.Threading.Tasks; using DG.Tweening; using System.Threading; using System; using UniRx; public class BackPackInItem : MonoBehaviour { [SerializeField] private Image imgIconGauge; public ItemData itemData; public float currentCoolTime; public float currentAccuracy; public float currentAttackSpeed; public int currentMinDamage; public int currentMaxDamage; public float currentStaminaCost; public float currentStaminaSpeed; private Tweener tweener; private Subject<Unit> onCancel = new Subject<Unit>(); public IObservable<Unit> OnCancel => onCancel; public async UniTask Hoge(ItemData itemData, CancellationToken token, EntityType myEntityType) { try { // ¥¥ã¥ó¥»¥ë¤µ¤ì¤Æ¤¤¤Ê¤¤´Ö while (!token.IsCancellationRequested) { imgIconGauge.fillAmount = 0; // ¥Ð¥Õ¡¢¥Ç¥Ð¥Õ¤òŬÍѤ·¤Æ¸½ºßÃͤò»»½Ð tweener = imgIconGauge.DOFillAmount(1.0f, currentAttackSpeed).SetEase(Ease.Linear).SetLink(gameObject); // ¥¥ã¥ó¥»¥ë»þ¤Î½èÍý¤òÅÐÏ¿(¤³¤Î¥¹¥³¡¼¥×Æâ¤Ç¥¥ã¥ó¥»¥ë¤µ¤ì¤¿¾ì¹ç¤Ë¡¢¤³¤Î½èÍý¤¬¼Â¹Ô¤µ¤ì¤ë) using (token.Register(() => { tweener?.Kill(); })) { // ¹¶·â¤Þ¤Ç¤ÎÂÔµ¡»þ´Ö // ¾®¿ôÅÀ°Ê²¼¤â´Þ¤á¤¿ÀºÅÙ¤òÊݤĤ¿¤á¤Ë¡¢Å¬Àڤʥ¹¥±¡¼¥ê¥ó¥°¤ò¹Ô¤¦ int attackInterval = Mathf.CeilToInt(currentAttackSpeed * 1000); // 1000 ¤Ç¥¹¥±¡¼¥ë¤·¤ÆÀÚ¤ê¾å¤² await UniTask.Delay(attackInterval, cancellationToken: token); } // ¤É¤Á¤é¤«¤Î hp ¤¬ 0 ¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤¤« EntityType ¤ÇȽÄê // »Ä¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï²¼¤Î½èÍý¤Ø¡£»Ä¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï½ªÎ» // ¤É¤Á¤é¤«¤Î hp ¤¬ 0 ¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤¤« EntityType ¤ÇȽÄê //bool isEndBattle = BattleManager.Instance.CheckEndCondition(); //if (isEndBattle) { // break; //} // ¥¹¥¿¥ß¥Ê¤¬»Ä¤Ã¤Æ¤¤¤ë¤«È½Äê(¥¹¥¿¥ß¥Ê¥³¥¹¥È¤Î¤ß¸«¤ë) // »Ä¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ë¤Ï²¼¤Î½èÍý¤Ø¡£»Ä¤Ã¤Æ¤¤¤Ê¤¤¾ì¹ç¤Ë¤Ï½ªÎ» // ¥³¥¹¥È¾ÃÈñ // Ì¿ÃæȽÄê // currentAccuracy ¤ò 0-100 ¤ÎÈϰϤ˥¹¥±¡¼¥ê¥ó¥° float scaledAccuracy = currentAccuracy * 100f; // 0-100 ¤ÎÈϰϤΥé¥ó¥À¥à¤ÊÃͤòÀ¸À® int randomValue = UnityEngine.Random.Range(0, 100); // Ì¿ÃæȽÄê if (randomValue <= scaledAccuracy) { // Ì¿Ã椷¤¿¾ì¹ç¤Î½èÍý Debug.Log("Hit!"); // ¹¶·âÎÏÀßÄê int damage = UnityEngine.Random.Range(currentMinDamage, currentMaxDamage); // ¥À¥á¡¼¥¸½èÍý } else { // ¼ºÇÔ¤·¤¿¾ì¹ç¤Î½èÍý Debug.Log("Miss!"); } // ¥¯¡¼¥ë¥¿¥¤¥àÀßÄê int coolTimeInterval = Mathf.CeilToInt(currentAccuracy * 1000); await UniTask.Delay(coolTimeInterval, cancellationToken: token); // ¤É¤Á¤é¤«¤Î hp ¤¬ 0 ¤Ë¤Ê¤Ã¤Æ¤¤¤Ê¤¤¤« EntityType ¤ÇȽÄꤷ¡¢0 ¤Î¾ì¹ç¤Ë¤Ï break ¤ÇÈ´¤±¤ë //isEndBattle = BattleManager.Instance.CheckEndCondition(); //if (isEndBattle) { // break; //} } } catch (OperationCanceledException) { // while ʸ¤òÈ´¤±¤Æ¥¥ã¥ó¥»¥ë¤µ¤ì¤¿¾ì¹ç¤Î½èÍý tweener?.Kill(); tweener = null; // ¥¥ã¥ó¥»¥ë¤µ¤ì¤¿¤³¤È¤òÄÌÃÎ onCancel.OnNext(Unit.Default); } } }
¡¡using ([...A]) ¤Ï¡¢¤½¤Î¥Ï¥ó¥É¥é¡¼¤ò¥¹¥³¡¼¥×¤Î½ª¤ï¤ê¤Ç²ò½ü¤¹¤ë¤¿¤á¤Î½èÍý¤òµ½Ò¤¹¤ë¾ì½ê¤Ç¤¹¡£
¡¡º£²ó¡¢([...A]) ¤ÎÉôʬ¤Ë¤Ï¡¢token.Register(() => { tweener.Kill(); }) ¤¬ÅÐÏ¿¤µ¤ì¤Æ¤¤¤Þ¤¹¡£
¤è¤Ã¤Æ¡¢token ¤¬¥¥ã¥ó¥»¥ë¤µ¤ì¤¿¤È¤¤Ë¼Â¹Ô¤µ¤ì¤ë¥Ç¥ê¥²¡¼¥È(º£²ó¤Ï tweener.Kill ¤ò¼Â¹Ô¤¹¤ë¥Ç¥ê¥²¡¼¥È)¤¬ token ¤ËÅÐÏ¿¤µ¤ì¤Þ¤¹¡£
¤³¤Î¥á¥½¥Ã¥É¤Ï CancellationTokenRegistration ¥ª¥Ö¥¸¥§¥¯¥È¤òÊÖ¤·¤Þ¤¹¡£
¤³¤Î¥ª¥Ö¥¸¥§¥¯¥È¤Ï IDisposable ¤ò¼ÂÁõ¤·¤Æ¤ª¤ê¡¢Register ¥á¥½¥Ã¥É¤ÎÌá¤êÃͤȤ·¤Æ using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤ËÅϤµ¤ì»ÈÍѤµ¤ì¤Þ¤¹¡£
MicroSoft
CancellationTokenRegistration
https://learn.microsoft.com/ja-jp/dotnet/api/syste...
¡¡using ¥¹¥Æ¡¼¥È¥á¥ó¥È¥Ö¥í¥Ã¥¯Æâ¤Î¥³¡¼¥É¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£¤³¤Î¾ì¹ç¡¢await UniTask.Delay(attackInterval, cancellationToken: token); ¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
¤³¤ÎÂÔµ¡Ãæ¤Ë token ¤¬¥¥ã¥ó¥»¥ë¤µ¤ì¤ë¤È(½èÍýÃæ¤Ë¥¥ã¥ó¥»¥ë¤¬Í׵ᤵ¤ì¤¿¾ì¹ç)¡¢ÅÐÏ¿¤µ¤ì¤¿¥Ç¥ê¥²¡¼¥È¡Ê¤³¤³¤Ç¤Ï tweener.Kill()¡Ë¤¬¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
¡¡await ¤¬´°Î»¤¹¤ë¤«¡¢¥¥ã¥ó¥»¥ë¤¬È¯À¸¤·¤Æ OperationCanceledException ¤¬¥¹¥í¡¼¤µ¤ì¤ë¤È¡¢using ¥Ö¥í¥Ã¥¯¤Î½ª¤ï¤ê¤ËÅþ㤷¤Þ¤¹(¥Ö¥í¥Ã¥¯¤òÈ´¤±¤Þ¤¹)¡£
¥Ö¥í¥Ã¥¯¤òÈ´¤±¤ë¤È¡¢¤³¤³¤Ç CancellationTokenRegistration.Dispose ¤¬¼«Æ°Åª¤Ë¸Æ¤Ó½Ð¤µ¤ì¡¢IDisposable ¤¬¼«Æ°Åª¤ËÇË´þ¤µ¤ì¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢using ¥¹¥Æ¡¼¥È¥á¥ó¥È¥Ö¥í¥Ã¥¯¤Î¥¥ã¥ó¥»¥ë¥Ï¥ó¥É¥é¡¼¤ËÅÐÏ¿¤µ¤ì¤¿¥Ç¥ê¥²¡¼¥È(²¼µ¤ÎÉôʬ)¤¬²ò½ü¤µ¤ì¤Þ¤¹¡£
¡Ê°Ê¸å¤½¤Î¥È¡¼¥¯¥ó¤¬¥¥ã¥ó¥»¥ë¤µ¤ì¤Æ¤â¤³¤Î¥Ç¥ê¥²¡¼¥È¤Ï¸Æ¤Ó½Ð¤µ¤ì¤Þ¤»¤ó¡Ë¡£
token.Register(() => { tweener.Kill(); })
¡¡¤³¤ÎÉôʬ¤Î¥ê¥½¡¼¥¹¤ò²òÊü¤·¡¢»È¤ï¤ì¤Ê¤«¤Ã¤¿¥Ç¥ê¥²¡¼¥È¤ò¼«Æ°Åª¤ËÇË´þ¤·¤Æ¤¤¤Þ¤¹¡£
¡¡£±¡¥token.Register ¤Ï¥¥ã¥ó¥»¥ë¥Ï¥ó¥É¥é¡¼¤òÅÐÏ¿¤· IDisposable ¤òÊÖ¤¹¡£
¡¡£²¡¥using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤Ï¤½¤Î IDisposable ¤òÍøÍѤ·¤Æ¡¢¥¹¥³¡¼¥×½ªÎ»»þ¤Ë¼«Æ°Åª¤Ë¥ê¥½¡¼¥¹¤ò²òÊü¡Ê¤³¤³¤Ç¤Ï¥¥ã¥ó¥»¥ë¥Ï¥ó¥É¥é¡¼¤ÎÅÐÏ¿¤ò²ò½ü¡Ë¡£
¡¡£³¡¥using ¥Ö¥í¥Ã¥¯Æâ¤Ë¤Ï await UniTask.Delay ¤¬´Þ¤Þ¤ì¤Æ¤ª¤ê¡¢¥¥ã¥ó¥»¥ë¤¬È¯À¸¤¹¤ë¤È tweener.Kill() ¤¬¸Æ¤Ó½Ð¤µ¤ì¤ë¡£
¡¡
¤³¤ÎÍý²ò¤ò¤â¤È¤Ë¡¢Àµ¤·¤¯¥¥ã¥ó¥»¥ë½èÍý¤ò¹Ô¤¦¤³¤È¤¬¤Ç¤¤ë¤Ç¤·¤ç¤¦¡£
¡¡CancellationToken.Register ¥á¥½¥Ã¥É¤Ï IDisposable¡Ê¶ñÂÎŪ¤Ë¤Ï CancellationTokenRegistration¡Ë¤òÊÖ¤·¡¢¤³¤ì¤ò using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤Ç´ÉÍý¤¹¤ë¤³¤È¤Ç¡¢¥¥ã¥ó¥»¥ë¤¬Í׵ᤵ¤ì¤¿»þ¤Ë¼Â¹Ô¤µ¤ì¤ë¥Ç¥ê¥²¡¼¥È¤òÅÐÏ¿¤ª¤è¤Ó²ò½ü¤¹¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
using ¥¹¥Æ¡¼¥È¥á¥ó¥È¤òÍѤ¤¤ë¤³¤È¤Ç¡¢¥¹¥³¡¼¥×½ªÎ»»þ¤Ë¼«Æ°Åª¤Ë¥ê¥½¡¼¥¹¡Ê¤³¤³¤Ç¤Ï¥¥ã¥ó¥»¥ë¥Ï¥ó¥É¥é¡¼¡Ë¤ò²òÊü¤·¡¢¥Ç¥ê¥²¡¼¥È¤¬ÉÔÍפˤʤä¿¥¿¥¤¥ß¥ó¥°¤ÇŬÀÚ¤ËÇË´þ¤µ¤ì¤ë¤è¤¦¤Ë¤·¤Þ¤¹¡£
¤³¤ÎÍý²ò¤Ç¥³¡¼¥É¤ò¿Ê¤á¤Æ¤¤¤¿¤À¤±¤ì¤Ð¡¢Àµ¤·¤¯¥¥ã¥ó¥»¥ë½èÍý¤ò¼ÂÁõ¤Ç¤¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- ¥×¥í¥°¥é¥ß¥ó¥°
¥³¥á¥ó¥È¤ò¤«¤¯