Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

GUIStyle - Text Alignment

Discussion in 'Scripting' started by marraboy, Sep 4, 2008.

  1. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Hi

    This is a fairly basic question. I done a quick search but had no luck - Apologies if this has already been answered.

    I am trying to center align some text in a Label:


    Code (csharp):
    1.     private void OnGUI()
    2.     {  
    3.         GUIStyle style = new GUIStyle();
    4.         style.alignment = TextAnchor.MiddleCenter;
    5.         GUI.Label(new Rect(Screen.width / 2 - 200,Screen.height - 30,400,30), "Some Text in a Label!", style);
    6. }
    7.  
    It just doesn't display anything. If I take the GUIStyle param out the text displays fine, although not center aligned.

    Thanks in advance.

    JT
     
    p_lewis and shikekaka like this.
  2. jashan

    jashan

    Joined:
    Mar 9, 2007
    Posts:
    3,307
    Most likely the reason is because your GUIStyle is empty, so it displays something "empty".

    If you have a GUISkin assigned, you could start with something like (untested forum-code, may be incorrect, I'm spoiled by Intellisense ;-) ):

    Code (csharp):
    1. GUIStyle style = new GUIStyle(GUI.skin.label);
    ... that (or something along those lines) should work ;-)

    Sunny regards,
    Jashan
     
    shikekaka likes this.
  3. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Yeah I did wonder about that, just wasn't sure how to fix it.

    That worked a treat anyway!

    Thanks

    JT
     
    shikekaka likes this.
  4. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Just for completeness...

    I found that 'exposing' a skin and dragging it from the Unity editor a great option...

    Code (csharp):
    1.  
    2. public GUISkin aSkin;
    3.  
    4. // then later
    5. GUI.skin = aSkin;
    6. GUIStyle style = new GUIStyle(GUI.skin.GetStyle("label"));
    7.  
    A big thanks to shaun for the examples and the GUI skins to play around with: http://forum.unity3d.com/viewtopic.php?t=7713

    Ta

    JT
     
    shikekaka likes this.