Skip to content

Instantly share code, notes, and snippets.

@bendangelo
Created September 8, 2016 03:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendangelo/67302261204126e8e1132298f0df9c10 to your computer and use it in GitHub Desktop.
Save bendangelo/67302261204126e8e1132298f0df9c10 to your computer and use it in GitHub Desktop.
Unity3D class used for health bars and anything else that has a min / max.
using UnityEngine;
using System.Collections;
public class StatRange {
public int min { get; set; }
public int max { get; set; }
public int value { get; set; }
public StatRange(int min = 0, int max = 0) {
this.max = max;
this.min = min;
}
public void Reset() {
value = max;
}
public float Percent {
get { return (float)value / max; }
set { this.value = (int)((float)max * value); }
}
public void Add(int val) {
value += val;
Clamp();
}
public void Minus(int val) {
value -= val;
Clamp();
}
public void Clamp() {
value = Mathf.Clamp(value, min, max);
}
public bool IsMin {
get { return value == min; }
}
public bool IsMax {
get { return value == max; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment