Unity血条缓冲效果

用到四张图片,按照以下层级:
1.血条背景
2.血条缓冲效果
3.血条
4.血条外框

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

public class Damage : MonoBehaviour
{
    
    
    public float maxHP = 100;
    public float currentHP=100;
    private Image HP_Effect;
    private Image HP_Point;

    private void Awake()
    {
    
    
        currentHP = maxHP;
        HP_Point = GameObject.Find("Canvas/HP_Point").GetComponent<Image>();
        HP_Effect = GameObject.Find("Canvas/HP_Effect").GetComponent<Image>();
       
    }

    IEnumerator Hurt()
    {
    
    
        HP_Point.fillAmount = currentHP / maxHP;
        while (HP_Point.fillAmount <= HP_Effect.fillAmount)
        {
    
    
            HP_Effect.fillAmount -= 0.003f;
            yield return new WaitForSeconds(0.005f);
        }
        if (HP_Point.fillAmount > HP_Effect.fillAmount)
            HP_Effect.fillAmount = HP_Point.fillAmount;
    }

    public void Attack() {
    
    
        if (currentHP <= 0)
            return;
        currentHP -= 10;
        StartCoroutine(Hurt());
    }
}

想要看到效果可以把脚本挂在按钮上,运行Attack()即可;

猜你喜欢

转载自blog.csdn.net/memory_MM_forever/article/details/118389286