1、首先我们用unity3d打开场景。
2、在菜单中选择创建一个Text文本组件。
3、然后我们设置文本的显示,文字大小样式等。
4、接着在资源文件夹中创建一个 Time_down 脚本文件。
5、把脚本放到Text上面。
6、在脚本中创建一个count_down变量赋值为10,就是倒计时10秒,在start方法中初始化Text数值为count_down。
7、创建一个Time_count方法,在start方法中使用InvokeRepeating方法,在2秒后每1秒执行一次Time_count方法,在里面减去1并更新Text文本显示,当数值小于0时就停止CancelInvoke调用。void Start () { GetComponent<UnityEngine.UI.Text> ().text = ""+count_down; InvokeRepeating ("Time_count", 2.0f, 1.0F); } void Time_count() { if (count_down > 0) { count_down--; GetComponent<UnityEngine.UI.Text> ().text = "" + count_down; } else { CancelInvoke (); } }
8、现在我们运行游戏场景等待两秒后就会显示倒计时了。