Skip to content

dducode/com.dducode.streams

Repository files navigation

About Streams

Streams is the unity package for simplify managed the low level player loop API. In the most common case you can subscribe to any stream of the player loop subsystem

UnityPlayerLoop.GetStream<Update>().Add(deltaTime => Debug.Log(deltaTime));

Installing

Install the package in unity package manager from the git url https://github.com/dducode/com.dducode.streams.git

Execution types

Many different types of actions can be connected to a stream. In the previous example showed one way to subscribe to a stream - it's persistent action. Also, you can connect parallel action, which will be executed in parallel with the others

UnityPlayerLoop.GetStream<Update>().AddConcurrent(deltaTime => /* some actions */);

To configure the parallel work strategy, set the stream's WorkStrategy property to Economy, Optimal or Performance

Ordinary actions will be executed sequentially by priority or by the time of their creation with the same priority. Also, one of the common ways to connect to the stream - once action

UnityPlayerLoop.GetStream<Update>().AddOnce(() => /* some actions */);

And async once action

UnityPlayerLoop.GetStream<Update>().AddOnce(async () => {
  await StreamTask.Delay(100);
  Debug.Log("Complete action");
});

You can use StreamTask inside the stream execution only. Also, you can convert Task, UniTask or Awaitable to StreamTask using extension methods. StreamTask allows code execution to continue in the same stream

Execution configuration

If you want, you can configure the action by setting the priority and subscription cancellation token

UnityPlayerLoop.GetStream<Update>().Add(deltaTime => Debug.Log(deltaTime), subscriptionToken).SetPriority(0);

Zero priority is the highest, the lowest priority is the max value of the uint type. If a token cancellation is requested, the subscription will be destroyed. Also, you can configure the delta of the execution and its tick rate

UnityPlayerLoop.GetStream<Update>().Add(deltaTime => Debug.Log(deltaTime)).SetDelta(0.5f); // will be executed twice per second

You can set either delta or tick rate, but not both at the same time

Streams lifetime

Streams from the unity player loop live all the time. But you can subscribe to the scene streams or the game object streams

SceneManager.GetActiveScene().GetStream<Update>();
GameObject.Find("MyGO").GetStream<FixedUpdate>();

Scene streams live as long as their scene is loaded. Same goes for game object streams - they live as long as their game object is alive. For scene streams, the underlying streams are the player loop streams, and for game object streams, the underlying streams are the scene streams.

Managed streams

In a more advanced scenario you can create and configure managed execution stream

var customStream = new ManagedExecutionStream(UnityPlayerLoop.GetStream<Update>()) { // a managed stream requires a base stream in which to execute
  Priority = 0,
  Delta = 0.02f
};
customStream.Add(deltaTime => Debug.Log(deltaTime));

You can also join a stream with another stream, reconnect to another base stream, and terminate it

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages