Skip to content

Commit

Permalink
Merge pull request #9 from coryleach/dev
Browse files Browse the repository at this point in the history
Minor Bump for WebGL Compatibility Changes
  • Loading branch information
coryleach authored Jul 16, 2023
2 parents ec20cf0 + b784184 commit 2fb8f43
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 32 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
#### Using UnityPackageManager (for Unity 2019.3 or later)
Open the package manager window (menu: Window > Package Manager)<br/>
Select "Add package from git URL...", fill in the pop-up with the following link:<br/>
https://github.com/coryleach/UnityAsync.git#1.0.5<br/>
https://github.com/coryleach/UnityAsync.git#1.0.6<br/>

#### Using UnityPackageManager (for Unity 2019.1 or later)

Find the manifest.json file in the Packages folder of your project and edit it to look like this:
```js
{
"dependencies": {
"com.gameframe.async": "https://github.com/coryleach/UnityAsync.git#1.0.5",
"com.gameframe.async": "https://github.com/coryleach/UnityAsync.git#1.0.6",
...
},
}
Expand Down Expand Up @@ -93,7 +93,7 @@ await Awaiters.MainUnityThread;

## Show your support
Give a ⭐️ if this project helped you!
{AUTHOR.KOFI}


***
_This README was generated with ❤️ by [Gameframe.Packages](https://github.com/coryleach/unitypackages)_
35 changes: 17 additions & 18 deletions Runtime/Coroutines/CoroutineRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ private static void Install()
UnitySynchronizationContext = SynchronizationContext.Current;
Application.quitting += OnApplicationQuitting;
}

#if UNITY_EDITOR
[InitializeOnLoadMethod]
private static void EditorInstall()
{
EditorApplication.playModeStateChanged += EditorApplicationOnPlayModeStateChanged;
EditorApplication.playModeStateChanged += EditorApplicationOnPlayModeStateChanged;
}

private static void EditorApplicationOnPlayModeStateChanged(PlayModeStateChange obj)
{
//I do not intend to support editor coroutines
//Do I need to cancel coroutines on play mode change?
}
#endif
#endif

private static SynchronizationContext UnitySynchronizationContext { get; set; }
private static CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();

/// <summary>
/// Use this method when you want to await coroutine completion.
/// Coroutine itself will always run on the main thread.
Expand All @@ -49,19 +49,19 @@ private static void EditorApplicationOnPlayModeStateChanged(PlayModeStateChange
/// <returns>awaitable task</returns>
public static async Task RunAsync(IEnumerator routine)
{
await UnityTaskUtil.RunOnUnityThreadAsync(async () => await RunAsync(routine,cancellationTokenSource.Token).ConfigureAwait(false) );
await UnityTaskUtil.RunOnUnityThreadAsync(async () => await RunAsync(routine,cancellationTokenSource.Token));
}

/// <summary>
/// Use this method to start a coroutine from any thread.
/// Coroutines themselves always run on the main thread
/// </summary>
/// <param name="enumerator">coroutine to run</param>
public static void Start(IEnumerator enumerator)
{
UnityTaskUtil.RunOnUnityThread(async () => await RunAsync(enumerator,cancellationTokenSource.Token).ConfigureAwait(false));
UnityTaskUtil.RunOnUnityThread(async () => await RunAsync(enumerator,cancellationTokenSource.Token));
}

/// <summary>
/// Stop all coroutines that have been started with CoroutineRunner
/// </summary>
Expand All @@ -70,12 +70,12 @@ public static void StopAll()
cancellationTokenSource.Cancel();
cancellationTokenSource = new CancellationTokenSource();
}

private static void OnApplicationQuitting()
{
StopAll();
}

private static async Task RunAsync(IEnumerator routine, CancellationToken token)
{
var coroutine = RunCoroutine(routine);
Expand All @@ -85,17 +85,17 @@ private static async Task RunAsync(IEnumerator routine, CancellationToken token)
await Task.Yield();
}
}

private static IEnumerator RunCoroutine(object state)
{
var processStack = new Stack<IEnumerator>();
processStack.Push((IEnumerator)state);

while (processStack.Count > 0)
{
var currentCoroutine = processStack.Peek();
var done = false;

try
{
done = !currentCoroutine.MoveNext();
Expand All @@ -105,7 +105,7 @@ private static IEnumerator RunCoroutine(object state)
Debug.LogException(e);
yield break;
}

if (done)
{
processStack.Pop();
Expand All @@ -121,10 +121,9 @@ private static IEnumerator RunCoroutine(object state)
yield return currentCoroutine.Current;
}
}

}
}

}
}

20 changes: 10 additions & 10 deletions Runtime/UnityTaskUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static void Install()
}

public static bool CurrentThreadIsUnityThread => UnityThreadId == Thread.CurrentThread.ManagedThreadId;

public static TaskScheduler UnityTaskScheduler { get; private set; }

public static TaskFactory<UnityEngine.Object> UnityTaskFactory { get; private set; }
Expand All @@ -42,7 +42,7 @@ public static CoroutineWrapper StartCancelableCoroutineAsync(IEnumerator corouti
RunOnUnityScheduler(() => { host.StartCoroutine(wrapper.Run(coroutine)); });
return wrapper;
}

/// <summary>
/// Invokes an action on the Unity main thread.
/// Will invoke immediately if already on the main thread.
Expand Down Expand Up @@ -75,10 +75,10 @@ public static async Task<T> RunOnUnityThreadAsync<T>(Func<T> func)
}
var taskFactory = new TaskFactory<T>(UnityTaskScheduler);
var task = taskFactory.StartNew(func);
await task.ConfigureAwait(false);
await task;
return task.Result;
}

/// <summary>
/// Runs the given action on the Unity main thread
/// Will invoke immediately if already on the main thread
Expand All @@ -94,7 +94,7 @@ public static async Task RunOnUnityThreadAsync(Action action)
}
var taskFactory = new TaskFactory(UnityTaskScheduler);
var task = taskFactory.StartNew(action);
await task.ConfigureAwait(false);
await task;
}

/// <summary>
Expand All @@ -113,7 +113,7 @@ public static void RunOnUnityThread(Action action)
UnitySynchronizationContext.Post(_=> action(), null);
}
}

/// <summary>
/// Runs the given async delegate on the Unity main thread
/// </summary>
Expand Down Expand Up @@ -151,10 +151,10 @@ public static async Task<T> InstantiateAsync<T>(T prefab, Transform parent) wher
instance.name = prefab.name;
return instance;
});
await task.ConfigureAwait(false);
await task;
return task.Result as T;
}

/// <summary>
/// Instantiate a prefab asynchronously
/// Always creates a task on the Unity task scheduler even if already on the main thread.
Expand All @@ -170,9 +170,9 @@ public static async Task<T> InstantiateAsync<T>(T prefab) where T : UnityEngine.
instance.name = prefab.name;
return instance;
});
await task.ConfigureAwait(false);
await task;
return task.Result as T;
}

}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.gameframe.async",
"displayName": "Gameframe.Async",
"version": "1.0.5",
"version": "1.0.6",
"description": "> Async task utility package for Unity \n> Helper methods for starting tasks on the Unity thread. \n> Start and await coroutines from any thread.",
"keywords": [],
"author": {
Expand Down

0 comments on commit 2fb8f43

Please sign in to comment.