https://github.com/sassyasssas/playerloop-extension
Lightweight unity asset package that provides a convenient way of editing Unity's PlayerLoop.
https://github.com/sassyasssas/playerloop-extension
csharp playerloop unity unity-package unity-scripts unity2d unity3d
Last synced: 5 months ago
JSON representation
Lightweight unity asset package that provides a convenient way of editing Unity's PlayerLoop.
- Host: GitHub
- URL: https://github.com/sassyasssas/playerloop-extension
- Owner: SassyAssSas
- License: mit
- Created: 2024-07-08T12:35:58.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2024-10-05T21:10:55.000Z (over 1 year ago)
- Last Synced: 2025-02-13T22:16:56.776Z (over 1 year ago)
- Topics: csharp, playerloop, unity, unity-package, unity-scripts, unity2d, unity3d
- Language: C#
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# PlayerLoop Extension
Provides a convenient way of editing Unity's PlayerLoop.
## Table of contents
- [Installation](#installation)
- [PlayerLoopBuilder](#playerloopbuilder)
- [PlayerLoopSystem extension methods](#playerloopsystem-extension-methods)
- [Editor mode nuanses](#editor-mode-nuanses)
## Installation
To start using the package, go to [releases page](https://github.com/SassyAssSas/playerloop-extension/releases) and get the release of your choice.
## PlayerLoopBuilder
To start working with the library use the `Violoncello.PlayerLoopExtensions` namespace.
To edit the PlayerLoop use `PlayerLoopBuilder` class. There are 3 ways to get an instance of `PlayerLoopBuilder`. You can see them all on the example below:
```csharp
// Will return a new instance PlayerLoopBuilder with empty player loop:
PlayerLoopBuilder.FromNew();
// Will return a new instance PLayerLoopBuilder that already contains default PlayerLoop systems:
PlayerLoopBuilder.FromDefault();
// Will return a new instance PLayerLoopBuilder that contains all the current PlayerLoop systems:
PlayerLoopBuilder.FromCurrent();
```
`PlayerLoopBuilder` object is used to add and remove systems in the PlayerLoop using methods shown on the example below:
```csharp
// All the creation methods return the instance of PlayerLoopBuilder
// So you can proceed to editing immediately
PlayerLoopBuilder.FromCurrent()
.AddToRoot(mySystem)
.AddToSubSystem(mySystem)
.AddToRoot(Callback)
.AddToSubSystem(Callback)
.RemoveFromRoot()
.RemoveFromSubSystem()
.RemoveFromRoot(typeof(MySystem))
.RemoveFromSubSystem(typeof(Update), typeof(MySystem));
```
When you finish working with the builder you can either call the `PlayerLoopBuilder.Build` method to get a result `PlayerLoopSystem`:
```csharp
var playerLoop = PlayerLoopBuilder.FromCurrent()
.AddToSubSystem(MyUpdateCallback)
.AddToSubSystem()
.Build();
// Will set the result PlayerLoopSystem as unity's main PlayerLoop
PlayerLoop.SetPlayerLoop();
```
Or call `PlayerLoopBuilder.SetPlayerLoop()` which will set the result `PlayerLoopSystem` as unity's default PlayerLoop without you having to retrieve the result `PlayerLoopSystem` and doing it yourself:
```csharp
PlayerLoopBuilder.FromCurrent()
.AddToSubSystem(MyUpdateCallback)
.AddToSubSystem()
.SetPlayerLoop();
```
## PlayerLoopSystem extension methods
If you need to edit a `PlayerLoopSystem`, you might make a use of it's new extension methods:
```csharp
// Searches for a subSystem with the passed type and returns it.
// Throws an exception if doesn't find the subSystem
playerLoopSystem.FindSubSystem();
playerLoopSystem.FindSubSystem(typeof(MySystem));
// Searches for a subSystem with the passed type and puts it in the out variable.
// Returns true if the subSystem was found, otherwise returns false
playerLoopSystem.TryFindSubSystem(out PlayerLoopSystem system);
playerLoopSystem.TryFindSubSystem(out PlayerLoopSystem system, typeof(MySystem));
// Adds a subSystem
playerLoopSystem.AddSubSystem(mySystem);
// Creates a new subSystem using passed type and callback and adds it
playerLoopSystem.AddSubSystem(Callback);
// Removes all subSystems with passed type
playerLoopSystem.RemoveSubSystem();
playerLoopSystem.RemoveSubSystem(typeof(MySystem));
// Searches for a subSystem with the passed type and replaces it
playerLoopSystem.ReplaceSubSystem(mySystem);
playerLoopSystem.ReplaceSubSystem(Callback, typeof(OtherSystem));
```
## Editor mode nuanses
By default all the added systems keep working even if you exit play mode. `PlayerLoopBuilder` saves all the systems you add and removes them on play mode exit. If you want to disable this behaviour, while adding a new system pass `false` as a second argument:
```csharp
// This subsystem won't be automatically removed
PlayerLoopBuilder.FromCurrent()
.AddToSubSystem(mySystem, false)
.SetPlayerLoop();
```