Unity¤Ë´ØÏ¢¤¹¤ëµ­»ö¤Ç¤¹

¥³¥ë¡¼¥Á¥ó¤òÊÑ¿ô¤ËÂåÆþ¤·¤Æ¼Â¹Ô¤¹¤ë¡Ê̤¡Ë


¡¡½ñ¼°¤Ï¼ç¤Ë£²¼ïÎढ¤ê¤Þ¤¹¡£¤É¤Á¤é¤Ç¤âÌäÂê¤Ê¤¯ÊÑ¿ôÆâ¤Ë¥³¥ë¡¼¥Á¥ó¤Î¥á¥½¥Ã¥É¤¬ÂåÆþ¤µ¤ì¤Þ¤¹¡£

£±¡¥IEnumrator·¿
£²¡¥Coroutine·¿

¡¡IEnumrator·¿¤Ï¡¢IEnumrator¥¤¥ó¥¿¡¼¥Õ¥§¥¤¥¹¤ò»ÈÍѤ·¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£
¡¡Coroutine·¿¤Ï¡¢MonoBehavior.Coroutine¥¯¥é¥¹¤Î¥¤¥ó¥¹¥¿¥ó¥¹¤Î»²¾È¤ò¥ª¡¼¥Ð¡¼¥í¡¼¥É¤·¤Æ»ÈÍѤ·¤Æ¤¤¤ë¤â¤Î¤Ç¤¹¡£

£±¡¥IEnumerator¤ò»ÈÍѤ¹¤ë¾ì¹ç


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;
        }
    }
}

£²¡¥Coroutine¤ò»ÈÍѤ¹¤ë¾ì¹ç


¡¡¥³¥ë¡¼¥Á¥ó¤ÎÂåÆþ½ñ¼°¤È¼Â¹Ô½ñ¼°¤¬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¤Ç¼Â¹ÔÃæ¤Î¥³¥ë¡¼¥Á¥ó¤òÄä»ß¤¹¤ë


¡¡¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤Ë¤¢¤¿¤ê¡¢ÆÃÄê¤Î½èÍý¤¬¹Ô¤ï¤ì¤¿¾ì¹ç¤Ë¤Ï¡¢¤½¤Î¥³¥ë¡¼¥Á¥ó½èÍý¤òÄä»ß¤·¤¿¤¤¤È¤¤¤¦¾õ¶·¤¬¤¢¤ë¤È¤·¤Þ¤¹¡£
¤½¤¦¤¤¤Ã¤¿¾ì¹ç¡¢»öÁ°¤ËÊÑ¿ô¤òÍÑ°Õ¤·¤Æ¤ª¤¤¤Æ¡¢¤½¤ÎÊÑ¿ôÆâ¤Ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤òÂåÆþ¤·¡¢¼Â¹Ô¤ò¤µ¤»¤Æ¤ª¤¯¤³¤È¤Ç
StopCoroutine½èÍý¤Ë¤è¤ê¡¢Æ°¤¤¤Æ¤¤¤ë¥³¥ë¡¼¥Á¥ó½èÍý¤ò½ªÎ»¤µ¤»¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£

¡¡¤½¤Î¤Þ¤ÞºÆÅÙStartCoroutine¤ò¤¹¤ì¤Ð¡¢¥³¥ë¡¼¥Á¥óÄä»ßÁ°¤Î¾õÂÖ¤«¤éºÆ³«¤µ¤ì¤Þ¤¹¡£
¤â¤·¤â¤½¤Î¥³¥ë¡¼¥Á¥ó½èÍý¤òÅÓÃæ¤Ç¤Ï¤Ê¤¯¡¢ºÇ½é¤«¤é½èÍý¤ò¼Â¹Ô¤·¤¿¤¤¤Î¤Ç¤¢¤ì¤Ð¡¢
ºÆÅÙ¡¢¥á¥½¥Ã¥É¤òÂåÆþ¤·Ä¾¤·¤Æ¤«¤é¼Â¹Ô¤¹¤ë¤³¤È¤Ç¥á¥½¥Ã¥É¤ÎºÇ½é¤Î½èÍý¤«¤é¼Â¹Ô¤µ¤ì¤Þ¤¹¡£

¡¡¤Þ¤¿¡¢yield return StartCoroutine(¥³¥ë¡¼¥Á¥óÍѤÎÊÑ¿ô)¤ÇÌ¿Îᤷ¤Æ¤¤¤ëStartCoroutine¤â¡¢StopCoroutine¤ÇÄä»ß¤Ç¤­¤Þ¤¹¡£

