Dssdiego
4/23/2020 - 7:11 PM

FloatVariable

Unity Float Variable according to Best Practices

using UnityEngine;
using UnityEngine.Events;

[CreateAssetMenu(menuName = "Variable/Float")]
public class FloatVariable : ScriptableObject
{
#if UNITY_EDITOR
    [Multiline]
    public string description;
#endif

    public float runtimeValue;
    public float initialValue;
    public float maxValue;

    public static UnityAction<float> OnChanged;

    public void Add(float val)
    {
        runtimeValue += val;
        OnChanged?.Invoke(runtimeValue);
    }

    public void AddWithLimit(float val)
    {
        if (runtimeValue > maxValue) return;
        
        runtimeValue += val;
        OnChanged?.Invoke(runtimeValue);
    }

    public void Reset()
    {
        runtimeValue = initialValue;
    }
}