Unity使用協程技術制作倒計時器


先上效果圖

圖片資源來自http://www.51miz.com/

1.素材准備

在http://www.51miz.com/搜索png格式的數字圖片,用Unity自帶的圖集制作工具,進行分割。Container是一個Image,很簡單就不細說了。

2.素材准備好,就制作UI了。

3.前戲做好就可以擼代碼了。

using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace View
{


    public class MyTimer : MonoBehaviour
    {
        public float timeDelay = 1f;                                            //時間間隔
        public Sprite[] numbersImage;                                           //替換的圖片
        public Image numbersContainer;                                          //顯示圖片的容器
      //  private bool _onOff = true;                                             //開關
      //  private short curImageIndex = 0;                                      //當前播放的圖片編號

        private void Start()
        {
            StartCoroutine("StartTimer");
        }
        /// <summary>
        /// 使用協程等待,替換圖片
        /// </summary>
        /// <returns></returns>
        private IEnumerator StartTimer()
        {
            int index = 0;                                                       //當前播放的圖片編號
            while (index < (numbersImage.Length))
            {
                numbersContainer.sprite = numbersImage[index];                  //替換圖片
                yield return new WaitForSeconds(timeDelay);
                ++index;
            }
        }
    }

    
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM