I'm working on creating a resource bar (health, mana, etc) using NGUI's UISlider (as a progressbar). I'm updating sliderValue to change the foreground, but this causes the foreground to jump. Is there a way to use tweening to soften the changes? My guess would be adding the component to the foreground and destroying it once the animation is done or updating it if the sliderValue changes again. But if I add the component manually, how do I know what the scale size should be?
I took a look at the documentation and didn't see any hints toward tweening it and Google is "catering" my results too much to be useful. Below is a driver and the resource bar script. Ideally the tweening would be inside UpdateResource()
#pragma strict
public var bar : ResourceBar;
function Start() {
bar.SetResource(125, 125);
}
private var lastUpdate : long;
private var updateGap : int = 3;
function Awake() {
lastUpdate = Time.time + updateGap;
}
function Update () {
if(Time.time >= lastUpdate) {
bar.SetResource( Random.Range(0, maxResource) );
lastUpdate = Time.time + updateGap;
}
}
#pragma strict
public var progressBar : UISlider;
public var foreground : UISprite;
public var label : UILabel;
private var currentResource : int;
private var maxResource : int;
function SetResource(current : int, max : int) {
maxResource = max;
SetResource(current);
}
function SetResource(current : int) {
currentResource = current;
UpdateResource();
}
private function UpdateResource() {
label.text = "" + currentResource + " / " + maxResource;
// TODO: Tweening
progressBar.sliderValue = Mathf.Clamp(currentResource * 1f / maxResource, 0, 1);
}