无法弄清楚如何让Unity UI正确扩展

时间:2018-04-02 02:46:47

标签: c# unity3d unity-ui

我正在尝试在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;

}

1 个答案:

答案 0 :(得分:0)

您应该可以使用UI滑块来实现此目的。

  

滑块组件是可选择的,用于控制填充,手柄或两者。 填充使用时,从最小值跨越到当前值,而句柄在使用时会跟随当前值。

来源:https://docs.unity3d.com/ScriptReference/UI.Slider.html

此外,这是UI滑块上的视频教程,可能很有用:

  

UI滑块可用于从设置菜单到健康栏的各种用途。

https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-slider