最近用Coroutine协同程序取代Update的人越来越多,故此我对Coroutine进行了一些研究,以进一步了解该函数的作用,好加快往后游戏的开发效率。
这段文章主要是对拥有一定Coroutine只是的朋友们,若还不了解Coroutine的话可以先去看看Unity3D的文档,再过来做进一步的深层了解。
首先先贴上一段代码:
public class ResearchCoroutine : MonoBehaviour
{
void Start ()
{
StartCoroutine ( CheckInputPartOne () ) ;
}
IEnumerator CheckInputPartOne ()
{
Debug.Log ( "Part One Start" ) ;
bool SwitchOffTheLoop = false ;
while ( !SwitchOffTheLoop )
{
if ( Input.GetButtonDown ( KeyCode.Space ) )
{
yield return StartCoroutine ( CheckInputPartTwo () ) ;
SwitchOffTheLoop = true ;
}
yield return null ;
}
Debug.Log ( "Part One End" ) ;
}
IEnumerator CheckInputPartTwo ()
{
Debug.Log ( "Part Two Start" ) ;
bool SwitchOffTheLoop = false ;
while ( !SwitchOffTheLoop )
{
if ( Input.GetButtonUp ( KeyCode.Space ) )
{
SwitchOffTheLoop =