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也可以实现倒计时效果,不过由于字体限制显示效果有限吧。

原文地址:https://www.cnblogs.com/fatherloveyou/p/4532568.html