0

我的Unity游戏中有一个健康栏,它被实现为GUITexture从红色到绿色的渐变图像。

在此处输入图像描述

现在我可以将它的宽度从最大宽度减小到 0,但仍然是缩放渐变。

public void UpdateHealthBar(int hitPoints) {
    healthBar.pixelInset = new Rect(
        healthBar.pixelInset.x, healthBar.pixelInset.y,
        3* hitPoints, healthBar.pixelInset.height);
}

在此处输入图像描述

但我想在游戏进程中隐藏(透明)这个健康栏的正确部分。

在此处输入图像描述

我怎样才能做到这一点?谢谢!

4

2 回答 2

1

或者你可以使用 GUI.DrawTextureWithTexCoords()

public Texture2D texture;
public float hp = 100f;
public const float hpMax = 100f;

void OnGUI()
{
    GUI.DrawTextureWithTexCoords (new Rect (0, 0, hp, 20), texture, new Rect (0f, 0f, hp/hpMax, 1f));
}
于 2014-05-07T08:23:20.837 回答
0

正如巴特建议的那样,您可以使用 GUI.BeginGroup

这是一个例子

public Texture2D texture;
public Rect textureCrop = new Rect( 0.1f, 0.1f, 0.5f, 0.25f );
public Vector2 position = new Vector2( 10, 10 );

void OnGUI()
{
    GUI.BeginGroup( new Rect( position.x, position.y, texture.width * textureCrop.width, texture.height * textureCrop.height ) );
    GUI.DrawTexture( new Rect( -texture.width * textureCrop.x, -texture.height * textureCrop.y, texture.width, texture.height ), texture );
    GUI.EndGroup();
}
于 2014-05-07T07:24:35.513 回答