¡¡¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ë¤Ë¤Ï¡¢
StartCoroutine ¥á¥½¥Ã¥É¤ò»ÈÍѤ·¡¢°ú¿ôÆâ¤Ë¼Â¹Ô¤·¤¿¤¤¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤ò»ØÄꤷ¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤¹¤ëÎã¤Ç¤¹¡£
public class Example : MonoBehaviour
{
IEnumerator MyCoroutine()
{
yield return new WaitForSeconds(2.0f);
}
void Start()
{
// ¥³¥ë¡¼¥Á¥ó¤Î¼Â¹Ô
StartCoroutine(MyCoroutine());
}
}
¡¡¤³¤Î¥³¡¼¥É¤Ç¤Ï¡¢StartCoroutine ¥á¥½¥Ã¥É¤ò»ÈÍѤ·¤Æ MyCoroutine ¥³¥ë¡¼¥Á¥ó¤ò¼Â¹Ô¤·¤Æ¤¤¤Þ¤¹¡£
¥³¥ë¡¼¥Á¥óÆâ¤Î yield return ¥¹¥Æ¡¼¥È¥á¥ó¥È¤Ë¤è¤Ã¤Æ¡¢»ØÄꤷ¤¿ÂÔµ¡»þ´Ö¤¬·Ð²á¤·¤¿¸å¤Ë¥³¥ë¡¼¥Á¥ó¤¬ºÆ³«¤µ¤ì¤Þ¤¹¡£
¡¡¥³¥ë¡¼¥Á¥óÆâ¤ÇÂÔµ¡¤¹¤ë¤¿¤á¤Ë¤Ï¡¢yield return ¥¹¥Æ¡¼¥È¥á¥ó¥È¤ò»È¤Ã¤Æ¡¢WaitForSeconds ¤ä¾¤ÎÂÔµ¡Ì¿Îá¤ò»ØÄꤷ¤Þ¤¹¡£
¤³¤ì¤Ë¤è¤ê¡¢¥²¡¼¥àÆâ¤Ç»þ´Ö¤Ë´ð¤Å¤¤¤¿ÈóƱ´ü½èÍý¤ä¥¢¥Ë¥á¡¼¥·¥ç¥óÀ©¸æ¤Ê¤É¤¬²Äǽ¤Ë¤Ê¤ê¤Þ¤¹¡£
¡¡¼Â¹Ô¤¹¤ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤Ë°ú¿ô¤¬¤¢¤ë¾ì¹ç¤Ë¤Ï¡¢Ä̾ï¤Î¥á¥½¥Ã¥É¤ÈƱÍͤ˰ú¿ô¤ò»ØÄꤹ¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
¡¡°Ê²¼¤Ï¡¢°ú¿ô¤òȼ¤¦¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤ò¼Â¹Ô¤¹¤ë¥µ¥ó¥×¥ë¥³¡¼¥É¤Ç¤¹¡£
using System.Collections;
using UnityEngine;
public class Example : MonoBehaviour
{
// °ú¿ô¤ò¼õ¤±¼è¤ë¥³¥ë¡¼¥Á¥ó¥á¥½¥Ã¥É¤ÎÄêµÁ
IEnumerator MyCoroutine(int count)
{
for (int i = 0; i < count; i++)
{
// ²¿¤«¤Î½èÍý
Debug.Log("Step " + i);
// ¤³¤³¤ÇÂÔµ¡
yield return new WaitForSeconds(1.0f);
}
}
void Start()
{
// °ú¿ô¤ò»ØÄꤷ¤Æ¥³¥ë¡¼¥Á¥ó¤Î¼Â¹Ô
int numberOfSteps = 3; // Îã: 3²ó·«¤êÊÖ¤¹
StartCoroutine(MyCoroutine(numberOfSteps));
}
}
¡¡¤³¤Î½èÍý¤ò¼Â¹Ô¤¹¤ë¤È¡¢1É䪤¤Ë Step 0¡¢Step 1¡¢Step 2 ¤Èɽ¼¨¤µ¤ì¤Æ¤¤¤¤Þ¤¹¡£