用携程来启动一个监听程序

1.启动携程 监听空格键 一旦空格键按下 就运行程序 然后携程结束

 1 void Start()
 2 {
 3 StartCoroutine(测试携程());
 4 
 5 }
 6 
 7  
 8 
 9 IEnumerator 测试携程()
10 {
11 Debug.Log("携程被激活了");
12 
13 while(!Input.GetKeyDown(KeyCode.Space)) //监听到一个事件 就结束携程
14 yield return null;
15 
16 Debug.Log("携程响应了");
17 
18 }

2.启动携程 监听空格键 一旦空格键按下 就运行程序 携程不关闭 继续监听

 1 void Start()
 2 {
 3 StartCoroutine(测试携程());
 4 
 5 }
 6 
 7  
 8 
 9 IEnumerator 测试携程()
10 {
11 Debug.Log("携程被激活了");
12 
13  
14 
15 while (true)
16 {
17 if (Input.GetKeyDown(KeyCode.Space))
18 {
19 Debug.Log("携程响应了");
20 }
21 yield return null;
22 }
23 
24  
25 
26 }
原文地址:https://www.cnblogs.com/vsdog/p/6218965.html