怎么让unity3d的2d小游戏的分数用图片显示。

    我们在平常制作2d小游戏的时候一般有一个分数的显示。对于用UGUI的童鞋来说,我们一般用text这个组件来实时显示分数。然后在Text组件上用上特殊的字体和OUTLine组件给一个描边。

下面的这段代码很简单的通过美术提供的图片将分数显示出来。

using UnityEngine;

using System.Collections;

using UnityEngine.UI;

public class NumberChange : MonoBehaviour 

{    

public  Image AAA;   

 public  Image BBB;  

  public  Image CCC;  

  public  Image DDD;   

 public GameObject a, b, c, d;  

  public int number=0;

// Use this for initialization

void Start ()    

{        AAA.GetComponent().showMaskGraphic = true;   

     BBB.GetComponent().showMaskGraphic = true;   

     CCC.GetComponent().showMaskGraphic = true; 

       DDD.GetComponent().showMaskGraphic = true;     

     a.gameObject.SetActive(false);   

     b.gameObject.SetActive(false);   

     c.gameObject.SetActive(false);    

    d.gameObject.SetActive(false);      

              }// Update is called once per frame

void Update ()    {         

     // int i = number;        //if (number == i) AAA.GetComponent().sprite = Resources.Load(i.ToString());           

   int a = number % 10;//各位 

       int b = (number / 10)%10;//shiwei     

   int c = number / 100 % 10;//baiwei   

     int d = number / 1000 & 10;       

 AAA.GetComponent().sprite = Resources.Load(a.ToString());     

  BBB.GetComponent().sprite = Resources.Load(b.ToString());    

   CCC.GetComponent().sprite = Resources.Load(c.ToString());    

  DDD.GetComponent().sprite = Resources.Load(d.ToString());

}

public void addsele_click()

{

number+=1;

if (number > 0) a.gameObject.SetActive(true);

if (number > 9) b.gameObject.SetActive(true);

if (number > 99) c.gameObject.SetActive(true);

if (number > 999) d.gameObject.SetActive(true);

}

}代码看着有点乱,其实很简单。有几个注意的事项。

1:要加载的图片数字要放在Resources文件夹下(资源加载的方法),如果你将图片放在了Resource层的image里面。只要将代码改成AAA.GetComponent().sprite Resources.Load((“Resource”/image)a.ToString());

2:由于图片资源的原因。我给每个图片加了一个Mask组件。

你可能感兴趣的:(怎么让unity3d的2d小游戏的分数用图片显示。)