0
\$\begingroup\$

I have this GUI to graphically display a variable similar to the health in a game. The variable 'test' goes up and down during gameplay.

To show this I have the following onGUI ():

public GUISkin btnskin1;
public Texture2D emptyTex;
public Texture2D fullTex;
public Vector2 pos = new Vector2(20,40);
public Vector2 size = new Vector2(60,20);

void OnGUI()
{
    float test;

    GUI.skin = btnskin1;

    if (LoadDiagram.diagramaCarga.TryGetValue (TimeManager.gametime, out test)) 
    {       
        GUI.BeginGroup (new Rect (pos.x, pos.y, size.x, size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), emptyTex);

        //draw the filled-in part:
        GUI.BeginGroup (new Rect (0, 0, size.x * test, size.y));
        GUI.Box (new Rect (0, 0, size.x, size.y), fullTex);
        GUI.EndGroup ();
        GUI.EndGroup ();
    }
}

In the game scene I attached the empty bar to the emptyTex texture, and the full bar to the fullTex texture. When I play the game, he bar is always full. I have not inserted the minimum and maximum of the variable test because I don't know where, but I guess that may be necessary.

Any hints?

\$\endgroup\$
  • \$\begingroup\$ What is your test value during gameplay? Is it changing or not? \$\endgroup\$ – Hamza Hasan Jan 6 '16 at 11:59
  • \$\begingroup\$ maxmium of 6F and minimum of 3F \$\endgroup\$ – ClaudioA Jan 6 '16 at 12:01
  • \$\begingroup\$ Got the problem, I need to make my variable test change from 0 to 1. \$\endgroup\$ – ClaudioA Jan 6 '16 at 12:05
  • \$\begingroup\$ you should divide your value with 6. Like size.x * (test/6.0f) instead of size.x * test \$\endgroup\$ – Hamza Hasan Jan 6 '16 at 12:05
  • 1
    \$\begingroup\$ When this is a new project and not a pre-unity5 legacy project, you might want to consider using the new canvas-based UI system. Using the OnGUI event and the GUI class is obsolete. \$\endgroup\$ – Philipp Jan 6 '16 at 13:14
3
\$\begingroup\$

Actually your trying to use percentage but no with right way. If you want to play with values of slider like stuff then then multiply the FullValue with percentage. And you can get useable value from percentage with dividing it by 100.

For example

  • 100% = 1
  • 50% = 0.5
  • 10% = 0.1
  • 0% = 0

In this line of code you are using GUI.BeginGroup (new Rect (0, 0, size.x * test, size.y));

Suppose size.x = 500 and test = 6 then size.x * test will become 3000, which is greater than 500,greater than the actual size.x.

You should treat these type scenario with percentage values.

In your case you can do this by,

GUI.BeginGroup (new Rect (0, 0, size.x * (test/6.0f), size.y));
// Where 6 is the maximum value


// For test = 6
size.x * (test/6.0f)` will give you => `500 * 1 // this means full/maximum.


// For test = 3
size.x * (test/6.0f)` will give you => `500 * 0.5 // this means 250 (Half).

// So on...

Hope you got my point.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.