Unity3d协程实现倒数计时
2016-05-17 14:57
405 查看
Unity3d协程的知识,不了解的同学可以在网上查找一下相关资料或者看一下Unity3D协程介绍 以及 使用。
下面介绍Unity3d协程实现倒数计时,实现代码:
在Inspector面板为Texture赋值,直接将图上拖上去就可以了。
运行效果:
这样就完成了协程实现倒数计时。不懂的同学可以私聊哦。
下面介绍Unity3d协程实现倒数计时,实现代码:
public class GameManager : MonoBehaviour { private bool _BoolIsDisplayNumber = false;//是否显示数字 private bool _BoolIsDisplayGo = false;//是否显示go private float _IntDisplayNumber; private string _StringDisplayNumber; public Texture TextNum3;//要显示的图片3 public Texture TextNum2; public Texture TextNum1; public Texture TextGo; void Start() { StartCoroutine(DisplayGameStartCountDown()); } IEnumerator DisplayGameStartCountDown() { yield return new WaitForEndOfFrame(); //显示3 _BoolIsDisplayNumber = true; _IntDisplayNumber = 3; yield return new WaitForSeconds(1f); _BoolIsDisplayNumber = false; yield return new WaitForSeconds(0.5f); //显示2 _BoolIsDisplayNumber = true; _IntDisplayNumber = 2; yield return new WaitForSeconds(1f); _BoolIsDisplayNumber = false; yield return new WaitForSeconds(0.5f); //显示1 _BoolIsDisplayNumber = true; _IntDisplayNumber = 1; yield return new WaitForSeconds(1f); _BoolIsDisplayNumber = false; yield return new WaitForSeconds(0.5f); //显示go _BoolIsDisplayGo = true; _StringDisplayNumber = "go"; yield return new WaitForSeconds(1f); _BoolIsDisplayGo = false; yield return new WaitForSeconds(0.5f); } void OnGUI() { //使用GUI显示图片 if (_BoolIsDisplayNumber) { if (_IntDisplayNumber == 3) //显示3 { GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum3.width / 2, Screen.height / 2 - TextNum3.height / 2, TextNum3.width, TextNum3.height), TextNum3); } else if (_IntDisplayNumber == 2) //显示2 { GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum2.width / 2, Screen.height / 2 - TextNum2.height / 2, TextNum2.width, TextNum2.height), TextNum2); } else if (_IntDisplayNumber == 1) //显示1 { GUI.DrawTexture(new Rect(Screen.width / 2 - TextNum1.width / 2, Screen.height / 2 - TextNum1.height / 2, TextNum1.width, TextNum1.height), TextNum1); } } if (_BoolIsDisplayGo) { if (_StringDisplayNumber == "go") //显示go { GUI.DrawTexture(new Rect(Screen.width / 2 - TextGo.width / 2, Screen.height / 2 - TextGo.height / 2, TextGo.width, TextGo.height), TextGo); } } } }具体实现代码如上所示,并不复杂。
在Inspector面板为Texture赋值,直接将图上拖上去就可以了。
运行效果:
这样就完成了协程实现倒数计时。不懂的同学可以私聊哦。
相关文章推荐
- Unity3d基础知识&面试题(重要)
- Unity AssetDatabase和Resources资源管理
- Unity 脚本 自动添加 脚本信息
- Unity3D中自带事件函数的执行顺序
- Unity3D编辑器插件编写
- unity3D 协程如何模拟Update
- JsonFX Unity3D 如何使用JsonFX
- Unity3D 之NGUI各种脚本及应用
- Unity逻辑热更新
- Unity3D 鼠标以及触屏移动、缩放控制器(相机)
- UGUI -(unity3d 5)判断是否点击在UI 上 Bug,IsPointerOverGameObject()在移动输入模式检测失败
- Unity UGUI —— 鼠标穿透UI问题(Unity官方的解决方法)
- unity 3D 安卓上退出确认的方法
- Unity 画图效果
- UNITY3D android 启动其他程序,启动apk C#代码
- 【UNITY填坑日记】unity图片资源优化神器dither4444
- 【UNITY填坑日记】UGUI不消耗DRAW CALL的EventTrigger接收器
- Unity 脚本生命周期流程图
- unity中结合oculus制作VR场景控制相机的移动的三种方式
- 基于Unity的多线程之间的事件派发