diff --git a/Assets/Kekser/PowerBus/Bus.cs b/Assets/Kekser/PowerBus/Bus.cs index 72977d8..f013c55 100644 --- a/Assets/Kekser/PowerBus/Bus.cs +++ b/Assets/Kekser/PowerBus/Bus.cs @@ -5,6 +5,7 @@ namespace Kekser.PowerBus public class Bus : IBus where T : class { private BusManager _manager; + private Action _onChange; public Bus(T initialValue = null, BusManager manager = null) { @@ -12,8 +13,18 @@ public Bus(T initialValue = null, BusManager manager = null) _manager.RegisterBus(this, initialValue); } + public Bus(Action onChange, T initialValue = null, BusManager manager = null) + { + _manager = manager ?? BusManager.GlobalInstance; + _manager.RegisterBus(this, initialValue); + _onChange = onChange; + OnChange += _onChange; + } + ~Bus() { + if (_onChange != null) + OnChange -= _onChange; _manager.UnregisterBus(this); } diff --git a/Assets/Kekser/PowerBus/package.json b/Assets/Kekser/PowerBus/package.json index 34ab4b8..8155a1b 100644 --- a/Assets/Kekser/PowerBus/package.json +++ b/Assets/Kekser/PowerBus/package.json @@ -2,7 +2,7 @@ "name": "com.kekser.powerbus", "displayName": "Power Bus", "author": { "name": "Kekser", "url": "https://github.com/DerKekser" }, - "version": "1.0.0", + "version": "1.1.0", "unity": "2020.3", "description": "A simple event bus for Unity", "keywords": [ "bus", "event", "eventbus", "powerbus", "simple", "unity" ], diff --git a/Assets/Kekser/Tests/PowerBusTests.cs b/Assets/Kekser/Tests/PowerBusTests.cs index 6393b56..001e27f 100644 --- a/Assets/Kekser/Tests/PowerBusTests.cs +++ b/Assets/Kekser/Tests/PowerBusTests.cs @@ -47,6 +47,14 @@ public IEnumerator TestBusOnChange() Assert.AreEqual("World", receivedValue.StringValue); } + [UnityTest] + public IEnumerator TestBusOnChangeInline() + { + yield return null; + var bus = new Bus((v) => Assert.AreEqual("Hello", v.StringValue)); + bus.Value = new Event1 { StringValue = "Hello" }; + } + [UnityTest] public IEnumerator TestBusImplicitConversion() { diff --git a/README.md b/README.md index 6128655..b1c8741 100644 --- a/README.md +++ b/README.md @@ -6,8 +6,9 @@ - [Simple Example](#simple-example) - [Event Handling](#event-handling) - [Bus Creation](#bus-creation) -- [Initial Value](#initial-value) -- [Bus Manager](#bus-manager) + - [Inline Event Handling](#inline-event-handling) + - [Initial Value](#initial-value) + - [Bus Manager](#bus-manager) - [Install](#install) - [Install via Unity Package](#install-via-unity-package) - [Install via git URL](#install-via-git-url) @@ -52,7 +53,26 @@ var bus = new Bus(); bus.Value = new StringEvent { Value = "Hello World" }; ``` -### Initial Value +#### Inline Event Handling + +You can handle events inline by passing a lambda function or a method to the constructor. + +```csharp +var bus = new Bus(e => Debug.Log(e.Value)); +bus.Value = new StringEvent { Value = "Hello World" }; + +// or + +void HandleEvent(StringEvent e) +{ + Debug.Log(e.Value); +} + +var bus = new Bus(HandleEvent); +bus.Value = new StringEvent { Value = "Hello World" }; +``` + +#### Initial Value You can set the initial value of the bus by passing it to the constructor. @@ -62,7 +82,7 @@ You can set the initial value of the bus by passing it to the constructor. var bus = new Bus(new StringEvent { Value = "Hello World" }); ``` -### Bus Manager +#### Bus Manager You can manage multiple buses using the `BusManager` class.