Unity は血液バーの速度低下の効果を認識

翻訳者

1. 3 つの新しい Image ゲーム オブジェクトを作成し、BackGround、Mid、Front という名前を付けます。
画像の説明を追加してください

2. 中央画像と前面画像に元の画像を配置し、画像タイプを塗りつぶしとして選択し、塗りつぶしモードを水平 (水平方向に塗りつぶす) に設定し、塗りつぶしの原点を左に設定します。

画像の説明を追加してください

効果

画像の説明を追加してください

コード

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

public class HPUIDynamic : MonoBehaviour
{
    
    
    public float currentHP;
    public float maxHP;
    public float speed;

    [SerializeField] private Image mid;

    [SerializeField] private Image front;


    // Start is called before the first frame update
    void Start()
    {
    
    
        maxHP = 100f;
        currentHP = maxHP;

        front.fillAmount = currentHP;
        mid.fillAmount   = currentHP;
    }
    
    // Update is called once per frame
    void Update()
    {
    
    
        mid.fillAmount = Mathf.Lerp(mid.fillAmount, front.fillAmount, Time.deltaTime * speed);

        if(Input.GetKeyDown(KeyCode.Alpha1)){
    
    
            currentHP = Mathf.Clamp(currentHP - 10, 0, maxHP);
            front.fillAmount = currentHP / maxHP;
        }

        if(Input.GetKeyDown(KeyCode.Alpha2)){
    
    
            currentHP = Mathf.Clamp(currentHP + 10, 0, maxHP);
            front.fillAmount = currentHP / maxHP;
        }

    }
}

ビデオをご希望の場合は(私もこのビデオを学びました)、ここをクリックしてください

おすすめ

転載: blog.csdn.net/blastospore/article/details/130692927