我怎样才能为我的进度条获得这个计时器?
How can I get this timer for my progress bar?
我目前正在学习如何创建进度条,但我 运行 遇到了问题。我不确定如何在我的 CraftCopperBar 脚本中引用 运行ning 计时器进行更新。或者如果我的想法有误请指正。
public IEnumerator CraftCopperBar()
{
while (copper >= copperBarValue)
{
button.SetActive(false);
copper -= copperBarValue;
yield return new WaitForSeconds(5f);
copperBar += 1 * multiplier;
if (copper < copperBarValue)
{
button.SetActive(true);
break;
}
}
public void Update()
progressBar.fillAmount = (float)(x / 5f);
在 IEnumerator
中升级到自动连续计时器。这是解决您问题的好方法,在以下代码中,您不再需要 Update 事件来填充进度。
public IEnumerator CraftCopperBar(float waitTime)
{
while (copper >= copperBarValue)
{
copper -= copperBarValue;
button.SetActive(false);
var timer = 0f;
while (timer <= waitTime)
{
timer += Time.deltaTime;
progressBar.fillAmount = timer / waitTime;
yield return new WaitForEndOfFrame();
}
copperBar += 1 * multiplier;
if (copper < copperBarValue)
{
button.SetActive(true);
break;
}
}
}
也将等待时间放在括号中。
public void Start() => StartCoroutine(CraftCopperBar(5f));
如何停止协程?
这也是一种停止协程的方法。
public Coroutine craftCoroutine;
public void Start()
{
craftCoroutine = StartCoroutine(CraftCopperBar(5f));
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.S)) StopCoroutine(craftCoroutine);
}
我目前正在学习如何创建进度条,但我 运行 遇到了问题。我不确定如何在我的 CraftCopperBar 脚本中引用 运行ning 计时器进行更新。或者如果我的想法有误请指正。
public IEnumerator CraftCopperBar()
{
while (copper >= copperBarValue)
{
button.SetActive(false);
copper -= copperBarValue;
yield return new WaitForSeconds(5f);
copperBar += 1 * multiplier;
if (copper < copperBarValue)
{
button.SetActive(true);
break;
}
}
public void Update()
progressBar.fillAmount = (float)(x / 5f);
在 IEnumerator
中升级到自动连续计时器。这是解决您问题的好方法,在以下代码中,您不再需要 Update 事件来填充进度。
public IEnumerator CraftCopperBar(float waitTime)
{
while (copper >= copperBarValue)
{
copper -= copperBarValue;
button.SetActive(false);
var timer = 0f;
while (timer <= waitTime)
{
timer += Time.deltaTime;
progressBar.fillAmount = timer / waitTime;
yield return new WaitForEndOfFrame();
}
copperBar += 1 * multiplier;
if (copper < copperBarValue)
{
button.SetActive(true);
break;
}
}
}
也将等待时间放在括号中。
public void Start() => StartCoroutine(CraftCopperBar(5f));
如何停止协程?
这也是一种停止协程的方法。
public Coroutine craftCoroutine;
public void Start()
{
craftCoroutine = StartCoroutine(CraftCopperBar(5f));
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.S)) StopCoroutine(craftCoroutine);
}