我试图在 Unity 的 UI 上显示一个健康栏,但无法弄清楚如何让缩放正常工作。我需要最左边的值为 0,所以当 currenthealth=0 时,条形消失,当 currenthealth = maxhealth 时,它应该等于 maxhealth。我当前的代码如下。
public RectTransform this_rect;
public float left = 3f;
public float right = 0f;
public float posY = -50f;
public float height = 20f;
public int MaxHealth = 3;
public int currentHealth = 3;
void Update()
{
this_rect.anchorMin = new Vector2(0, 1);
this_rect.anchorMax = new Vector2(1, 1);
Vector2 temp = new Vector2(left, posY - (height / 2f));
this_rect.offsetMin = temp;
temp = new Vector3(-right, temp.y + height);
this_rect.offsetMax = temp;
// currentHealth = (int)(left + 1 * MaxHealth);
right = -currentHealth + MaxHealth;
}