Giter VIP home page Giter VIP logo

unitymainthreaddispatcher's Introduction

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.

unitymainthreaddispatcher's People

Contributors

pimdewitte avatar negodandre avatar damianmehers avatar admiralsnyder avatar fireforge avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.