制作自己的HUD

最近在写一个单机的小游戏,用过UGUI的HUD,并不知道可不可以自定义数字的样式,好像没有吧....索性就自己写个。功能基本是实现了,代码还有待优化,最初是想着用少量的预制不用频繁的实例就可以实现,但那样的话比如伤害数字是555555,这同样的数字出现了6个,就不大好办了,所以就改成现在这样了.....

好了上代码,有时间的朋友可以帮忙完善一下....

 using UnityEngine; 

using UnityEngine.UI;
using System.Collections.Generic;
using System.Collections;


public class CreateHit : MonoBehaviour
{
    public Sprite[] monsterNumber;//怪物的
    public Sprite[] playerNumber;//玩家的
    int fontSpace = 30;//数字与数字之间的距离 
    int height = 60;//数字距离怪物或者玩家的高度


    public GameObject baseNum;//预制体,就是一个image,用来显示数字
    public Transform textParent;//直接实例是在UI外,所以要放在UI里才能被看到


    //注意这个方法是协程,显示伤害数值的时候就调用这个方法
    public IEnumerator NewHud(Transform target, float hit, bool isPlayer)//目标的组件,受到的伤害值,是否玩家
    {
        yield return new WaitForSeconds(0.4f);//受到伤害之后延迟一下,看起来比较自然
        string number = hit.ToString();//把数字转换为字符串
        int numberLength = number.Length;//获取字符串的长度,也就是有几位数
        Vector3 screen = Camera.main.WorldToScreenPoint(target.transform.position);//获取目标在屏幕上的位置 
        float xStart;//计算第一位数字的X坐标
        if (numberLength % 2 == 1)//是奇数还是偶数(数字的长度)
            xStart = screen.x - (numberLength / 2 + 1.5f - 0.5f) * fontSpace;//计算位数为奇数时X的初始值
        else
            xStart = screen.x - (numberLength / 2 + 1 - 0.5f) * fontSpace;//计算位数为偶数时X的初始值
        Vector2 curPos = new Vector3(xStart, screen.y + height); //计算最高位数x坐标


        List<int> num = new List<int>();//截取每一位数字放入列表
        List<Vector2> pos = new List<Vector2>();//每一位数字的坐标
        List<GameObject> game = new List<GameObject>();//转换为图片的数字物体
        for (int i = 0; i < numberLength; i++)
        {
            num.Add(int.Parse(number.Substring(i, 1)));//截取每一个数字放入列表
            curPos.x += fontSpace;//计算数字之间的位置 
            pos.Add(curPos);//坐标也放入列表
            game.Add(Instantiate(baseNum, curPos, Quaternion.identity) as GameObject);//实例的物体也加入列表
            ChangeIcon(num[i], i, game, isPlayer);//将数字替换为相应的图片
        }
        for (int i = 0; i < numberLength; i++)//需要单独写一个循环,否则高度会不一致
        {
            StartCoroutine(UpNumber(game[i], pos[i]));//使实例的物体上升
            game[i].transform.SetParent(textParent);//放在UI里才能被看到
        }
    }


    void ChangeIcon(int num, int index, List<GameObject> game, bool isPlayer)//设置并打开
    {
        if (isPlayer)//
            game[index].GetComponent<Image>().sprite = playerNumber[num];
        else
            game[index].GetComponent<Image>().sprite = monsterNumber[num];
        game[index].SetActive(true);
    }


    IEnumerator UpNumber(GameObject obj, Vector3 target)//使替换的数字图片上升
    {
        yield return new WaitForSeconds(0.2f);
        target.y += 100;//在原基础上升100个单位
        iTween.MoveTo(obj, target, 1);//用tween将数字移动到指定位置
        yield return new WaitForSeconds(1);//1秒钟后隐藏或者销毁
        obj.SetActive(false);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值