【 unity3d 】用脚本代码控制进度条progressbar(假加载)


一:制作UI对象

1、先制作进度条progressbar (一个空物体,包含3个sprite图片),

添加设置Progress Bar脚本


2、给thumb设置Privot,使之移动角度和方向看起来更合适些

3、给thumb添加动画Sprite Animation

二:写上脚本代码

主要是对Progress的value值进行改写

using UnityEngine;
using System.Collections;

public class ProgressSC : MonoBehaviour {

	public float _duration = 10f;
	UIProgressBar _progress;

	void Start () {
		_progress = GetComponent<UIProgressBar> ();

	}

	void Update () {
		if (_progress.value < 1 ) {
			float value = 1 / _duration * Time.deltaTime;
			_progress.value += value;
		} else {
			_progress.value = 0;
		}
	}
}


此时播放游戏,控制台会随着进度条的滑动打印文字

三:扩展 添加文字Label,使之数据能够随着进度条更新

找到UILabel的setCurrentPercent方法



此时Label文字显示会随着进度条更新显示百分比数值


猜你喜欢

转载自blog.csdn.net/liaoshengg/article/details/80832382