先根据需求做极限数值版的数字预制体“NumberPre”,如最大的位数是 9 ,
然后将数字的状态设置为不激活。
放在“Resources”包下,以便整个游戏场景都可以引用。
如果那个场景需要数字,直接将预制体“NumberPre"拖到需要的位置,并可根据需求修改名字,如,我此处记录的是金币的数量 coinnum
接下来就是更新的问题,新增脚本 CustomizedNumbers 放在公共脚本区域,脚本里有设置和获取两个静态方法。
public class CustomizedNumbers : MonoBehaviour
{
/*
* 通过传过来的数字Transform来修改
* 根据nuenum和numbertype类更新
*/
public static void SetCustomNumbers(Transform numberpre, int newnum, int numbertype)
{
//为预制体绑定一个脚本,记录数值
numberpre.GetComponent<Coin>().SelfNum = newnum;
string numstr = newnum + "";
int max = numstr.Length;
string resname = "numbertype_" + numbertype + "_";
foreach (Transform cc in numberpre)
{
cc.gameObject.SetActive(false);
}
for (int i = 1; i <= max; i++)
{
Transform c = numberpre.FindChild(i.ToString());
c.gameObject.SetActive(true);
string thisnum = numstr.Substring(i - 1, 1);
c.GetComponent<tk2dSprite>().SetSprite(resname + thisnum);
}
}
/*
* 得到个性化数字的数值
*/
public static int GetCustomNumbers(Transform numberpre)
{
return numberpre.GetComponent<Coin>().SelfNum;
}
}