A thread-safe way of dispatching IEnumerator functions to the main thread in unity. Useful for calling UI functions and other actions that Unity limits to the main thread from different threads. Initially written for Firebase Unity but now used across the board!
This fork is based on version from PimDeWitte and adds changes from nvandamme and ssootube
You have a few different options to install into your Unity project:
-
Probably the easiest way. Just add the git URL and let the package manager install it for you.
-
Edit the project manifest file by hand.
-
Download a tarball and install in folder.
Open the Package Manager (UPM) in Unity Windows -> Package Manager
.
Select +
in the top-left of the UPM panel and select Add package from Git URL...
Enter https://github.com/NeGodAndre/UnityMainThreadDispatcher.git
in the text box and click add.
More info: Unity Manual: Installing from a Git URL.
Syntax: | URL example |
---|---|
Latest default branch | "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git" |
Specific branch | "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git/#branch-name" |
Specific version (tag) | "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git#v.1.0.0" |
Commit hash | "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git#4f712b3070ed21ba239d472158885efa0d3e26b3" |
Open Packages/manifest.json
with your favorite text editor. Add the following line to the dependencies block.
{
"dependencies": {
"unity.main.thread.dispatcher": "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git"
},
}
Notice: Unity Package Manager records the current commit to a lock entry of the manifest.json. To update to the latest version, change the "hash"
value manually or just remove the lock entry to resolve the package in Packages/packages-lock.json
.
{
"version": "https://github.com/NeGodAndre/UnityMainThreadDispatcher.git",
"depth": 0,
"source": "git",
"dependencies": {},
"hash": "4f712b3070ed21ba239d472158885efa0d3e26b3"
}
More info: Unity Manual about
- Download and extract a release or a tag to your machine.
- Import it into the following directory in your Unity project:
- For
Unity 2018.1 or later
:Packages
- For
Unity 2017.x or later
:Assets
- For
Repository have two version scrits:
-
UnityMainThreadDispatcherSemaphore use SemaphoreSlim for wait.
-
UnityMainThreadDispatcherLock use lock for wait.
Although scripts create of an object on the first call. Recommend to create an object with the selected script in advance.
Example for UnityMainThreadDispatcherSemaphore (similar to UnityMainThreadDispatcherLock)
public IEnumerator ThisWillBeExecutedOnTheMainThread() {
Debug.Log ("This is executed from the main thread");
yield return null;
}
public void ExampleMainThreadCall() {
UnityMainThreadDispatcherSemaphore.Instance().Enqueue(ThisWillBeExecutedOnTheMainThread());
}
OR
UnityMainThreadDispatcherSemaphore.Instance().Enqueue(() => Debug.Log ("This is executed from the main thread"));
1.1.0 - Added alternative script using SemaphoreSlim. Added functional for autocreate object. Unity package is created.
1.0. - Tested and functional in one or more production applications, including those from major companies.