废话不多说直接上代码
using UnityEngine; using System.Collections; public class CoroutineDemos : MonoBehaviour { public Transform targetts; public bool kisPress = false; public bool isMoreOnePositon = false; public bool isLessOnePositon = false; // Use this for initialization void Start () { } public void BtnKeyCheck() { StartCoroutine( KeyCheck(KeyCode.A) ); } public void CheckPos() { StartCoroutine( CheckTransform( targetts ) ); } public void CheckPosTwo() { StartCoroutine( CheckTransformTwo( targetts ) ); } IEnumerator KeyCheck(KeyCode code) { while(!kisPress) { if(Input.GetKeyUp(code) ) { StartGame(); break; } yield return null; } Debug.Log("Game is end"); } void StartGame(){ kisPress = true; print("StartGame!!!!!!!!!!!!!!!!!!!"); } IEnumerator CheckTransform(Transform t) { while(!isMoreOnePositon) { if(t.position.x <0.2 ) { NewPosition(); break; } yield return null; } Debug.Log("CheckTransform is end"); isMoreOnePositon = false; } void NewPosition() { isMoreOnePositon = true; print( targetts.position.x +"--NewPosition!!!!!!!!!!!!!!!!!!!" ); } IEnumerator CheckTransformTwo(Transform t) { while(! isLessOnePositon ) { if(t.position.x >4.9 ) { NewPositionTwo(); break; } yield return null; } Debug.Log("CheckTransformTwo is end"); isLessOnePositon = false; } void NewPositionTwo() { isLessOnePositon = true; print( targetts.position.x +"--NewPositionTwo!!!!!!!!!!!!!!!!!!!" ); } }
一个共有三个协同程序
一个用来检测用户按了键盘上的A键事件
另外两个分别检测 Cube对象 的 X坐标 大于4.9 或者 小于 0.2 的事件
分别给予三个按钮点击事件
协同程序入迷,如有错误,欢迎指正,谢谢!
时间: 2024-10-29 06:22:59