Unity動態更換圖片


博主寫的是五秒倒計時后開始游戲,用圖片顯示倒計時數字,由於初學Unity所以方法可能不太好,但不失為一種解決方法。

using UnityEngine;
using System.Collections;
using UnityEngine.UI;//別忘加了

public class GameController : MonoBehaviour {

public Sprite five;
public Sprite four;
public Sprite three;
public Sprite two;
public Sprite one;

public Image CDownImage;

bool gameStarted;

float pTime;

void Start(){
  pTime = 0.0f;
  gameStarted=false;
}

void FixedUpdate(){
  if (gameStarted == false) {

  if (pTime-4.0f>=0)
    cDownImage.overrideSprite = one;
  else if (pTime-3.0f>0)
    cDownImage.overrideSprite = two;
  else if (pTime-2.0f>=0)
    cDownImage.overrideSprite = three;
  else if (pTime-1.0f>=0)
    cDownImage.overrideSprite = four;
  else if (pTime-0.0f>=0)
    cDownImage.overrideSprite = five;

  pTime += Time.deltaTime;

  if (pTime-5.0f>=0) {//倒計時結束
    pTime = 0.0f;
    gameStarted = true;
    cDownImage.gameObject.SetActive(false);
  }
  }

}

當然通過動態更改text也可以實現倒計時效果,不過由於字體限制顯示效果有限吧。


免責聲明!

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



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