我试图获得一个健康条,以显示在我的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;
}
发布于 2018-04-02 02:20:39
您应该能够使用UI滑块来达到这个目的。
滑块组件是控制填充、句柄或两者的可选选项。填充在使用时从最小值到当前值,而句柄在使用时遵循当前值。
来源:https://docs.unity3d.com/ScriptReference/UI.Slider.html
此外,这里有一个关于UI滑块的视频教程,它可能很有用:
UI滑块可用于各种用途,从设置菜单到健康栏。
https://unity3d.com/learn/tutorials/topics/user-interface-ui/ui-slider
https://stackoverflow.com/questions/49604686
复制