翻訳者
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;
}
}
}
ビデオをご希望の場合は(私もこのビデオを学びました)、ここをクリックしてください