赞
踩
1、yield return null:等到下一帧继续执行
2、yield return newwaitforendofframe:这帧结束后继续执行
3、yield return newwaitforfixedupdates:等到下一物理帧后执行
4、yield return newwaitforseconds:N秒后继续执行。实际应用:定时器。当然,也可以使用update和invoke来实现定时器
5、yield return www:开启WWW的异步下载,直到下载完毕后继续执行
6、yield coroutine:等待另一个协程,实现协程的串联,如游戏的引导步骤
7、yield break:终止掉yield迭代逻辑并跳出协程
yield return www
- private string url = "http://pic14.nipic.com/20110522/7411759_164157418126_2.jpg";
- public RawImage image;
-
- void Start()
- {
- StartCoroutine(LoadImage());
- }
-
- IEnumerator LoadImage()
- {
- WWW www = new WWW(url);
- yield return www;
-
- if (www != null && string.IsNullOrEmpty(www.error))
- {
- image.texture = www.texture;
- }
- }
yield coroutine协程串联
串联几个持续性逻辑:控制角色先导航到某一处,然后做某个动作,做完动作后再导航到另一处
- NavMeshAgent agent;
- Animator anim;
-
- void Start()
- {
- agent = GetComponent<NavMeshAgent>();
- anim = GetComponent<Animator>();
-
- StartCoroutine(Work());
- }
-
- IEnumerator Work()
- {
- yield return StartCoroutine(MoveToDes(new Vector3(9, 0, 6)));
- yield return StartCoroutine(PlayAnim("Jump"));
- yield return StartCoroutine(MoveToDes(new Vector3(19, 0, 8)));
- }
-
- IEnumerator MoveToDes(Vector3 des)
- {
- agent.SetDestination(des);
- while (agent.remainingDistance > 0.01)
- {
- yield return null;
- }
- }
-
- IEnumerator PlayAnim(string name)
- {
- anim.Play(name);
- while (anim.GetCurrentAnimatorStateInfo(0).IsName(name))
- {
- yield return null;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。