您好,欢迎来到爱玩科技网。
搜索
您的当前位置:首页unity伤害数字显示,最简单简洁的方法

unity伤害数字显示,最简单简洁的方法

来源:爱玩科技网

想做一个伤害数字显示,结果发现搜索结果都太繁琐,我觉得这么简单的功能不应该这么麻烦啊!
所以做一个教程
在怪物身上创建一个画布,设置渲染模式为世界空间,然后重置,修改大小,高度高一点,因为text只有在画布范围内才显示,我们要做飘升数字!
在画布下创建一个text修改属性为自己喜爱!我的设置为

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

public class FacingCamera : MonoBehaviour
{
    Transform[] childs;
    void Start()
    {
        childs = new Transform[transform.childCount];
        for (int i = 0; i < transform.childCount; i++)
        {
            childs[i] = transform.GetChild(i);
        }
    }

    void Update()
    {
        for (int i = 0; i < childs.Length; i++)
        {
            childs[i].rotation = Camera.main.transform.rotation;
        }
    }
}

HudText脚本挂在text上,把text拖为预制体

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

public class HudText : MonoBehaviour
{
    Text text;
    void Start()
    {
        text= GetComponent<Text>();


        lowercenter();




    }
    public void lowercenter()
    {
        text.alignment = TextAnchor.LowerCenter;
        for (int i = 28; i >14;i--)
        {
            text.fontSize = i;
        }
        Invoke("middlecenter", 0.2f);
    }
    public void middlecenter()
    {
        text.alignment = TextAnchor.MiddleCenter;
        for (int i = 14; i > 7; i--)
        {
            text.fontSize = i;
        }
        Invoke("uppercenter", 0.2f);
    }
    public void uppercenter()
    {
        text.alignment = TextAnchor.UpperCenter;
        for (int i = 7; i > 0; i--)
        {
            text.fontSize = i;
        }
        Destroy(gameObject, 0.2f);
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}

在怪物身上写个方法

public GameObject texthud;//text预制体
    public Transform texthudte;//画布位置
    
    public void shoushang(int damage)
    {
       GameObject go = Instantiate(texthud, texthudte, false);
        go.GetComponent<Text>().text="-"+damage.ToString();
    }

在造成伤害处调用这个方法,参数为伤害值!

说了很多废话,其实主要内容非常少,非常简单,其他的根据需要看!

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- aiwanbo.com 版权所有 赣ICP备2024042808号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务