ãUnityãCoroutine(ã³ã«ã¼ãã³)ã§ä¸å®æéå¦çãå¾ ã¤ï¼ãã²ããã¨ãã»ã³ã¹ã
Unity5.0.0f4 Personal(2015å¹´5æ)
Â
Â
ã²ããã¨ãã»ã³ã¹ã®æéã§ãã
ä»åã¯ä¸å®æéå¦çãå¾ ã¤ã³ã«ã¼ãã³ã«ã¤ãã¦è§£èª¬ãã¦ããã¾ãã
Â
ã³ã«ã¼ãã³ã§ä¸å®æéå¦çãå¾ ã¤
Â
â Startã¡ã½ããã§ã³ã«ã¼ãã³
ã¾ãã¯Startã¡ã½ããã§ã³ã«ã¼ãã³ã使ã£ã¦ã¿ã¾ãããããã®åã«ãã³ã«ã¼ãã³ã使ã£ã¦çæããCubeã®Prefabãå ã«ä½ã£ã¦ããã¾ãããã
Prefabã®ä½ãæ¹ã¯ä»¥ä¸ã¨ãã»ã³ã¹ããåç §ä¸ããã
Â
Â
ç¶ãã¦CubePrefabã«moveScriptãåãä»ãã¾ããããã¯ãCubeããã å³ããå·¦ã¸æµãã¦ããã ãã®ã¹ã¯ãªããã§ãã
Â
Â
ç¶ãã¦CubeãçæããFieldManagerã¨ããGameObjectãä½æãã¾ããxã®ãã¸ã·ã§ã³ã ã10ã«ãã¦ããã¦ä¸ããã
Â
CreateEmptyã§ç©ºã²ã¼ã ãªãã¸ã§ã¯ãä½æ
Â
position.xã ã10ã«ãã¦ãã
Â
ããã§ã¯ãã®FieldManagerãªãã¸ã§ã¯ãã«coroutineScriptã¨ãããã³ã«ã¼ãã³ã使ã£ãCubeçæã¹ã¯ãªãããåãä»ãã¦ããã¾ãã以ä¸ã½ã¼ã¹ã§ãï¼
Â
Â
ã¾ãã¯publicå¤æ°objã«å ã»ã©ä½æããCubePrefabãæå®ãã¾ãã
Â
ãã©ãã°ï¼ããããï¼
Â
ã³ã«ã¼ãã³ã¯æ»ãå¤ãIEnumeratorã¨ãããã¨ã§å®è£ ãããã¨ãã§ãã¾ãã
Â
Startã¡ã½ããã®æ»ãå¤ãIEnumeratorã«ããStartã¡ã½ããã®ä¸ãç¡éã«ã¼ããã¦ãã¾ãã
Â
ç¡éã«ã¼ãã®ä¸ã®å¦çã¯ãInstantiateã§CubeãçæããWaitForSecondsã¡ã½ããã«ãã£ã¦1ç§å¾ æ©ããå¾ æ©å¾ã«ç¡éã«ã¼ãã®å é ã«æ»ã£ã¦ãã¾ããããã«ãã1ç§ééã§Cubeãçæããå¦çãå®ç¾ãã¦ãã¾ãã
Â
1ç§ã«1ã¤Cubeãä½ããå·¦ããå³ã¸æµã
Â
WaitForSecondsã¡ã½ãããå®è£ ã§ããã®ã¯ã³ã«ã¼ãã³å ã®ã¿ãã¤ã¾ãæ»ãå¤ãIEnumeratorã§ããã¡ã½ããå ã§ã®ã¿ãªã®ã§ãã注æä¸ããã
Â
ã¾ããééã®ç©ºãæ¹ã«ã¯
Â
ã³ã«ã¼ãã³å ã§1ãã¬ã¼ã ã ãå¾ ã¤ï¼yield return null;
ã³ã«ã¼ãã³å ã§ã³ã«ã¼ãã³ãçµäºããï¼yield break;
Â
ã¨ããæ¹æ³ãããã¾ãã®ã§ããæ´»ç¨ä¸ããã
Â
â¡å¥ã®ã¡ã½ããããã³ã«ã¼ãã³å¼åº
å¤é¨ããã³ã«ã¼ãã³ãå¼ã³åºããã¨ãã§ãã¾ããã¨ãããããã£ã¡ã®ã»ããå¤ããã¨æãã¾ãã
Â
ããã§ã¯å ã»ã©ã®coroutineScriptãä¿®æ£ãã¾ãããã以ä¸ã½ã¼ã¹ã§ãï¼
Â
Â
Startã¡ã½ããå ã§StartCoroutineã¨ããã¡ã½ãããå®è¡ãã¦ãã¾ããå¼æ°ã«æåå"CreateCube"ãæå®ãã¦ä½¿ããã¨ã§ãCreateCubeã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
ããã²ã¨ã¤å¥ãªå½¢ã¨ãã¦ãã¡ã½ããããã®ã¾ã¾æå®ããæ¹æ³ãããã¾ãã
Â
ã©ã¡ãã«ããã³ã«ã¼ãã³ãã¤ã¾ãIEnumeratorã¨ããæ»ãå¤ãããã¡ã½ããã¯ãStartCoroutineã§å¼ã³åºãå¿ è¦ããããã¨ãè¦ãã¦ããã¦ä¸ããã
Â
CreateCubeã¡ã½ããå ã¯ãå ã»ã©ã®Startã¡ã½ããå ã¨åãã§ãã
Â
â¢ã³ã«ã¼ãã³ã«å¼æ°ãæå®ãã
ã³ã«ã¼ãã³ã«å¼æ°ãæå®ãã¦ä½¿ãããå ´åãStartCoroutineã®å¼æ°ãæååã§ã¯ãªãã¡ã½ããã§æå®ããããããã¯("ã¡ã½ãã", å¼æ°)ã®å½¢ã§æå®ããå¿ è¦ãããã¾ãã以ä¸ã«ä¾ãããã¾ãã
Â
Â
å¼æ°ãæåå"CreateCube"ã§ã¯ãªããã¡ã½ããCreateCube( )ãæå®ãã¦ãå¼æ°3.0fãæå®ãã¦ãã¾ãã
ãã®å¼æ°ãåãåãposition.yã0ã3ã®ã©ã³ãã ã¨ããçµæã¯ã»ã»ã»
Â
Â
ãã¡ãã¨å¼æ°ãåãåããã©ã³ãã ã§Cubeãçæãã¦ãã¾ããã©ã³ãã ãªè¶³å ´ãä½æãããæçã«ä½¿ãããã§ããã
Â
â£ã³ã«ã¼ãã³ãæ¢ãã
StartCoroutineã¡ã½ããã¨ã»ããã§è¦ãã¦ã»ããã®ãStopCoroutineã§ãããã®åã®éããã³ã«ã¼ãã³å¦çãéä¸ã§ã¹ãããããããã¨ãã§ãã¾ãã
Â
ãã®StopCoroutineã使ãéã«æ³¨æãã¦é ãããã®ã¯ãæååæå®ã§å¼ã³åºãããã³ã«ã¼ãã³ã«å¯¾ãã¦ããæ©è½ããªãã¨ãããã¨ã§ããä¾ãããã¦ã¿ã¾ãã
Â
Â
Startã¡ã½ããã«ã¦æåå"CreateCube"ãå¼æ°ã«æå®ããStartCoroutineãå®è¡ããCubeã1ç§ééã§çæãã¦ãã¾ããããã¦ãUpdateã¡ã½ããã«ã¦ã¹ãã¼ã¹ãã¼ãæ¼ããæã«StopCoroutineãæ©è½ããã³ã«ã¼ãã³ãã¹ãããããããã¨ãã§ãã¾ãã
Â
æååæå®ã§å¼ã³åºãããã³ã«ã¼ãã³ã«å¯¾ãã¦ããæ©è½ããªãã®ã§ãå¼æ°ä»ãã®ã³ã«ã¼ãã³ã«å¯¾ãã¦StopCoroutineã使ãããæã¯ãStartCoroutine(CreateCube( å¼æ° ))ã§ã¯ãªããStartCoroutine("CreateCube", å¼æ°)ã®å½¢ã使ã£ã¦ä¸ããã
Â
â¤è¤æ°ã®å¼æ°ãæã¤ã³ã«ã¼ãã³ãã¹ãããããã
 StartCoroutineã§å¼ã³ã ãã¡ã½ããã®å¼æ°ã¯ãã¡ã½ããåãæå®ããã°ãã¡ããè¤æ°æå®ãããã¨ãã§ãã¾ããä¾ãã°ã StartCoroutine(CreateCube(-3.0f, 3.0f)) ã¨ããå½¢ã§ãã
