话不多说,直接上代码:
using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private int m_nStep = 0;
void Start()
{
StartCoroutine(RunCoroutine());
}
private IEnumerator RunCoroutine()
{
IEnumerator itor = null;
itor = C1();
yield return itor;
Debug.Log((int)itor.Current);
itor = C1();
yield return itor;
Debug.Log((int)itor.Current);
itor = C2();
yield return itor;
Debug.Log(itor.Current == null);
itor = C3();
yield return itor;
Debug.Log(itor.Current.GetType());
}
private IEnumerator C1()
{
yield return m_nStep++;
}
private IEnumerator C2()
{
yield return null;
}
private IEnumerator C3()
{
yield return new WaitForSeconds(1);
}
}
输出的结果是:
0
1
true
UnityEngine.WaitForSeconds
所以可以得出结论,yield return 可以包含返回值。
IEnumerator是个迭代器,但同时按照语法来说他本身就是个返回值类型。
public interface IEnumerator
{
object Current { get; }
bool MoveNext();
void Reset();
}
可以看到IEnumerator的定义里有Current,就是当前次迭代的yield return后面返回的值,是object类型。
尝试自定义class,基本数据类型,都是可以的。
程序学无止尽。
欢迎大家沟通,有啥不明确的,或者不对的,也可以和我私聊
我的QQ 334524067 神一般的狄狄