1
NGUI 3 Support / Re: UISlider - changing its position and appearance by script?
« on: May 20, 2014, 09:00:01 PM »
This might help, its how I update a UISlider as a healthbar
using UnityEngine;
using System.Collections;
public class VitalBar : MonoBehaviour {
public UILabel healthBarLabel;
private UISlider vitalSlider;
private float _maxWidth;
private bool _displayText = false;
void Awake (){
vitalSlider = GetComponent<UISlider> ();
if (vitalSlider == null) {
Debug.LogError("Could not find the UISlider Component!");
return;
}
_maxWidth = vitalSlider.foregroundWidget.localSize.x;
_displayText =true;
}
// Use this for initialization
void Start () {
UpdateDisplay (- 5f, "100/200");
}
//vitalSlider.sliderValue = health/healthBarLabel;
public void UpdateDisplay(float x ){
if (x > 0)
x = 0;
else if (x > 1)
x = 1;
vitalSlider.value = 50;
DisplayText = false;
}
public void UpdateDisplay(float x, string str ){
UpdateDisplay (x);
if (str != "") healthBarLabel.text = str;
}
public bool DisplayText {
get { return _displayText;}
set { _displayText = value;
if (!_displayText)
healthBarLabel.text = ""; }
}
}
using UnityEngine;
using System.Collections;
public class VitalBar : MonoBehaviour {
public UILabel healthBarLabel;
private UISlider vitalSlider;
private float _maxWidth;
private bool _displayText = false;
void Awake (){
vitalSlider = GetComponent<UISlider> ();
if (vitalSlider == null) {
Debug.LogError("Could not find the UISlider Component!");
return;
}
_maxWidth = vitalSlider.foregroundWidget.localSize.x;
_displayText =true;
}
// Use this for initialization
void Start () {
UpdateDisplay (- 5f, "100/200");
}
//vitalSlider.sliderValue = health/healthBarLabel;
public void UpdateDisplay(float x ){
if (x > 0)
x = 0;
else if (x > 1)
x = 1;
vitalSlider.value = 50;
DisplayText = false;
}
public void UpdateDisplay(float x, string str ){
UpdateDisplay (x);
if (str != "") healthBarLabel.text = str;
}
public bool DisplayText {
get { return _displayText;}
set { _displayText = value;
if (!_displayText)
healthBarLabel.text = ""; }
}
}
