列舉出幾種使用Coroutine組合的結果是停止成功或停止失敗,同時也在最下方附上測試用小程式~
成功的→
StartCoroutine("Runner", "Text Run"); + StopCoroutine("Runner");
m_Runner = StartCoroutine(Runner("Love")); + StopCoroutine(m_Runner);
失敗的→
StartCoroutine(Runner("Love")); + StopCoroutine("Runner");
StartCoroutine(Runner("Love")); + StopCoroutine(Runner("Love"));
public bool IsFinished = false;
private float m_Timer = 0;
private Coroutine m_Runner = null;
void Start ()
{
m_Runner = StartCoroutine(Runner("Love"));
m_Timer = 0;
}
private void Stop()
{
StopCoroutine(m_Runner);
}
private IEnumerator Runner(string Name)
{
while (!IsFinished)
{
yield return new WaitForSeconds(1f);
Debug.Log(Name + " Running");
}
}
void Update()
{
if (m_Timer > -1)
{
m_Timer += Time.deltaTime;
if (m_Timer >= 10f)
{
Debug.LogError("Time Up!");
Stop();
m_Timer = -1;
}
}
}
}
沒有留言:
張貼留言
歡迎大家留言提問,我會答的都會盡力回答!
如果太久沒出現回應就是我又忘記回來看留言了TAT