コルーチンって便利
以前コルーチンについての記事を書きましたが
【Unity】コルーチンでよく引っかかる罠
↑の方法だと引数が2つ以上のコルーチンは止めることが出来ません。
そこで今回はUnity4.5から出来るようになった方法を紹介したいと思います。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
private IEnumerator m_Coroutine; void Start () { // コルーチン開始 m_Coroutine = Hoge ( 1, 2 ); StartCoroutine( m_Coroutine ); } void Update(){ // 右クリックしたら if( Input.GetMouseButtonDown(0) ){ // コルーチンを止める StopCoroutine( m_Coroutine ); } } // 引数が2つ以上あるコルーチン IEnumerator Hoge( int x, int y ){ int sum = 0; while( true ){ sum += ( x + y ); yield return new WaitForSeconds (0.5f); } } |
こんな感じで止めることができます。
コメント