NeGodAndre / UnityMainThreadDispatcher

A simple, thread-safe way of executing actions (Such as UI manipulations) on the Unity Main Thread

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UnityMainThreadDispatcher

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!

Cradits

This fork is based on version from PimDeWitte and adds changes from nvandamme and ssootube

Installation

You have a few different options to install into your Unity project:

  1. Unity Package Manger

    Probably the easiest way. Just add the git URL and let the package manager install it for you.

  2. Manual Installation

    Edit the project manifest file by hand.

  3. Install from a File

    Download a tarball and install in folder.

Unity Package Manager

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"

Manual Installation

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

Install from a File

  1. Download and extract a release or a tag to your machine.
  2. Import it into the following directory in your Unity project:
    • For Unity 2018.1 or later : Packages
    • For Unity 2017.x or later : Assets

Usage

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"));

Version

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.

About

A simple, thread-safe way of executing actions (Such as UI manipulations) on the Unity Main Thread

License:Apache License 2.0


Languages

Language:C# 100.0%