Util, that Designed to avode unnecessary boilerplate code while using publisher-subscriber pattern in Unity
- Auto event on value changes
- Safety access to your variable via readonly interface
- Implicit casting to value and EventValue shell vise-versa
Find option "Add package from git URL..." and insert
https://github.com/NickKhalow/EventValue.git?path=/Packages/EasyTools
Downlaod https://github.com/NickKhalow/EventValue/tree/main/Packages/EasyTools directory and add to your Packages directory into your project
Main class that provides functionality of auto eventing
private EventValue<int> _jumpCount;
private void Awake()
{
_jumpCount = 0; // we can use implicit casting
}
private EventValue<int> _jumpCount;
private void Save()
{
PlayerPrefs.SetInt("MaxCount", _jumpCount);
}
private void Update()
{
if (Input.anyKeyDown)
_jumpCount.Value++; //necessary change value only via Value property
}
private void Awake()
{
_jumpCount.ValueChanged += JumpCountOnValueChanged;
}
private void JumpCountOnValueChanged(int obj)
{
//TODO something with value
}
private void OnDestroy()
{
_jumpCount.ValueChanged -= JumpCountOnValueChanged; //Don't forget unsubscribe
}
Use when you need to ensure your client code can't modify value
Interface provides readonly access
- Event Action allows subscribe to value
- Value Property allows directly get current value
public interface IReadOnlyEventValue<out T> where T : new()
{
/// <summary>
/// Action notifies of any value changes
/// </summary>
public event Action<T> ValueChanged;
/// <summary>
/// Returns current value.
/// </summary>
public T Value { get; }
}
EventValue upcasts to IReadOnlyEventValue
private EventValue<int> _jumpCount;
public IReadOnlyEventValue<int> JumpCount => _jumpCount;
With this example you can familiarize with EventValue util
MIT