Unity实现DNF血条效果

1 篇文章 0 订阅
1 篇文章 0 订阅

unity实现类似dnf血条效果,话不多说,先上效果,再上代码:

 

using UnityEngine;
using UnityEngine.UI;

public class HPBar : MonoBehaviour
{
#pragma warning disable 0649
    /// <summary>
    /// 下一层血条图片
    /// </summary>
    [SerializeField] private Image nextBar;
#pragma warning disable 0649
    /// <summary>
    /// 残影图片
    /// </summary>
    [SerializeField] private Image ghostBar;
#pragma warning disable 0649
    /// <summary>
    /// 血条图片
    /// </summary>
    [SerializeField] private Image healthBar;
#pragma warning disable 0649
    /// <summary>
    /// 血量文本
    /// </summary>
    [SerializeField] private Text healthText;
#pragma warning disable 0649
    /// <summary>
    /// 血条颜色循环列表
    /// </summary>
    [SerializeField] private Color[] colors;
#pragma warning disable 0649
    /// <summary>
    /// 保证最后一条血是红色
    /// </summary>
    [SerializeField] private Color redColor;
    /// <summary>
    /// 血量值
    /// </summary>
    [SerializeField] private float currentValue;
#pragma warning disable 0649
    /// <summary>
    /// 怪物信息
    /// </summary>
    [SerializeField] private Text title;

    private int plies;
    private readonly float singlePlyValue = 130;
    private float barFillAmount;
    private int index;
    private int counter;
    private Image ghostImage;

    public static HPBar Ins;

    private void Awake()
    {
        Ins = this;
        ghostImage = ghostBar.GetComponent<Image>();
        gameObject.SetActive(false);
    }

    public void SetDamage(float curValue, float maxValue, int lv, string name)
    {
        gameObject.SetActive(curValue > 0);
        if (curValue < 0) curValue = 0;
        //设置值
        plies = (int)(maxValue / singlePlyValue);
        counter = (int)(curValue / singlePlyValue);
        ghostBar.fillAmount = healthBar.fillAmount;
        currentValue = curValue;
        //设置颜色
        Color color = healthBar.GetComponent<Image>().color;
        ghostImage.color = new Color(color.r, color.g, color.b, 1);
        int i = (int)(currentValue / singlePlyValue);
        nextBar.gameObject.SetActive(i != 0);
        i = plies - i - 1;
        i %= colors.Length;
        i = i < 0 ? 0 : i;
        healthBar.GetComponent<Image>().color = curValue < singlePlyValue ? redColor : colors[i];
        nextBar.GetComponent<Image>().color = curValue < singlePlyValue * 2 ? redColor : colors[(i + 1) % colors.Length];
        if (i != index)
        {
            ghostImage.color = colors[i];
            ghostBar.fillAmount = 1;
            index = i;
        }
        //设置位置
        barFillAmount = currentValue % singlePlyValue / singlePlyValue;
        if (barFillAmount == 0)
            barFillAmount = currentValue != maxValue ? 0.001f : 1;
        healthText.text = $"{counter}/{plies}";
        title.text = $"Lv {lv} {name}";
    }

    private void Update()
    {
        Color color = ghostImage.color;
        color.r += 0.38f;
        color.g += 0.38f;
        color.b += 0.38f;
        color.a = 0.2f;
        ghostImage.color = Color.Lerp(ghostImage.color, color, Time.deltaTime * 10);
        healthBar.fillAmount = barFillAmount;
        if (ghostImage.color.a < 0.25f)
            ghostBar.fillAmount = Mathf.Lerp(ghostBar.fillAmount, barFillAmount, Time.deltaTime * 10);
    }
}

 面板设置:

 

 unity开发dnf相关视频:unity 无cd无敌版dnf 手游_哔哩哔哩_bilibili

 源码(QQ):1141243331

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tiny-Q

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值