-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from mewlist/refactoring
Refactoring
- Loading branch information
Showing
11 changed files
with
120 additions
and
62 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System.Threading.Tasks; | ||
using Mew.Core.TaskHelpers; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace Mew.Core.Assets | ||
{ | ||
internal static class CompatibleSceneLoader | ||
{ | ||
internal static async ValueTask<SceneHandle> LoadSceneAsync(UnifiedScene unifiedScene, LoadSceneParameters parameters) | ||
{ | ||
#if UNITY_2023_2_OR_NEWER | ||
await SceneManager.LoadSceneAsync(unifiedScene.SceneReference, parameters); | ||
#else | ||
var asyncOp = SceneManager.LoadSceneAsync(unifiedScene.SceneReference, parameters); | ||
while (!asyncOp.isDone) await TaskHelper.NextFrame(); | ||
#endif | ||
var loadedScene = SceneManager.GetSceneAt(SceneManager.loadedSceneCount - 1); | ||
return new SceneHandle(loadedScene); | ||
} | ||
|
||
internal static async ValueTask UnloadSceneAsync(SceneHandle sceneHandle) | ||
{ | ||
#if UNITY_2023_2_OR_NEWER | ||
await SceneManager.UnloadSceneAsync(sceneHandle.Scene); | ||
#else | ||
var asyncOp = SceneManager.UnloadSceneAsync(sceneHandle.Scene); | ||
while (!asyncOp.isDone) await TaskHelper.NextFrame(); | ||
#endif | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#if UNITY_2023_2_OR_NEWER | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using UnityEngine; | ||
|
||
namespace Mew.Core.TaskHelpers | ||
{ | ||
internal static class TaskHelperInternal | ||
{ | ||
private static readonly Stack<AwaitableCompletionSource> Pool = new(); | ||
private static readonly Queue<AwaitableCompletionSource> Queued = new(); | ||
private static readonly List<AwaitableCompletionSource> Running = new(); | ||
private static bool registered; | ||
|
||
public static async Task NextFrame() | ||
{ | ||
if (!registered) | ||
{ | ||
MewLoop.Add<MewUnityUpdate>(OnUpdate); | ||
registered = true; | ||
} | ||
if (Pool.Count == 0) | ||
Pool.Push(new AwaitableCompletionSource()); | ||
var awaitableCompletionSource = Pool.Pop(); | ||
awaitableCompletionSource.Reset(); | ||
Queued.Enqueue(awaitableCompletionSource); | ||
await awaitableCompletionSource.Awaitable; | ||
return; | ||
|
||
void OnUpdate() | ||
{ | ||
while (Queued.Count > 0) | ||
Running.Add(Queued.Dequeue()); | ||
|
||
foreach (var completionSource in Running) | ||
{ | ||
completionSource.TrySetResult(); | ||
Pool.Push(completionSource); | ||
} | ||
Running.Clear(); | ||
} | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#if !UNITY_2023_2_OR_NEWER | ||
using System.Threading.Tasks; | ||
|
||
namespace Mew.Core.TaskHelpers | ||
{ | ||
internal static class TaskHelperLegacyInternal | ||
{ | ||
public static async Task NextFrame() | ||
{ | ||
TaskCompletionSource<bool> taskCompletionSource = new(); | ||
MewLoop.Add<MewUnityUpdate>(OnUpdate); | ||
await taskCompletionSource.Task; | ||
MewLoop.Remove<MewUnityUpdate>(OnUpdate); | ||
return; | ||
|
||
void OnUpdate() => taskCompletionSource.TrySetResult(true); | ||
} | ||
} | ||
} | ||
#endif |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters