您并不总是需要保存具有返回值的函数的结果。如果你没有
public void Awake()
{
Foo();
}
private int Foo()
{
return 42;
}
编译器没有问题。
原因StartCoroutine
返回任何东西,如果你想做嵌套的协同程序,并且你想等待内部例程完成,然后继续外部。
这里是一个例子,其中房间在游戏中动态加载,一旦第一个房间被加载,玩家被允许移动,然后附加房间在之后依次加载。
private bool _allowPlayerMovement;
public Room FirstRoom;
public Room SecondRoom;
public Room ThirdRoom;
//...
public void Awake()
{
_allowPlayerMovement = false;
StartCoroutine(LoadRooms())
}
private IEnumerator LoadRooms()
{
//Runs the LoadFirstRoom Coroutine then waits for it to finish.
yield return StartCoroutine(LoadFirstRoom())
//One the first routine finishes set the variable to true.
_allowPlayerMovement = true;
//Start loading the 2nd room, wait for it to finish
yield return StartCoroutine(LoadSecondRoom())
//Once the 2nd room is finished loading start loading the 3rd room.
yield return StartCoroutine(LoadThirdRoom())
//... And so on
}
private IEnumerator LoadFirstRoom()
{
WWW www = new WWW("http:\\example.com\levels\FirstRoom.json")
yield return www;
FirstRoom = JsonUtility.FromJson<Room>(www.text);
}
//... And so on
如果你不在乎当协程“完成”就没有必要从StartCoroutine
保存结果(就像在我上面的例子中的Awake
功能)
我不能为了我的生活找出你在这里问的问题。你能给一个代码示例说明什么让你感到困惑吗? –