// class TestCortouine
public class TestCoroutine : MonoBehaviour {
public IEnumerator CoLog ( string _name ) {
Debug.Log(_name + " hello foobar 01");
yield return new WaitForSeconds (2.0f);
Debug.Log(_name + " hello foobar 02");
}
}
// component attached on GameObject A
public class A: MonoBehaviour {
public GameObject B;
void Start () {
TestCoroutine compB = B.GetComponentTestCoroutine();
// GOOD, thread state in B
// same as: compB.StartCoroutine ( "CoLog", "B" );
compB.StartCoroutine ( compB.CoLog("B") );
// BAD, thread state in A
StartCoroutine ( compB.CoLog("A") );
Debug.Log("Bye bye A, we‘ll miss you");
Destroy(gameObject); // T_T I don‘t want to die...
}
}
以上代码,得到的结果将会是:
B hello foobar 01
A hello foobar 01
Bye bye A, we‘ll miss you
B hello foobar 02