Search Unity

gui label

Discussion in 'Scripting' started by Euphoric, Jul 7, 2013.

  1. Euphoric

    Euphoric

    Joined:
    Sep 6, 2012
    Posts:
    46
    Hello, this may sound like a newbie question. Im looking to put a gui label ontop of a gui texture and was wondering how can I go about approaching this via javascript? I tried searching for a solution but could not find one. Any help to lead me in the right direction is appreciated. Thanks.
     
  2. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    in my opinion you should not mix the ongui and guitext(ure) stuff. you can also use a label to display a texture and then use it again at the same coordinates with the text you want to display.
     
  3. TheRaider

    TheRaider

    Joined:
    Dec 5, 2010
    Posts:
    2,250
    put the texture in your code before the label. That is all.
     
  4. Euphoric

    Euphoric

    Joined:
    Sep 6, 2012
    Posts:
    46
    @exiguous The text I want to display is not in the same coordinates as the texture. Its aligned different coordinates yet still ontop of texture on the bottom right. Im basically displaying amount of bullets and clip size ontop (in text) of the equipped weapon gui.texture.

    @TheRaider If worse comes to worst Ill have to do this but I was hoping to avoid it because it will require me to change the structure of allot of code just to make something simple like this happen. Right now im wondering if there is any way to tell the gui.label rect to display ontop of everything? thus making it ontop of the gui.texture almost like a layer.
     
  5. Euphoric

    Euphoric

    Joined:
    Sep 6, 2012
    Posts:
    46
    bump. still haven't figured this one out yet. any help is appreciated.
     
  6. exiguous

    exiguous

    Joined:
    Nov 21, 2010
    Posts:
    1,749
    what is the question? where do you have issues with? why don't you post your code?
     
  7. Euphoric

    Euphoric

    Joined:
    Sep 6, 2012
    Posts:
    46
    well im wondering if i can tell a gui.label to display above a gui.texture via code like a layer or alpha channel of some sort.

    the gui texture is like this:
    GUI.DrawTexture (new Rect (Screen.width + 8 - (Screen.width/12.5), 30, 88, 87),WeaponHud);

    the gui labels i want displayed above it like a layer is like this:
    GUI.Label (new Rect (Screen.width - (Screen.width/15),50,200,80),"" + bulletsLeft, style1);
    GUI.Label (new Rect (Screen.width - (Screen.width/16.5),50,200,80)," | " + magazines);
     
  8. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    There are several different ways you could do this. One is to use GUI Groups:

    Code (csharp):
    1. var hudPos : Rect;
    2. var hudImage : Texture2D;
    3. var bulletsLeft = 6;
    4. var magazines = 2;
    5.  
    6. function OnGUI(){
    7.     GUI.BeginGroup(hudPos, hudImage);    
    8.     GUI.Label(Rect(10,10,200,80),bulletsLeft.ToString());
    9.     GUI.Label(Rect(10,100,200,80)," | " + magazines);
    10.     GUI.EndGroup();
    11. }
    Another is to use a GUIStyle:

    Code (csharp):
    1. var hudPos : Rect;
    2. var hudGUI : GUIStyle;
    3. var bulletsLeft = 6;
    4. var magazines = 2;
    5.  
    6. function OnGUI(){    
    7.     GUI.Label(Rect(10,10,200,80), "" + bulletsLeft + "\n| " + magazines, hudGUI);
    8. }
    9.  
    Note that I combined both labels into one and added "\n" to make sure magazines appears on a second line. You'll add your hud image to the GUIStyle itself (Normal > Background), and you can alter the Font, Alignment, and Content Offset as needed to make your text look better.
     
  9. Euphoric

    Euphoric

    Joined:
    Sep 6, 2012
    Posts:
    46
    Thanks allot tonyd, this was definitely some very helpful information. I ended up using the gui groups and called the weapon hud texture image from a variable stored in a different script for the background. So I didn't have to change much of my code to make it all work properly all thanks to your examples. Thanks again!