Last active
April 28, 2020 08:52
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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