Unity插件 Unitask學習日志
下載地址
https://github.com/Cysharp/UniTask
點擊這里可以查閱中文文檔
在Unity 2020,2021 中使用UPM下載會找不到,可以使用2022版本的unity可以在upm中找到。
安裝方式:
下載zip之后解壓,
復制
Plugins
到Unity項目中即可。
Content
-
使用Unitask 的最低版本是:Unity 2018.4.13f1.
_ = DelayFrame_Test(100);
中的_=
在 丟棄 符號,不是沒有意義的。用于忽略返回值的語法,通常用于那些不需要處理返回值的方法調用,但仍然希望調用該方法。 -
僅僅記錄了github中文檔的一小部分,文檔的后面有“錯誤處理”,“超時處理”,“進度”,“線程切換” 等。詳情請查閱github文檔
-
寫法是:
private async UniTask 方法名稱{
await 操作
}private async UniTask<T> 方法名稱{
await 操作
retun 返回值
}
全部代碼記錄
using Cysharp.Threading.Tasks;
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;public class UnitaskTest : MonoBehaviour
{public RotateCube goRotateCube;void Start(){Debug.Log("進入start方法");UniTask<string> ssst = DemoAsync();_ = DelayFrame_Test(100);_ = SendGetHttpRequest();_ = WaitUnityPlayerLoop();_ = ReplaceYield();_ = WaitForEndOfFrame();_ = WaitUntilCondition();_ = WaitUntilValueChangedCondition();_ = FooCoroutineEnumerator();Debug.Log("start方法結束");}private void Update(){if (Input.GetKeyDown(KeyCode.O)){goRotateCube.enabled = false;}if (Input.GetKeyDown(KeyCode.I)){goRotateCube.gameObject.transform.localPosition = new Vector3(1, 0, 0);}}private async UniTask<string> DemoAsync(){Debug.Log("Unitask進入 異步");var asset = await Resources.LoadAsync<TextAsset>("fulman");for (int i = 0; i < 500; i++){GameObject.CreatePrimitive(PrimitiveType.Capsule);}Debug.Log("Unitask異步讀取文件,創建物體結束");return (asset as TextAsset)?.text ?? throw new InvalidOperationException("Asset not found");}/// <summary>/// 等待100幀/// </summary>/// <returns></returns>private async UniTask DelayFrame_Test(int frameCount){Debug.Log("等待100幀開始" + Time.frameCount);// 等待一個基于幀的延時操作(就像一個協程一樣)await UniTask.DelayFrame(frameCount);Debug.Log("等待100幀結束" + Time.frameCount);}/// <summary>/// 異步發送http的get請求/// </summary>/// <returns></returns>private async UniTask<string> SendGetHttpRequest(){Debug.Log("異步發送http的get請求開始");string webText =(await UnityWebRequest.Get("https://github.com/Cysharp/UniTask/blob/master/README_CN.md").SendWebRequest()).downloadHandler.text;Debug.Log("異步發送http的get請求結束" + webText);return webText;}/// <summary>/// 等待一個Unity的生命周期/// </summary>/// <returns></returns>private async UniTask WaitUnityPlayerLoop(){Debug.Log("異步開始等待Unity的一個生命周期,LastInitialization");await UniTask.Yield(PlayerLoopTiming.LastInitialization);Debug.Log("異步結束等待Unity的一個生命周期,LastInitialization");var go = GameObject.CreatePrimitive(PrimitiveType.Capsule);go.transform.position = new Vector3(3, 0, 3);}/// <summary>/// Yield的替代,/// </summary>/// <returns></returns>private async UniTask ReplaceYield(){Debug.Log("UniTask.Yield當前幀數是start:" + Time.frameCount);//等下一幀執行await UniTask.Yield();Debug.Log("UniTask.Yield之后的幀數是end:" + Time.frameCount);Debug.Log("第二個開始");Debug.Log("NextFrame當前幀數是start:" + Time.frameCount);//等下一幀執行await UniTask.NextFrame();Debug.Log("NextFrame等待一幀之后end:" + Time.frameCount);}/// <summary>/// 等待這幀結束后進行操作/// </summary>/// <returns></returns>private async UniTask WaitForEndOfFrame(){Debug.Log("幀開始:" + Time.frameCount);await UniTask.WaitForEndOfFrame(this);Debug.Log("幀結束 :" + Time.frameCount);}/// <summary>/// 某個條件滿足的時候觸發/// </summary>/// <returns></returns>private async UniTask WaitUntilCondition(){Debug.Log("方塊還在轉圈");// yield return WaitUntil 替代方案//等待滿足于某個條件的時候執行await UniTask.WaitUntil(() => goRotateCube.enabled == false);Debug.Log("方塊不轉圈啦!!!");}/// <summary>/// 當某個值改變的時候觸發/// </summary>/// <returns></returns>private async UniTask WaitUntilValueChangedCondition(){Debug.Log("監聽goRotateCube 是否改變位置");//開始監聽goRotateCube 的X是否 改變位置await UniTask.WaitUntilValueChanged(goRotateCube, gorotatecube => goRotateCube.transform.localPosition.x);Debug.Log("監聽到goRotateCube.transform.localPosition 位置改變");goRotateCube.gameObject.transform.localScale = new Vector3(2, 2, 2);}/// <summary>/// 可以等待一個協程/// </summary>/// <returns></returns>private async UniTask FooCoroutineEnumerator(){await SayHello100Times();Debug.Log("100次hello man說完啦!");}/// <summary>/// 說100次你好/// </summary>/// <returns></returns>private IEnumerator SayHello100Times(){yield return new WaitForSeconds(2);for (int i = 0; i < 100; i++){Debug.Log("hello man");}}//可以等待一個task runprivate async void TaskRunTest(){int resule = await Task.Run(GetRandomNumber);}/// <summary>/// 獲取一個隨機數/// </summary>/// <returns></returns>private int GetRandomNumber(){int ran = UnityEngine.Random.Range(0, 100990);return ran;}
}
RotateCube 的代碼,掛載于方塊上,
using UnityEngine;public class RotateCube : MonoBehaviour
{void Update(){// 繞Y軸旋轉transform.Rotate(Vector3.up, 50 * Time.deltaTime);}
}