DarkRewar / BaseTool

A big library of basic tools that you might need in your Unity projects.
MIT License
45 stars 6 forks source link

Add ValueListener #30 #31

Closed DarkRewar closed 8 months ago

DarkRewar commented 8 months ago

ValueListener

If you want to use an Observer Pattern for a value, and you don't want to implement the entire change event handler, the ValueListener<T> lets you do that for you.

You need to declare a ValueListener<T> of your type as a field or a property. I recommend to declare it as readonly to avoid loosing the OnChanged event references.

Value is implicitly casted to or from the value type you want. That means you can initialize your object using the value directly (see following example).

using BaseTool;
using UnityEngine;
using UnityEngine.UI;

public class MyComponent : MonoBehaviour
{
    public readonly ValueListener<int> Lifepoints = 100;
    public readonly ValueListener<string> Nickname = new();

    public Text NameLabel;
    public Text LifeLabel;

    public void Start()
    {
        Lifepoints.OnChanged += (oldLife, newLife) => 
            LifeLabel.text = $"{oldLife} -> {newLife}/100";
        Nickname.OnChanged += (_, newName) => 
            NameLabel.text = newName;

        Nickname.Value = "MyName";
        string name = Nickname;
        Debug.Log(name);
    }
}