こんにちは。ヤマヤタケシです。
Unityのコルーチンの挙動が怪しいんですよね。
StopCoroutineの挙動です。
レファレンスMonoBehaviour.StopCoroutineによると、
「Stops all coroutines named methodName running on this behaviour.」
とあり、「同じ名前のコルーチンが全て止まる」と書いてあります。
しかし、どうも StopCoroutine を1回呼ぶと1つのコルーチンだけしか止まらないみたい。
マニュアルの不備なのか、将来的に修正されるものなのか?果たして???
下記のコードは、 sキーを押すたびに StartCoroutine を行い、 dキーで StopCoroutine を行います。
#pragma strict
var sequenceNum = 0;
function Update ()
{
if( Input.GetKeyDown( "s" ) )
{
sequenceNum++;
StartCoroutine( "Sequence", sequenceNum );
}
if( Input.GetKeyDown( "d" ) )
{
StopCoroutine( "Sequence");
}
}
function Wait()
{
for( var n = 0 ; n < 30 ; ++n ){
yield;
}
}
function Sequence( no : int )
{
Debug.Log( "Start" );
for( var n = 0 ; n < 100 ; ++n )
{
Debug.Log( "" + no + " " + n );
yield Wait();
}
Debug.Log( "End" );
}
そんじゃまた。