時間計時,分數滾動實現。

時間和圖片結合代碼。顯示結果爲分和秒。

    public int minite;//分
    public int  second;//秒
    public float AllTime;//總時間
    int ChangeTime;//改變時間
    public Sprite[] Timer;//存放數字圖片數組
    public GameObject[] Ti;//存放Ui
     public int FindNum(int num, int n)
    {
        int power = (int)Math.Pow(10, n);
        return (num - num / power * power) * 10 / power;
    }
 void Update () {
        AllTime -= Time.deltaTime;
        minite = (int)AllTime / 60;
        second = (int)AllTime % 60;
        if(second>=0&&second<60)
        {
            Ti[0].GetComponent<Image>().sprite = Timer[FindNum(second, 1)];
        }
        if (second >= 0 && second < 60)
        {           
            Ti[1].GetComponent<Image>().sprite = Timer[FindNum(second, 2)];
        }
        if(minite>=0&& minite < 10)
        {
            Ti[2].GetComponent<Image>().sprite = Timer[FindNum(minite, 1)];
        }
        if (minite >= 10 && minite < 60)
        {
            Ti[3].GetComponent<Image>().sprite = Timer[FindNum(minite, 2)];
        }
        if(AllTime<=40&& music)
        {
            BG.GetComponent<AudioSource>().clip = BgHigh;
            BG.GetComponent<AudioSource>().Play();
            music = false;
        }  
      }

分數滾動實現

    public Sprite[] score;//存放數字圖片
    static public int Score1; //當前分數
    static public int NowScore;//更新分數 
    float ChangeTime;//變化時間變量
    public GameObject[] Sco;//分數存放數組UI
    float timeKeep = 0.1f;//滾動時間
    void Start()
    {
        Score1 =0;
        NowScore = 0;
        ChangeTime = 0;
    }
    void Update()
    {
        if (Score1 < 0)
        {
            Score1 = 0;
        }
        if (NowScore != Score1)
        {
            ChangeTime += Time.deltaTime;
            if (ChangeTime > timeKeep)
            {
                if (NowScore < Score1)
                {
                    NowScore++;
                }
                if (NowScore > Score1)
                {
                    NowScore--;
                }
                ChangeScore(NowScore);
                ChangeTime = 0;
                int dis = Math.Abs(NowScore - Score1);
                timeKeep = 0.01f / (float)(dis % 10 + 1);//分數滾動時間間隔             
            }
        }
        public void ChangeScore(int Number)
    {
        char[] chars = Number.ToString().ToCharArray();//存放分數數組
        for (int i = 0; i < chars.Length; i++)
        {   
            int index = int.Parse(chars[chars.Length - i - 1].ToString());//得到變化數字下標
            Sco[i].GetComponent<Image>().sprite = score[index];//得到分數圖片
        }
        for (int i = 0; i < Sco.Length- chars.Length; i++)
        {
            Sco[Sco.Length-1-i].GetComponent<Image>().sprite = score[0];改變分數
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章