Unity5.6.0f3
Windows10
□
コルーチンはStartCoroutine(TestCoro())とIEnumerator TestCoro(){yield~}で動く何秒後に何するということが設定しやすい便利な奴。
bool変数はtrue/falseでオンオフを指定できる便利な奴。
これらを使って敵の行動を設定したいのだけど、合わせて使うとどうなるのか気になったので実験してみました。
UIのTextにC#スクリプトをアタッチして試します。
■コルーチンの中でfalseにしたらどうなるの?
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Test : MonoBehaviour { | |
Text text; | |
bool test = true; | |
void Start () { | |
text = GetComponent<Text>(); | |
//testがtrueの時コルーチンが動く | |
if (test) | |
{ | |
StartCoroutine(TestCoro()); | |
} | |
} | |
private IEnumerator TestCoro() | |
{ | |
text.text = "0"; | |
yield return new WaitForSeconds(1f); | |
text.text = "1"; | |
yield return new WaitForSeconds(1f); | |
text.text = "2"; | |
//コルーチンの中でtestをfalseにする | |
test = false; | |
yield return new WaitForSeconds(1f); | |
text.text = "3"; | |
yield return new WaitForSeconds(1f); | |
text.text = "4"; | |
yield return new WaitForSeconds(1f); | |
text.text = "5"; | |
} | |
} |
bool変数のtestがtrueの時、TestCoroコルーチンが動きます。
0から5までカウントするのですが、その途中でtestがfalseになったらどうなるのでしょうか?
結果はこちら!
普通に5カウントできました。
一度発動すればfalseになっても関係ないようです。
■StartCoroutineのすぐ下でfalseにすると?
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Test : MonoBehaviour { | |
Text text; | |
bool test = true; | |
void Start () { | |
text = GetComponent<Text>(); | |
//testがtrueの時コルーチンが動く | |
if (test) | |
{ | |
StartCoroutine(TestCoro()); | |
//すぐtestをfalseにする | |
test = false; | |
} | |
} | |
private IEnumerator TestCoro() | |
{ | |
text.text = "0"; | |
yield return new WaitForSeconds(1f); | |
text.text = "1"; | |
yield return new WaitForSeconds(1f); | |
text.text = "2"; | |
yield return new WaitForSeconds(1f); | |
text.text = "3"; | |
yield return new WaitForSeconds(1f); | |
text.text = "4"; | |
yield return new WaitForSeconds(1f); | |
text.text = "5"; | |
} | |
} |
今度はStartCoroutineしたすぐ後にfalseにしてみました。
予想通り何事もなくできました。
スポンサーリンク
■StartCoroutineの直前にfalseにすると?
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Test : MonoBehaviour { | |
Text text; | |
bool test = true; | |
void Start () { | |
text = GetComponent<Text>(); | |
//testがtrueの時コルーチンが動く | |
if (test) | |
{ | |
//StartCoroutineの直前にfalseにする | |
test = false; | |
StartCoroutine(TestCoro()); | |
} | |
} | |
private IEnumerator TestCoro() | |
{ | |
text.text = "0"; | |
yield return new WaitForSeconds(1f); | |
text.text = "1"; | |
yield return new WaitForSeconds(1f); | |
text.text = "2"; | |
yield return new WaitForSeconds(1f); | |
text.text = "3"; | |
yield return new WaitForSeconds(1f); | |
text.text = "4"; | |
yield return new WaitForSeconds(1f); | |
text.text = "5"; | |
} | |
} |
今度は直前にfalseにします。
さすがにこれではコルーチンは動かないでしょう。
おおっと、これは意外!
testがtrueの場合、TestCoroコルーチンが動く仕組みでそれをスタートさせる前にtestをfalseにしてもTestCoroは動く!!
■int,float,bool,stringの場合は?
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.UI; | |
public class Test : MonoBehaviour { | |
Text text; | |
bool test = true; | |
public int num = 0; | |
public float num2 = 0; | |
public bool test2 = false; | |
public string test3 = "aaa"; | |
void Start () { | |
text = GetComponent<Text>(); | |
if (test) | |
{ | |
test = false; | |
text.text = "100"; | |
num = 123; | |
num2 = 0.456f; | |
test2 = true; | |
test3 = "bbb"; | |
} | |
} | |
} |
testがtrueの場合、Text,int,float,bool,stringの値が変わるのですが、その直前にtestをfalseにしたらどうなるのか確かめてみました。
やはりこれらもtestのif文の中で先にtestをfalseにしても問題なく値は変わるのですね。
□
今回何気なく確かめてみたのですが良い勉強になりました。
この調子でもっと気になったところをたしかめて行くことにします。
さて、今回はこれで終わりです。
お疲れさまでした。
■関連記事
ブログタイトルを「Unityで魔物使いゲームを作る」に変更しました
スポンサーリンク