尝试找到一种方法将计时器添加到我的代码中,然后用计时器不断循环它。例如,尝试通过单击按钮来制作物品,然后等待 5 秒以使其制作,然后只要我有材料,它就会自动开始再次制作,依此类推。我环顾四周的教程,但未能找到我一直在寻找的东西。这是我想要循环的代码:
public double copper;
public double copperBar;
public double copperBarValue;
public double multiplier;
public void Start()
{
copperBar = 0;
copperBarValue = 5;
copper = 0;
multiplier = 1;
}
**public void FurnaceInteraction()
{
if (copper >= copperBarValue)
{
copper -= copperBarValue;
copperBar += 1 * multiplier;
}
}**
这将彻底解决您的问题。您只能在 while 中放置一个条件。
private void Start() => StartCoroutine(Run());
public bool youHaveMaterials;
public IEnumerator Run()
{
while (youHaveMaterials) // repeat time until your materials end
{
Debug.Log("Do Crafting..");
yield return new WaitForSeconds(5);
}
}
IEnumerator
是一个基于时间的函数,可以支持等待时间本身。While
只要满足条件,也会返回代码。的组合wait
`while 会导致创建者在每次满足条件时创建项目,然后等待 5 秒。它会从您仍然拥有材料的地方重建。
例如..
在下面的代码中,用 2 个铁杆,我们还可以制作 2 把剑。赶紧跑StartCoroutine
当你的角色要去锻造时,例如......
private void Start() => StartCoroutine(CraftSword());
public int Iron = 2;
public IEnumerator CraftSword()
{
Debug.Log("Start Crafting..");
while (Iron > 0)
{
Iron--;
Debug.Log("Sword Created!!" + "Remaining Iron: " + Iron);
if (Iron == 0) break;
yield return new WaitForSeconds(.5f);
Debug.Log("Wait 0.5 second.");
}
Debug.Log("My Irons End..");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)