tradutor
1. Crie três novos objetos de jogo de imagem e nomeie-os como BackGround, Mid, Front
2. Coloque uma imagem original para as imagens do meio e da frente, selecione o Tipo de imagem como Preenchido, defina o Modo de preenchimento como Horizontal (preencher horizontalmente) e defina a Origem do preenchimento como Esquerda
Efeito
o código
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;
}
}
}
Se preferir vídeo (eu também aprendi esse vídeo) clique aqui