Unity的协程使用的就是wait-until-done模式,下面放上完整代码:
using UnityEngine;
using System.Threading;
using System;delegate int MyDel(int first, int second);//声明委托类型public class AsyncWaitUntilDoneDemo : MonoBehaviour
{void Start(){MyDel del = new MyDel(Sum);Debug.Log("Before BeginInvoke");IAsyncResult iar = del.BeginInvoke(5, 7, null, null);//开始异步调用Debug.Log("After BeginInvoke");Debug.Log("Doing stuff");int result = del.EndInvoke(iar);//等待结束并获取结果Debug.Log("After EndInvoke: " + result);} static int Sum(int x,int y)//声明异步方法{Debug.Log("Inside Sum");Thread.Sleep(3000);return x + y;}
}
这是输出结果: