PlayerLoopHelper
Simple helper class for registering/unregistering systems in the PlayerLoop
com.gilzoide.playerloophelper Install via UPM
Add to Unity Package Manager using this URL
https://www.pkglnk.dev/playerloophelper.git README Markdown
Copy this to your project's README.md
## Installation
Add **PlayerLoopHelper** to your Unity project via Package Manager:
1. Open **Window > Package Manager**
2. Click **+** > **Add package from git URL**
3. Enter:
```
https://www.pkglnk.dev/playerloophelper.git
```
[](https://www.pkglnk.dev/pkg/playerloophelper)README
PlayerLoopHelper
Single file helper class for registering/unregistering systems in Unity's PlayerLoop.
How to install
Either:
- In the Package Manager Window,
install using this repository's Git URL:
https://github.com/gilzoide/unity-playerloophelper.git - Copy the PlayerLoopHelper.cs file anywhere inside your project.
Usage example
using System;
using System.Collections.Concurrent;
using PlayerLoopHelper;
// Class for scheduling tasks that will run in Unity's Main Thread.
//
// Using a PlayerLoopSystem avoids needing a living singleton GameObject
// with a MonoBehaviour that overrides `Update`, which is the usual way
// of implementing such functionality in Unity.
//
// Usage:
// MainThreadDispatcher.Dispatch(() =>
// {
// Debug.Log("Some action that runs on Unity's Main Thread"));
// }
public static class MainThreadDispatcher
{
static readonly ConcurrentQueue<Action> _taskQueue;
static MainThreadDispatcher()
{
_taskQueue = new ConcurrentQueue<Action>();
Enable();
}
public static bool Enable()
{
return PlayerLoopSystemHelper.Register(
// PlayerLoop systems are identified by their Type
typeof(MainThreadDispatcher),
// "FirstChildOf Update": this system will run as the first step
// in the Update phase, before other components
// For more phases, check out UnityEngine.PlayerLoop subclasses
// (e.g.: https://docs.unity3d.com/ScriptReference/PlayerLoop.Update.html)
InsertPosition.FirstChildOf,
typeof(UnityEngine.PlayerLoop.Update),
// Callback that will run once per frame
UpdateCallback
);
}
public static bool Disable()
{
return PlayerLoopSystemHelper.Unregister(typeof(MainThreadDispatcher));
}
public static void Dispatch(Action action)
{
_taskQueue.Enqueue(action);
}
static void UpdateCallback()
{
while (_taskQueue.TryDequeue(out Action task))
{
task();
}
}
}
API
enum InsertPosition
Before: insert new system before specified one, as its siblingAfter: insert new system after specified one, as its siblingFirstChildOf: insert new system as the first child of specified oneLastChildOf: insert new system as the last child of specified one
class PlayerLoopSystemHelper
static bool Register(Type type, InsertPosition position, Type anchorType, PlayerLoopSystem.UpdateFunction action)Registers a
PlayerLoopSystemwith the giventypeandactionin the specifiedpositionrelative toanchorType.Returns whether
anchorTypewas found and system was inserted successfully.static bool Unregister(Type type)Unregisters a
PlayerLoopSystem.Returns whether
typewas found and removed successfully.static bool IsRegistered(Type type)Returns whether a
PlayerLoopSystemwithtypeis registered.
Other projects for injecting callbacks in Unity's PlayerLoop
Comments
No comments yet. Be the first!
Sign in to join the conversation
Sign In