From ffbadbcc4c94708833e3d7bfe188e7dee76bdfe9 Mon Sep 17 00:00:00 2001 From: hadashiA Date: Fri, 8 Sep 2023 18:00:39 +0900 Subject: [PATCH] Update README about WaitForEndOfFrame --- README.md | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ff1bf63..5f4374d 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,13 @@ async UniTask DemoAsync() await UniTask.Yield(); await UniTask.NextFrame(); - // replacement of WaitForEndOfFrame(requires MonoBehaviour(CoroutineRunner)) + // replacement of WaitForEndOfFrame +#if UNITY_2023_1_OR_NEWER + await UniTask.WaitForEndOfFrame(); +#else + // requires MonoBehaviour(CoroutineRunner)) await UniTask.WaitForEndOfFrame(this); // this is MonoBehaviour +#endif // replacement of yield return new WaitForFixedUpdate(same as UniTask.Yield(PlayerLoopTiming.FixedUpdate)) await UniTask.WaitForFixedUpdate(); @@ -499,6 +504,8 @@ It indicates when to run, you can check [PlayerLoopList.md](https://gist.github. `PlayerLoopTiming.Update` is similar to `yield return null` in a coroutine, but it is called before Update(Update and uGUI events(button.onClick, etc...) are called on `ScriptRunBehaviourUpdate`, yield return null is called on `ScriptRunDelayedDynamicFrameRate`). `PlayerLoopTiming.FixedUpdate` is similar to `WaitForFixedUpdate`. > `PlayerLoopTiming.LastPostLateUpdate` is not equivalent to coroutine's `yield return new WaitForEndOfFrame()`. Coroutine's WaitForEndOfFrame seems to run after the PlayerLoop is done. Some methods that require coroutine's end of frame(`Texture2D.ReadPixels`, `ScreenCapture.CaptureScreenshotAsTexture`, `CommandBuffer`, etc) do not work correctly when replaced with async/await. In these cases, pass MonoBehaviour(coroutine runnner) to `UniTask.WaitForEndOfFrame`. For example, `await UniTask.WaitForEndOfFrame(this);` is lightweight allocation free alternative of `yield return new WaitForEndOfFrame()`. +> +> Note: In Unity 2023.1 or newer, `await UniTask.WaitForEndOfFrame();` no longer requires MonoBehaviour. It uses `UnityEngine.Awaitable.EndOfFrameAsync`. `yield return null` and `UniTask.Yield` are similar but different. `yield return null` always returns next frame but `UniTask.Yield` returns next called. That is, call `UniTask.Yield(PlayerLoopTiming.Update)` on `PreUpdate`, it returns same frame. `UniTask.NextFrame()` guarantees return next frame, you can expect this to behave exactly the same as `yield return null`.