OnGUI Health Bar Scaling

时间:2014-06-01 15:00:16

标签: c# unity3d

我正在尝试使用OnGUI为2D游戏设置健康栏。我有一个外部纹理和一个内部纹理的仪表。我现在的设置部分工作。我能够改变内部仪表的比例,看起来它正在耗尽,但沿x的位置也在变化。这会导致笨拙的效果以及仪表在其容器外移动。

void Update ()
{
    score = EnemyScript.killCount * 100;
    powerScale -= 1; 
}

void OnGUI()
{
    //Score and Power Containter
    GUILayout.BeginArea(new Rect(Screen.width/8, Screen.height/8, 500, 200));
    GUILayout.Label("Score: " + score, style);
    GUI.DrawTexture(new Rect(-10,20,200,80),powerBoundry);
    GUILayout.EndArea();
    //Power Meter
    GUILayout.BeginArea(new Rect(Screen.width/8, Screen.height/8, 450, 200));
    GUI.DrawTexture(new Rect(-10,20,powerScale,80),powerFill, ScaleMode.ScaleAndCrop);
    GUILayout.EndArea();
}

是否可以将其固定到位?或者也许让它成为游戏对象的孩子来锚定它?

1 个答案:

答案 0 :(得分:2)

尝试使用DrawTextureWithTexCoords。

void OnGUI()
{
    GUILayout.BeginArea(new Rect(Screen.width / 8, Screen.height / 8, 500, 200));
    GUI.DrawTexture(new Rect(-10, 20, 200, 80), powerBoundry);
    GUI.DrawTextureWithTexCoords(new Rect(-10, 20, powerScale, 80), powerFill, new Rect(0f, 0f, (float)powerScale / 200f, 1f));
    GUILayout.EndArea();
}