Unty3d 个性化数字(Customized Numbers)

先根据需求做极限数值版的数字预制体“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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值