我正在尝试对精灵进行倒计时。倒计时将从 10 倒计时到零,并且不会附加到 Canvas,因此它不会在屏幕上保持静止。类似这样的所有教程都是针对 UI 的,并且不允许 3D 文本。有人对如何做到这一点有任何想法吗?
1 回答
0
有两种方法可以做到这一点:
- 在设置为 World Space 的单独画布上使用 UI 元素
- 使用 TextMesh 元素在世界中放置 3D 文本网格
我真的不知道每种方法的优缺点,因此请选择更容易实施的方法,如果遇到问题,请记住另一种方法。
如果您正在寻找详细的教程,任何解释如何弹出伤害数字的方法都将是相同的方法,只是使用不同的脚本告诉它要显示什么文本。YouTube 结果顶部有一些很好的教程。
至于倒计时脚本,它相当简单。
//put this script on a GameObject prefab with a TextMesh component, or a canvas element
public class Countdown : MonoBehaviour
{
public TextMesh textComponent; //set this in inspector
public float time; //This can be set in inspector for this prefab
float timeLeft;
public void OnEnable()
{
textComponent = GetComponent<TextMesh>(); //in case you forget to set the inspector
timeLeft = time;
}
private void Update()
{
timeLeft -= Time.deltaTime; //subtract how much time passed last frame
textComponent.text = Mathf.CeilToInt(timeLeft).ToString(); //only show full seconds
if(timeLeft < 0) { gameObject.SetActive(false); } //disable this entire gameobject
}
}
于 2020-05-25T20:55:53.270 回答