我创建了一个UI滑块,它根据我的脚本中的值(播放时间)移动,到目前为止它运行顺畅。这是我的滑块代码
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class testingtime : MonoBehaviour {
public int playtime =0;
public int seconds = 0;
private int minutes = 0;
private int hours = 0;
public int playtimemax = 0;
public Slider testslider;
public Texture playbutton;
public Texture Pausebutton;
Texture substitute;
bool ispaused = false;
// Use this for initialization
void Start () {
substitute = Pausebutton;
StartCoroutine ("playtimer");
}
private IEnumerator playtimer(){
while (true) {
yield return new WaitForSeconds(1);
playtime += 1;
seconds = (playtime % 60);
minutes = (playtime / 60) % 60;
hours = (playtime / 3600) % 24;
}
}
// Update is called once per frame
void Update () {
testslider.value = playtime;
if (ispaused) {
Time.timeScale = 0;
substitute = playbutton;
}
if (!ispaused) {
Time.timeScale = 1;
substitute = Pausebutton;
}
playtime = (int)testslider.value;
}
void OnGUI (){
if (GUI.Button (new Rect (685,710, 20, 20), substitute)) {
ispaused = !ispaused;
}
GUI.Label (new Rect (50, 50, 400, 50), "Playtime = " + hours.ToString () + " Hours " + minutes.ToString () + " Minutes " + seconds.ToString () + " Seconds");
}
}
所以基本上这会让我的滑块根据游戏时间移动,每秒钟像计时器一样增加1。
但是我尝试通过滑块(我已启用交互式)将代码playtime = (int)testslider.value;
更改为playtime的值。
我知道我无法移动滑块,因为它不断更新testtimer.value = playtime
。
是否有任何可用的功能,例如testtimer.onclicked,以便我可以将它放在if函数中并禁用更新?
答案 0 :(得分:1)
Slider具有onValueChanged
回调功能,您可以将其用于此目的。
答案 1 :(得分:1)
我知道了! :D最后 您只需要这段代码
public void adjusttimer(float newtime){
playtime = (int)newtime;
}
并指定类似
的方法它应该完美地工作。 :d 非常感谢Max Yankov的想法!