zoukankan      html  css  js  c++  java
  • unity3d游戏开发 —— 倒计时

    用GUI实现倒计时:

    方法一:利用Time.time,这是以秒计算到游戏开始的时间。也就是说,从游戏开始到到现在所用的时间。

    代码如下:

    using UnityEngine;
    using System.Collections;
    
    public class displayTime : MonoBehaviour
    {
        //数值
        public string myStringScore;
    
        public float x = 85;
        public float y = 19;
        public float scale = 1;
    
        public Color myColor;
        //定义数组
        public Texture[] myNumber = new Texture[10];
        //public Texture Tex;
        //
        private int index = 0;
        private int width = 30;
        private int height = 30;
        public float allTime = 100;
        public float countTime;
    
    
        void Start()
        {
            allTime = allTime + Time.time;
        }
        void FixedUpdate()
        {
            countTime = allTime - Time.time;
            //print(countTime);
            myStringScore = countTime.ToString();
            if (countTime <= 0)
            {
                //游戏结束之后进行设置
                countTime = 0;
                //Application.LoadLevelAdditive(4);
                //Application.Quit();//退出游戏
                // print("countTime");
                // return;
            }
            else
            {
                return;
            }
        }
    
        void Update()
        {
    
        }
    
        // Use this for initialization
        void OnGUI()
        {
            GUI.color = myColor;
            if (myStringScore != null)
            {
                for (int i = 0; i < myStringScore.Length; i++)
                {
                    if (myStringScore.Substring(i, 1) == ".")
                    {
                        break;
                    }
                    GUI.DrawTexture(new Rect(x + i * scale * width, y, scale * width, scale * height), myNumber[int.Parse(myStringScore.Substring(i, 1))], ScaleMode.StretchToFill, true, 0);
                    //GUI.DrawTexture(new Rect(x + i * scale * width, y, scale * width, scale * height),myNumber[myStringScore[i]-48]);
                }
            }
        }
    }

    绑定代码到Camera上,设置倒计时10个数图片:


     方法二:使用yield return new WaitForSeconds(1);等待1秒

    using UnityEngine;
    using System.Collections;
    
    public class CoolTime : MonoBehaviour {
        // Use this for initialization
        void Start () {
        }
        int CoolTimes = 100;
        // Update is called once per frame
        void Update () {
    
        }
        void OnGUI()
        {
            GUILayout.Label(CoolTimes.ToString());
            if (GUILayout.Button("Begin"))
            {
                StartCoroutine(waitForOneSecond());
            }
        }
    
        public IEnumerator waitForOneSecond()
        {
            while (CoolTimes >= 0)
            {
                CoolTimes--;
                yield return new WaitForSeconds(1);   
            }
        }
    }
  • 相关阅读:
    modal 过程中添加动画
    SVSegmentedControl 标签页
    带输入框的UIAlertView
    Django小结
    译Step-by-Step Guide on Configuring Django-Userena
    使用国内镜像通过pip安装python 包
    pycryto实现AES加密解密算法
    notepad++搜索的一些东西
    [转]notepad++正则表达式替换字符串详解
    [转]Notepad++快捷键
  • 原文地址:https://www.cnblogs.com/martianzone/p/3372256.html
Copyright © 2011-2022 走看看