£´¡¥ÊÑ¿ô¤òÍøÍѤ¹¤ë¤³¤È¤Ç¡¢ÆÃÄê¤Î¼ïÎà¤Î½èÍý¤ò¡¢¾ï¤Ë£±¤Ä¤Î½èÍý¤ËÀ©¸æ¤·¤Æ¼Â¹Ô¤¹¤ë


¡¡ÊÑ¿ô¤ò»È¤Ã¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤·¤Æ¤¤¤ë¤¿¤á¡¢¾ï¤Ë¤½¤ÎÊÑ¿ô¤ò»ÈÍѤ·¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ë¤è¤ê¡¢°Õ¿Þ¤·¤Æ¤¤¤Ê¤¤ÊÂÎó½èÍý¤¬Æ°¤¯¤³¤È¤òËɤ°¤³¤È¤â½ÐÍè¤Þ¤¹¡£

¡¡Î㤨¤Ð¡¢¤¢¤ë£±¤Ä¤Î¥¹¥Æ¡¼¥È¤Î¼Â¹Ô¤Î¤ß¤ò´üÂÔ¤µ¤ì¤Æ¤¤¤ë¤è¤¦¤Ê½èÍý¤¬Ê£¿ô¤Î¥³¥ë¡¼¥Á¥ó¤Ë¤è¤Ã¤Æ¹Ô¤ï¤ì¤ë¤è¤¦¤Ê¾ì¹ç¡¢
¥³¥ë¡¼¥Á¥óÍѤ˺î¤Ã¤¿ÊÑ¿ô¤ò¾ï¤Ë£±¤Ä¤À¤±»È¤Ã¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤·¤Æ¤¤¤¯¤³¤È¤Ç¡¢£²¤Ä°Ê¾å¤Î¥¹¥Æ¡¼¥È¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤òÇÓ½ü¤Ç¤­¤Þ¤¹¡£
¡¡
¤Ä¤Þ¤ê

¡¡¡¦¹¶·â¥¢¥¯¥·¥ç¥ó
¡¡¡¦Ëɸ楢¥¯¥·¥ç¥ó
¡¡¡¦Ãµº÷¥¢¥¯¥·¥ç¥ó
¡¡¡¦¥¹¥­¥ë¥¢¥¯¥·¥ç¥ó
¡¡
¤È¤¤¤Ã¤¿¤è¤¦¤Ê¡¢¥¢¥¯¥·¥ç¥ó·Ï¤Î½èÍý¤¬¤¹¤Ù¤Æ¥³¥ë¡¼¥Á¥ó¤Ë¤Æ¼ÂÁõ¤µ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¤¹¤Ù¤Æ¤¬ÊÂÎó½èÍý¤Ç¤­¤ë¾õÂ֤ˤʤäƤ¤¤Þ¤¹¡£
ÅöÁ³¤Ê¤¬¤é¡¢¤¢¤ë¥¢¥¯¥·¥ç¥óÃæ¤Ëͽ´ü¤·¤Ê¤¤Â¾¤Î¥¢¥¯¥·¥ç¥ó¤¬º¹¤·¹þ¤Þ¤ì¤Æ¤·¤Þ¤¦¤³¤È¤â¤¢¤êÆÀ¤Þ¤¹¡£

¡¡¤³¤Î¤è¤¦¤Ê½èÍý¤Ë¤Ä¤¤¤Æ¤Ï¡¢¤³¤ì¤é¤Î¥¢¥¯¥·¥ç¥ó·Ï¤Î½èÍý¤Ï¾ï¤Ë£±¤Ä¤ÎÊÑ¿ô¤òÄ̤¸¤Æ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤è¤¦¤ËÀ©¸æ¤¹¤ë¤³¤È¤Ç
¤¤¤º¤ì¤«£±¤Ä¤Î¥¢¥¯¥·¥ç¥ó¤Î¤ß¤·¤«¼Â¹Ô¤µ¤ì¤Ê¤¤¤è¤¦¤Ë½ÐÍè¤Þ¤¹¡££±¤Ä¤Î¥³¥ë¡¼¥Á¥ó¤¬½ªÎ»¡¢¤¢¤ë¤¤¤ÏÄä»ß¤µ¤ì¤Æ¤«¤é¡¢¼¡¤Î¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤³¤È¤Ç
Ê£¿ô¤Î¥¢¥¯¥·¥ç¥ó¤¬¼Â¹Ô¤µ¤ì¤ë¤³¤È¤òËɤ®¡¢´üÂÔ¤·¤¿µóÆ°¤Ë¤¹¤ë¤³¤È¤¬½ÐÍè¤Þ¤¹¡£

£µ¡¥whileʸ¤ò»ý¤Ä¥³¥ë¡¼¥Á¥ó¤ò»ß¤á¤ë¾ì¹ç


