协程的简单用法
函数
- 按常用排序
- WaitForSeconds
- WaitForEndOfFrame
- WaitForFixedUpdate
- WaitForSecondsRealtime
- WaitUnil
- WaitWhile
StartCoroutine(WaitUntilFunc());
StartCoroutine(WaitForSecondsFunc());
StartCoroutine(WaitForSecondsRealtimeFunc());
StartCoroutine(WaitWhileFunc());
StartCoroutine(WaitForEndOfFrameFunc());
StartCoroutine(WaitForStartCoroutine());
IEnumerator WaitUntilFunc()
{
Debug.Log("WaitUntilFunc Start~");
yield return new WaitUntil(() => _frame> 10);
Debug.Log("WaitUntilFunc End~");
}
IEnumerator WaitForSecondsFunc()
{
Debug.Log("WaitForSecondsFunc Start~");
yield return new WaitForSeconds(2f);
Debug.Log("WaitForSecondsFunc End~");
}
IEnumerator WaitForSecondsRealtimeFunc()
{
Debug.Log("WaitForSecondsRealtimeFuncStart~");
yield return new WaitForSecondsRealtime(2f);
Debug.Log("WaitForSecondsRealtimeFuncEnd~");
}
IEnumerator WaitWhileFunc()
{
Debug.Log("WaitWhileFunc Start~");
yield return new WaitWhile(()=> !(_frame>10));
Debug.Log("WaitWhileFunc End~");
}
IEnumerator WaitForEndOfFrameFunc()
{
Debug.Log("WaitForEndOfFrameFunc Start~");
yield return new WaitForEndOfFrame();
Debug.Log("WaitForEndOfFrameFunc End~");
}
IEnumerator WaitForAsyncOperation()
{
Debug.Log("WaitForAsyncOperation Start~");
AsyncOperation async = null;
async = SceneManager.LoadSceneAsync("SampleScene");
async = AssetBundle.LoadFromFileAsync("Path");
async = Resources.LoadAsync("Path");
var request = UnityWebRequest.Get(new Uri("www.baidu.com"));
async = request.SendWebRequest();
yield return async;
Debug.Log("WaitForAsyncOperation End~");
}
IEnumerator WaitForStartCoroutine()
{
Debug.Log("WaitForStartCoroutine Start~");
yield return StartCoroutine(WaitForSecondsFunc());
Debug.Log("WaitForStartCoroutine End~");
}
协程的执行顺序
官网更加详细点这里哟 执行顺序
- FixedUpdate
- yield WaitForFixedUpdate
- Update
- yield null
- yield WaitForSeconds
- yield WWW
- yield StartCoroutine
- LateUpdate
- yield WaitForEndOfFrame
Yleid Return是什么
简述 yield return 用来判断协程内部是否挂起或继续执行的语句 支持类型
- IEnumerator
- CustomYieldInstruction
- YieldInstruction
- AsyncOperation
自定义一个迭代器
就不一一展示用法了,代码太长,需要自己去尝试
private void Start()
{
StartCoroutine(TestCor());
}
IEnumerator TestCor()
{
Debug.Log("TestCor Start~");
yield return new CustomIEnumerator();
yield return new Custom2IEnumerator();
yield return new Custom3IEnumerator();
yield return new Custom4IEnumerator();
Debug.Log("TestCor End~");
}
}
public class CustomIEnumerator : IEnumerator
{
private int _index = -1;
private string[] strName = {"Alan", "Kino", "Bruce"};
public bool MoveNext()
{
_index++;
Debug.Log("MoveNext:" + (_index > strName.Length).ToString());
return false;
return _index < strName.Length;
}
public void Reset()
{
Debug.Log("Reset");
_index = 0;
}
public object Current {
get
{
Debug.Log("获取当前对象:" + strName[_index]);
return strName[_index];
}
}
}
public class Custom2IEnumerator : CustomYieldInstruction
{
public override bool keepWaiting { get; }
}
public class Custom3IEnumerator : YieldInstruction
{
}
public class Custom4IEnumerator : AsyncOperation
{
}
优化
- 有些不需要每帧进行执行的任务可以使用协程延迟
官网案例
IEnumerator DoCheck()
{
for(;;)
{
ProximityCheck();
yield return new WaitForSeconds(.1f);
}
}
private WaitForSeconds delay = new WaitForSeconds(.1f);
IEnumerator TestCor()
{
for(;;)
{
yield return delay;
}
}
|