ºÇ½ª¹¹¿·¡§ orika_ex_miyako 2020ǯ08·î22Æü(ÅÚ) 13:19:38ÍúÎò
¡¡½ñ¼°¤Ï¼ç¤Ë£²¼ïÎढ¤ê¤Þ¤¹¡£¤É¤Á¤é¤Ç¤âÌäÂê¤Ê¤¯ÊÑ¿ôÆâ¤Ë¥³¥ë¡¼¥Á¥ó¤Î¥á¥½¥Ã¥É¤¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£
£±¡¥IEnumrator·¿ £²¡¥Coroutine·¿
¡¡IEnumrator·¿¤Ï¡¢IEnumrator¥¤¥ó¥¿¡¼¥Õ¥§¥¤¥¹¤ò»ÈÍѤ·¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
¡¡Coroutine·¿¤Ï¡¢MonoBehavior.Coroutine¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Î»²¾È¤ò¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤Æ»ÈÍѤ·¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CoroutineTest : MonoBehaviour { // IEnumerator·¿¤ÎÀë¸À¡£³°Éô¤Î¥¯¥é¥¹¤«¤é¸Æ¤Ó¤¿¤¤¾ì¹ç¤Ë¤ÏPublic½¤¾þ»Ò¤ò¤Ä¤±¤ë private IEnumerator actionCoroutine = null; IEnumerator Start() { // ÊÑ¿ô¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ actionCoroutine = ActionFire(); // ÂåÆþ¤µ¤ì¤Æ¤¤¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤ò¼Â¹Ô yield return StartCoroutine(actionCoroutine); } public void Slash() { // ÊÑ¿ô¤ËÃ͡ʥ᥽¥Ã¥É¡Ë¤¬¤Ê¤¤¤«³Îǧ if (actionCoroutine != null) { // ¥á¥½¥Ã¥É¤¬¤¢¤ë¾ì¹ç¤Ë¤Ï¥³¥ë¡¼¥Á¥ó½èÍý¤¬Æ°¤¤¤Æ¤¤¤ë¤Î¤ÇÄä»ß¤µ¤»¤ë StopCoroutine(actionCoroutine); // ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë actionCoroutine = null; } // ÊÑ¿ô¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ actionCoroutine = ActionSlash(); // ÂåÆþ¤µ¤ì¤Æ¤¤¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤ò¼Â¹Ô StartCoroutine(ActionSlash()); } IEnumerator ActionFire() { Debug.Log("Action"); yield return new WaitForSeconds(1.0f); // ½èÍý¤¬½ªÎ»¤·¤¿¤Î¤Ç¡¢ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë if (actionCoroutine != null) { actionCoroutine = null; } } IEnumerator ActionSlash() { Debug.Log("Slash"); yield return new WaitForSeconds(1.0f); // ½èÍý¤¬½ªÎ»¤·¤¿¤Î¤Ç¡¢ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë if (actionCoroutine != null) { actionCoroutine = null; } } }
¡¡¥³¥ë¡¼¥Á¥ó¤ÎÂåÆþ½ñ¼°¤È¼Â¹Ô½ñ¼°¤¬IEnumerator·¿¤È¤Ï°Û¤Ê¤ê¤Þ¤¹¡£
using System.Collections; using System.Collections.Generic; using UnityEngine; public class CoroutineTest : MonoBehaviour { // Coroutine·¿¤ÎÀë¸À¡£³°Éô¤Î¥¯¥é¥¹¤«¤é¸Æ¤Ó¤¿¤¤¾ì¹ç¤Ë¤ÏPublic½¤¾þ»Ò¤ò¤Ä¤±¤ë private Coroutine actionCoroutine = null; void Start() { // ÊÑ¿ô¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ¤·¤Æ¼Â¹Ô actionCoroutine = StartCoroutine(ActionFire()); } public void Slash() { // ÊÑ¿ô¤ËÃ͡ʥ᥽¥Ã¥É¡Ë¤¬¤Ê¤¤¤«³Îǧ if (actionCoroutine != null) { // ¥á¥½¥Ã¥É¤¬¤¢¤ë¾ì¹ç¤Ë¤Ï¥³¥ë¡¼¥Á¥ó½èÍý¤¬Æ°¤¤¤Æ¤¤¤ë¤Î¤ÇÄä»ß¤µ¤»¤ë StopCoroutine(actionCoroutine); // ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë actionCoroutine = null; } // ÊÑ¿ô¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ¤·¤Æ¼Â¹Ô actionCoroutine = StartCoroutine(ActionSlash()); } IEnumerator ActionFire() { Debug.Log("Action"); yield return new WaitForSeconds(1.0f); // ½èÍý¤¬½ªÎ»¤·¤¿¤Î¤Ç¡¢ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë if (actionCoroutine != null) { actionCoroutine = null; } } IEnumerator ActionSlash() { Debug.Log("Slash"); yield return new WaitForSeconds(1.0f); // ½èÍý¤¬½ªÎ»¤·¤¿¤Î¤Ç¡¢ÂåÆþ¤µ¤ì¤Æ¤¤¤ëÃͤònull¤Ë¤¹¤ë¡Ê¥á¥½¥Ã¥É¤òÈ´¤¯¡Ë if (actionCoroutine != null) { actionCoroutine = null; } } }
¡¡¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤Ë¤¢¤¿¤ê¡¢ÆÃÄê¤Î½èÍý¤¬¹Ô¤ï¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥³¥ë¡¼¥Á¥ó½èÍý¤òÄä»ß¤·¤¿¤¤¤È¤¤¤¦¾õ¶·¤¬¤¢¤ë¤È¤·¤Þ¤¹¡£
¤½¤¦¤¤¤Ã¤¿¾ì¹ç¡¢»öÁ°¤ËÊÑ¿ô¤òÍÑ°Õ¤·¤Æ¤ª¤¤¤Æ¡¢¤½¤ÎÊÑ¿ôÆâ¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ¤·¡¢¼Â¹Ô¤ò¤µ¤»¤Æ¤ª¤¯¤³¤È¤Ç
StopCoroutine½èÍý¤Ë¤è¤ê¡¢Æ°¤¤¤Æ¤¤¤ë¥³¥ë¡¼¥Á¥ó½èÍý¤ò½ªÎ»¤µ¤»¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡¤½¤Î¤Þ¤ÞºÆÅÙStartCoroutine¤ò¤¹¤ì¤Ð¡¢¥³¥ë¡¼¥Á¥óÄä»ßÁ°¤Î¾õÂÖ¤«¤éºÆ³«¤µ¤ì¤Þ¤¹¡£
¤â¤·¤â¤½¤Î¥³¥ë¡¼¥Á¥ó½èÍý¤òÅÓÃæ¤Ç¤Ï¤Ê¤¯¡¢ºÇ½é¤«¤é½èÍý¤ò¼Â¹Ô¤·¤¿¤¤¤Î¤Ç¤¢¤ì¤Ð¡¢
ºÆÅÙ¡¢¥á¥½¥Ã¥É¤òÂåÆþ¤·Ä¾¤·¤Æ¤«¤é¼Â¹Ô¤¹¤ë¤³¤È¤Ç¥á¥½¥Ã¥É¤ÎºÇ½é¤Î½èÍý¤«¤é¼Â¹Ô¤µ¤ì¤Þ¤¹¡£
¡¡¤Þ¤¿¡¢yield return StartCoroutine(¥³¥ë¡¼¥Á¥óÍѤÎÊÑ¿ô)¤ÇÌ¿Îᤷ¤Æ¤¤¤ëStartCoroutine¤â¡¢StopCoroutine¤ÇÄä»ß¤Ç¤¤Þ¤¹¡£
¡¡ÊÑ¿ô¤ò»È¤Ã¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤·¤Æ¤¤¤ë¤¿¤á¡¢¾ï¤Ë¤½¤ÎÊÑ¿ô¤ò»ÈÍѤ·¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢°Õ¿Þ¤·¤Æ¤¤¤Ê¤¤ÊÂÎó½èÍý¤¬Æ°¤¯¤³¤È¤òËɤ°¤³¤È¤â½ÐÍè¤Þ¤¹¡£
¡¡Î㤨¤Ð¡¢¤¢¤ë£±¤Ä¤Î¥¹¥Æ¡¼¥È¤Î¼Â¹Ô¤Î¤ß¤ò´üÂÔ¤µ¤ì¤Æ¤¤¤ë¤è¤¦¤Ê½èÍý¤¬Ê£¿ô¤Î¥³¥ë¡¼¥Á¥ó¤Ë¤è¤Ã¤Æ¹Ô¤ï¤ì¤ë¤è¤¦¤Ê¾ì¹ç¡¢
¥³¥ë¡¼¥Á¥óÍѤ˺î¤Ã¤¿ÊÑ¿ô¤ò¾ï¤Ë£±¤Ä¤À¤±»È¤Ã¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤·¤Æ¤¤¤¯¤³¤È¤Ç¡¢£²¤Ä°Ê¾å¤Î¥¹¥Æ¡¼¥È¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤òÇÓ½ü¤Ç¤¤Þ¤¹¡£
¡¡
¤Ä¤Þ¤ê
¡¡¡¦¹¶·â¥¢¥¯¥·¥ç¥ó
¡¡¡¦Ëɸ楢¥¯¥·¥ç¥ó
¡¡¡¦Ãµº÷¥¢¥¯¥·¥ç¥ó
¡¡¡¦¥¹¥¥ë¥¢¥¯¥·¥ç¥ó
¡¡
¤È¤¤¤Ã¤¿¤è¤¦¤Ê¡¢¥¢¥¯¥·¥ç¥ó·Ï¤Î½èÍý¤¬¤¹¤Ù¤Æ¥³¥ë¡¼¥Á¥ó¤Ë¤Æ¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¤¹¤Ù¤Æ¤¬ÊÂÎó½èÍý¤Ç¤¤ë¾õÂ֤ˤʤäƤ¤¤Þ¤¹¡£
ÅöÁ³¤Ê¤¬¤é¡¢¤¢¤ë¥¢¥¯¥·¥ç¥óÃæ¤Ëͽ´ü¤·¤Ê¤¤Â¾¤Î¥¢¥¯¥·¥ç¥ó¤¬º¹¤·¹þ¤Þ¤ì¤Æ¤·¤Þ¤¦¤³¤È¤â¤¢¤êÆÀ¤Þ¤¹¡£
¡¡¤³¤Î¤è¤¦¤Ê½èÍý¤Ë¤Ä¤¤¤Æ¤Ï¡¢¤³¤ì¤é¤Î¥¢¥¯¥·¥ç¥ó·Ï¤Î½èÍý¤Ï¾ï¤Ë£±¤Ä¤ÎÊÑ¿ô¤òÄ̤¸¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤è¤¦¤ËÀ©¸æ¤¹¤ë¤³¤È¤Ç
¤¤¤º¤ì¤«£±¤Ä¤Î¥¢¥¯¥·¥ç¥ó¤Î¤ß¤·¤«¼Â¹Ô¤µ¤ì¤Ê¤¤¤è¤¦¤Ë½ÐÍè¤Þ¤¹¡££±¤Ä¤Î¥³¥ë¡¼¥Á¥ó¤¬½ªÎ»¡¢¤¢¤ë¤¤¤ÏÄä»ß¤µ¤ì¤Æ¤«¤é¡¢¼¡¤Î¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç
Ê£¿ô¤Î¥¢¥¯¥·¥ç¥ó¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤òËɤ®¡¢´üÂÔ¤·¤¿µóÆ°¤Ë¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£
¡¡Ãí°ÕÅÀ¤¬¤¢¤ê¤Þ¤·¤Æ¡¢StopCoroutine¤Ë¤è¤Ã¤ÆÄä»ß¤·¤¿¥³¥ë¡¼¥Á¥ó½èÍý¤ÎÆâÉô¤Ëwhileʸ¤Ë¤è¤ë¥ë¡¼¥×½èÍý¤¬¤¢¤ë¾ì¹ç¡¢¥¿¥¤¥ß¥ó¥°¤Ë¤è¤Ã¤Æ¤Ïwhile¤Î½èÍý¤¬Æ°¤¤¤Æ¤·¤Þ¤¦¤³¤È¤¬¤¢¤ê¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢StopCoroutine¤ä¡¢¥³¥ë¡¼¥Á¥óÊÑ¿ô¤òÊ̤Υ³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤Ç¾å½ñ¤¤·¤Æ¤¤¤¿¤ê¡¢Null¤Ë¤·¤Æ¤¤¤Æ¤âÆ°¤¤¤Æ¤·¤Þ¤¦¤è¤¦¤Ê¾ì¹ç¤Ë¤Ï
whileʸ¼«ÂΤâ»ß¤á¤ë¤è¤¦¤Ë¤¹¤ë¤È¡¢¤³¤Î¸½¾Ý¤ò²óÈò¤Ç¤¤Þ¤¹¡£
- ¥«¥Æ¥´¥ê¡§
- ¿Ê³Ø/¥¹¥¯¡¼¥ë
- Áí¹ç
¥³¥á¥ó¥È¤ò¤«¤¯