怎么在NGUI显示3d特效呢?下面我就来详细的说一下流程(例子)
创建一个新的工程,删除Main Camera
创建UIroot
选择NGUI自带的Fantasy 图集和字体
选择Progress Bar 控件
OK,我们点击Add To 添加到场景中
选择背景将颜色改为淡红,Scale X设置为300
选择Foreground将颜色设置为大红色
好了,这样我们基本上就算是得到一个血条了,现在在将foreground的ScaleX也设置为300
创建C#脚本VatilBarControl
编写如下代码
- public class VatilBarControl : MonoBehaviour {
- public float MaxPower
- {
- get { return maxPower; }
- set { maxPower = value; UpdateDisplay(power / maxPower); }
- }
- public float Power
- {
- get { return power; }
- set
- {
- power = value;
- UpdateDisplay(power / maxPower);
- }
- }
- public UILabel lbl_text;
- public bool DisplayText { get; set; }
- private UISlider _powSlider;
- private float power = 0;
- private float maxPower = 0;
- private float _maxWidth;
- void Awake()
- {
- _powSlider = GetComponent<UISlider>();
- Debug.Log(_powSlider.name);
- if (_powSlider == null)
- {
- Debug.LogError("PowerBarBasic: powSlider is null!");
- }
- _maxWidth = _powSlider.foreground.localScale.x;
- }
- /// <summary>
- /// 显示能量条
- /// </summary>
- /// <param name="px">百分比</param>
- void UpdateDisplay(float px)
- {
- if (px < 0)
- px = 0;
- else if (px > 1)
- px = 1;
- _powSlider.foreground.localScale = new Vector3(_maxWidth * px, _powSlider.foreground.localScale.y, _powSlider.foreground.localScale.z);
- if (DisplayText)
- lbl_text.text = string.Format("{0}/{1}", power, maxPower);
- else
- lbl_text.text = "";
- }
- void Update()
- {
- UpdateDisplay(power / maxPower);
- }
- void Start()
- {
-
- <span style="white-space:pre"> </span>Power = 90;
- MaxPower = 100;
- DisplayText = true;
- UpdateDisplay(1.0f);
- }
- }
将其绑定到Progress Bar我们会发现脚本需要一个lable下面创建一个lable