traductor
1. Cree tres nuevos objetos de juego de imágenes y nómbrelos como Fondo, Medio, Frente
2. Coloque una imagen original para las imágenes Mid y Front, seleccione el Tipo de imagen como Relleno, configure el Modo de relleno como Horizontal (rellenar horizontalmente) y configure el Origen de relleno como Izquierda
Efecto
el 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;
}
}
}
Si prefieres video (también aprendí este video) haz clic aquí