¡¡Ãí°ÕÅÀ¤¬¤¢¤ê¤Þ¤·¤Æ¡¢StopCoroutine¤Ë¤è¤Ã¤ÆÄä»ß¤·¤¿¥³¥ë¡¼¥Á¥ó½èÍý¤ÎÆâÉô¤Ëwhileʸ¤Ë¤è¤ë¥ë¡¼¥×½èÍý¤¬¤¢¤ë¾ì¹ç¡¢¥¿¥¤¥ß¥ó¥°¤Ë¤è¤Ã¤Æ¤Ïwhile¤Î½èÍý¤¬Æ°¤¤¤Æ¤·¤Þ¤¦¤³¤È¤¬¤¢¤ê¤Þ¤¹¡£
¤½¤Î¤¿¤á¡¢StopCoroutine¤ä¡¢¥³¥ë¡¼¥Á¥óÊÑ¿ô¤òÊ̤Υ³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤Ç¾å½ñ¤­¤·¤Æ¤¤¤¿¤ê¡¢Null¤Ë¤·¤Æ¤¤¤Æ¤âÆ°¤¤¤Æ¤·¤Þ¤¦¤è¤¦¤Ê¾ì¹ç¤Ë¤Ï
whileʸ¼«ÂΤâ»ß¤á¤ë¤è¤¦¤Ë¤¹¤ë¤È¡¢¤³¤Î¸½¾Ý¤ò²óÈò¤Ç¤­¤Þ¤¹¡£

¥³¥á¥ó¥È¤ò¤«¤¯


¡Öhttp://¡×¤ò´Þ¤àÅê¹Æ¤Ï¶Ø»ß¤µ¤ì¤Æ¤¤¤Þ¤¹¡£

ÍøÍѵ¬Ìó¤ò¤´³Îǧ¤Î¤¦¤¨¤´µ­Æþ²¼¤µ¤¤

Menu


´ðÁÃ

µ»½Ñ/Ãμ±(¼ÂÁõÎã)

3D¥¢¥¯¥·¥ç¥ó¥²¡¼¥à

2D¤ª¤Ï¤¸¤­¥²¡¼¥à(ȯŸÊÔ)

2D¶¯À©²£¥¹¥¯¥í¡¼¥ë¥¢¥¯¥·¥ç¥ó(ȯŸÊÔ)

2D¥¿¥Ã¥×¥·¥å¡¼¥Æ¥£¥ó¥°(³ÈÄ¥ÊÔ)

¥ì¡¼¥¹¥²¡¼¥à(È´¿è)

2DÊüÃÖ¥²¡¼¥à(ȯŸÊÔ)

3D¥ì¡¼¥ë¥¬¥ó¥·¥å¡¼¥Æ¥£¥ó¥°(±þÍÑÊÔ)

3Dæ½Ð¥²¡¼¥à(È´¿è)

2D¥ê¥¢¥ë¥¿¥¤¥à¥¹¥È¥é¥Æ¥¸¡¼

3D¥¿¥Ã¥×¥¢¥¯¥·¥ç¥ó(NavMeshAgent »ÈÍÑ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥«¥¨¥ë¤Î°Ù¤Ë¡Á¡¢¥Ü¥³¥¹¥«¥¦¥©¡¼¥ºÉ÷)

VideoPlayer ¥¤¥Ù¥ó¥ÈϢư¤Î¼ÂÁõÎã

VideoPlayer ¥ê¥¹¥ÈÆ⤫¤é¥à¡¼¥Ó¡¼ºÆÀ¸¤Î¼ÂÁõÎã(ȯŸ)

AR ²èÁüÉÕ¤­¥ª¥Ö¥¸¥§¥¯¥ÈÀ¸À®¤Î¼ÂÁõÎã

AR ¥ê¥¹¥ÈÆ⤫¤éÀ¸À®¤Î¼ÂÁõÎã(ȯŸ)

2D¥È¥Ã¥×¥Ó¥å¡¼¥¢¥¯¥·¥ç¥ó(¥µ¥Ð¥¤¥Ð¡¼É÷)

private



¤³¤Î¥µ¥¤¥ÈÆâ¤ÎºîÉʤϥæ¥Ë¥Æ¥£¤Á¤ã¤ó¥é¥¤¥»¥ó¥¹¾ò¹à¤Î¸µ¤ËÄ󶡤µ¤ì¤Æ¤¤¤Þ¤¹¡£

´ÉÍý¿Í/Éû´ÉÍý¿Í¤Î¤ßÊÔ½¸¤Ç¤­¤Þ¤¹