using UnityEngine;
using System.Collections;
using System;
public class VitalBar: MonoBehaviour {
// pointers to the child controls
private UISlider _sliderCtrl;
private UILabel _labelCtrl;
// using a private variable and a public variable that can be scene
// in the inspector, and checking that in the update loop. we can simulate
// properties for Unity3D. latebound properties for the lack of a better term.
// private value for Label, this way we can see when it changes.
private string _label;
// public value for Label, for the inspector.
public string Label;
// private value for VitalColor, this way we can see when it changes.
private Color _vitalColor;
// public value for Label, for the inspector.
public Color VitalColor;
// private value for Value, this way we can see when it changes.
private int _value = 10;
// public value for Value, for the inspector.
public int Value = 10;
// private value for MaxValue, this way we can see when it changes.
private int _maxValue = 10;
// public value for MaxValue, for the inspector.
public int MaxValue = 10;
//Max with of the foreground component of the progress bar control.
private float _maxWidth = 0.0f;
//flag to detect the first time this is in the update loop.
private bool _firstTime = true;
//flag to detect if all the controls have been initialized.
private bool _vitalBarInitialized;
// private value for percentFull, this way we can see when it changes.
private float _percentFull;
void Awake()
{
//grab the pointers to the child object
_sliderCtrl = transform.FindChild ("ProgressBar").GetComponent<UISlider>();
_labelCtrl = transform.FindChild ("Label").GetComponent<UILabel>();
//set the flag to see if they have been referenced.
_vitalBarInitialized = (_sliderCtrl!=null && _labelCtrl!= null);
_firstTime = true;
//if the controls are there then procede with first update
if(_vitalBarInitialized)
{
Debug.Log ("Init:" + _sliderCtrl.sliderValue);
SetControlValues();
_maxWidth = _sliderCtrl.foreground.localScale.x;
}
else
{
//if we are here we failed
Debug.Log ("Unable to initialize. Missing Sub Controls!");
}
}
void Update()
{
SetControlValues();
}
//this function does double duty.
//First it checks to see if there have been changes to any of the
//latebound properties, if they have been changed set them. (do nothing and do not update
//any of the gui controls if no changes have been made.
//Second it computes the changes (if any to the percent full) and adjust the width of the
//foreground.
private void SetControlValues()
{
// if not initialized then quit method.
if (_vitalBarInitialized)
{
if(_maxWidth == 0 && _firstTime)
_maxWidth = _sliderCtrl.foreground.localScale.x;
if( _vitalColor != VitalColor)
{
UIWidget widget = _sliderCtrl.foreground.GetComponent<UIWidget>();
if (widget != null)
{
widget.color = VitalColor;
//set the private property
_vitalColor = VitalColor;
}
}
if(_maxValue != MaxValue)
{
//set the private property
_maxValue = MaxValue;
}
//Check the range of Value
if(Value < 0)
{
Value = 0;
}
else if(Value>_maxValue)
{
Value = _maxValue;
}
if( (_value != Value) || _firstTime)
{
float percentFull=0;
if(_maxValue>0)
{
percentFull = (float)Value / (float)_maxValue;
}
Debug.Log("percentFull:" + percentFull);
Debug.Log("_maxWidth:" + _maxWidth);
Debug.Log("_maxWidth * percentFull:" + _maxWidth * percentFull);
if(_percentFull!=percentFull)
{
Debug.Log ("Before:" + _sliderCtrl.sliderValue);
_sliderCtrl.sliderValue = percentFull;
//_sliderCtrl.foreground.localScale = new Vector3(_maxWidth * percentFull,_sliderCtrl.foreground.localScale.y, _sliderCtrl.foreground.localScale.z); ;
Debug.Log ("After:" + _sliderCtrl.sliderValue);
//set the private property
_percentFull = percentFull;
_value = Value;
}
}
//if the text has changed change the text on the label control.
if( _label != Label)
{
_labelCtrl.text = Label + " " + _percentFull * 100 + " %";
//set the private property.
_label = Label;
}
//set the first time flag
_firstTime = false;
}
}
}