Â
ããããã®å½¢ã ã¨å¼æ°ã«ã¡ã½ããã使ã£ã¦ãããããStopCoroutineã§æ¢ãããã¨ãã§ãã¾ãããããããæã®åé¿æ¹æ³ã以ä¸ã«è¼ãã¾ãã
Â
Â
ã¾ãã¯IEnumeratoråã®å¤æ°createCubeãä½ã£ã¦ãããStartã¡ã½ããã«ã¦CreateCubeã¡ã½ãããå¼æ°ä»ãã§æå®ãã¾ãã
Â
ãã®å¾ãå ã»ã©æå®ããå¤æ°ãStartCoroutineã®å¼æ°ã«ãã¦ãããè¤æ°ã®å¤æ°ã使ãã³ã«ã¼ãã³ãå®è¡ãã¦ãã¾ãã
Â
æå®ããã®ã¯ã¡ã½ããåã§ã¯ãªãå¤æ°åãªã®ã§ãStopCoroutineããã¡ãã¨æ©è½ããã¨ããããã§ãã
Â
â¥å ¨ã¦ã®ã³ã«ã¼ãã³ãåæã«æ¢ãã
æå¾ã«ãªãã¾ããã
Â
StopAllCoroutines( );
Â
ã¨ããã¡ã½ããã使ãã¨ãå®è¡ä¸ã®ã³ã«ã¼ãã³ãå ¨ã¦æ¢ãããã¨ãã§ãã¾ãã
Â
以ä¸ãã²ããã¨ãã»ã³ã¹ã§ããã
ãããã¨ããããã¾ããã