【发布时间】:2018-04-08 20:32:46
【问题描述】:
在我的游戏中,我有一个表示剩余时间的滑块,剩余时间每秒减少 1 这是我想出的,但 除了第一次之外,滑块不会下降 谁能帮我修复我的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class Timer : MonoBehaviour
{
public float time = 100;
public float totalTime = 100;
public float duration = 1;
public Slider slider;
// Use this for initialization
void Start()
{
time = totalTime;
slider.value = time;
StartCoroutine(CountDown());
}
// Update is called once per frame
IEnumerator CountDown()
{
if (time > 0)
{
yield return new WaitForSeconds(duration);
time -= 1;
slider.value = time;
yield return CountDown();
}
}
}
已解决:
public class Timer : MonoBehaviour
{
public float timeLeft;
public float maxTime = 100f;
public float duration = 1;
public Slider slider;
// Use this for initialization
private void Start()
{
timeLeft = maxTime;
slider.value = timeLeft;
}
// Update is called once per frame
void Update()
{
if (timeLeft > 0)
{
timeLeft -= Time.deltaTime;
slider.value = timeLeft / maxTime;
}
else
{
Time.timeScale = 0;
}
}
}
【问题讨论】:
-
我们需要一个比“不起作用”更精确的问题描述。
-
@Christopher 好的,我更改了描述
-
@Christopher 你会帮我写代码吗
-
@ModusTollens - 我已经部分修复了它。 OP 现在应该编辑答案并单独发布。
-
@JonBergeron 您能否复制并剪切问题中的答案并将其粘贴到答案部分?如果您愿意,您甚至可以接受自己的答案。
标签: c# user-interface unity3d timer