dkymore 发布于七月 20, 2018 分享 发布于七月 20, 2018 本萌新最近想用UNITY做个游戏 可是好难啊,,, 有没有大佬教教我异步 比如异步加载 和AutoResetEvent class Program { static void Main() { Request req = new Request(); //这个人去干三件大事 Thread GetCarThread = new Thread(new ThreadStart(req.InterfaceA)); GetCarThread.Start(); Thread GetHouseThead = new Thread(new ThreadStart(req.InterfaceB)); GetHouseThead.Start(); //等待三件事都干成的喜讯通知信息 AutoResetEvent.WaitAll(req.autoEvents); //这个人就开心了。 req.InterfaceC(); System.Console.ReadKey(); } } public class Request { //建立事件数组 public AutoResetEvent[] autoEvents = null; public Request() { autoEvents = new AutoResetEvent[] { new AutoResetEvent(false), new AutoResetEvent(false) }; } public void InterfaceA() { System.Console.WriteLine("请求A接口"); Thread.Sleep(1000*2); autoEvents[0].Set(); System.Console.WriteLine("A接口完成"); } public void InterfaceB() { System.Console.WriteLine("请求B接口"); Thread.Sleep(1000 * 1); autoEvents[1].Set(); System.Console.WriteLine("B接口完成"); } public void InterfaceC() { System.Console.WriteLine("两个接口都已经请求完,正在处理C"); } } public class AsyncLoadScene : MonoBehaviour { public Slider loadingSlider; public Text loadingText; private float loadingSpeed = 1; private float targetValue; private AsyncOperation operation; // Use this for initialization void Start () { loadingSlider.value = 0.0f; if (SceneManager.GetActiveScene().name == "Loading") { //启动协程 StartCoroutine(AsyncLoading()); } } IEnumerator AsyncLoading() { operation = SceneManager.LoadSceneAsync(Globe.nextSceneName); //阻止当加载完成自动切换 operation.allowSceneActivation = false; yield return operation; } // Update is called once per frame void Update () { targetValue = operation.progress; if (operation.progress >= 0.9f) { //operation.progress的值最大为0.9 targetValue = 1.0f; } if (targetValue != loadingSlider.value) { //插值运算 loadingSlider.value = Mathf.Lerp(loadingSlider.value, targetValue, Time.deltaTime * loadingSpeed); if (Mathf.Abs(loadingSlider.value - targetValue) < 0.01f) { loadingSlider.value = targetValue; } } loadingText.text = ((int)(loadingSlider.value * 100)).ToString() + "%"; if ((int)(loadingSlider.value * 100) == 100) { //允许异步加载完毕后自动切换场景 operation.allowSceneActivation = true; } 注释 赤紅の涙 -20.00节操 不合格报道 链接到点评
推荐贴