Skip to content

Instantly share code, notes, and snippets.

@Dssdiego
Last active April 28, 2020 08:52
Show Gist options
  • Save Dssdiego/f363e6c5b0f402052437d7b102667b2a to your computer and use it in GitHub Desktop.
Save Dssdiego/f363e6c5b0f402052437d7b102667b2a to your computer and use it in GitHub Desktop.
Unity Float Variable according to [Best Practices](https://unity.com/how-to/architect-game-code-scriptable-objects)
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment