Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Build in GUI Text Alignment

Discussion in 'Immediate Mode GUI (IMGUI)' started by yellowlabrador, Feb 25, 2008.

  1. yellowlabrador


    Oct 20, 2005
    Hello all,

    How can I access the build in GUI Text allignment thru scripting.

    Trying something like the code below

    Code (csharp):
    2. GUISkin.settings = TextAlignment.left;
    but getting errors.

  2. CoherentInk


    Jul 16, 2006
  3. yellowlabrador


    Oct 20, 2005
    Hi CoherentInk,

    Works great thank you.

    What I found out is if I make my GUIStlyle = null it affects all other GUI's that use the default built in GUI skin.

    Code (csharp):
    2. theNationTextFile = theTextFile[0].text;
    3. myGUIStyle = null;
    4. myGUIStyle.alignment = TextAnchor.UpperLeft;
    5. GUI.Box(Rect(11,100,330,315),GUIContent(theNationTextFile));
    The snippet above is inside an if condition.

    I guess we can not alter the builtin gui with out altering the other GUI's using the default built in GUIS. :?:

    When I make my GUIStyle = null and remove that script, the rest of the GUI remains on the upper left, needed to re start Unity(open a different project then re open the project I'm working on) for the GUI's to go back to the original state(Middle)

    Maybe I'm doing something gooffy to this GUIS. :?

  4. CoherentInk


    Jul 16, 2006
    Oh no! You're right! I thought that I had found a way to create a new style through scripting, but alas, I have not!

    It appears that my attempt to copy the style doesn't do that at all-- it merely copies the address (or the pointer) to the current style, and any modifications to it actually modify the original style!

    That said, I think I finally have the solution! The GUI system behaves in a manner unlike anything I've ever worked with, with all its oddities within the OnGUI() function.

    To change just one attribute for a given style, you go about it the same way you would gray-out disabled controls: make a statement in OnGUI() to change the state of the GUI, and then change it back later. Below is an example of how to change alignment of text:

    Code (csharp):
    1. function OnGUI () {
    2.     toggle = GUI.Toggle(Rect(x, y, width, height), toggle, "Enable/Disable");
    3.     // enable or disable a field based on the previous toggle
    4.     GUI.enabled = toggle;
    5.     slider = GUI.HorizontalSlider(Rect(x, y + height, width, height), slider, 0.0, 1.0);
    6.     // enable fields after here
    7.     GUI.enabled = true;
    9.     // a normal label
    10.     GUI.Label(Rect(x, y + 2 * height, width, height), "I'm a normal label");
    11.     // now change the alignment for labels to the right
    12. = TextAnchor.UpperRight;
    13.     GUI.Label(Rect(x, y + 3 * height, width, height), "I'm a right aligned label");
    14.     // now change the alignment for labels to the middle
    15. = TextAnchor.UpperCenter;
    16.     GUI.Label(Rect(x, y + 4 * height, width, height), "I'm a center aligned label");
    17.     // now make labels normal again
    18. = TextAnchor.UpperLeft;
    19.     GUI.Label(Rect(x, y + 5 * height, width, height), "I'm a normal label again");
    20. }
    Sorry for the confusion, but I'm still working this out myself!

    Hopefully this functionality can be added to the manual in future releases.
  5. uzquiano


    Feb 28, 2011

    Thank you, yellowLabrador and CoherentInk, I was working whole day in this issue but still I am not totally satisfied with the solution.

    With the final solution CoherentInk provides, it works, though next time you creates a new (e.g.) GUI.Label the alignment is still the same was modified last time.
    So, for me the perfect solution is the one that copies all the values the default GUIStyle provides into a new GUIStyle. And then, in this new GUIStyle you can modify (e.g.) the GUI.Label alignment.
    But I was working for few hours in this I can't bring with nothing new. Hopefully, someone could bring to light this issue.

  6. JacobPennock


    Jul 7, 2011
    Hey guys I found a solution to this problem that works for me. That is how to copy an existing GUIStyle into a new GUIStyle and modify just a few of its variables. I'm working in C# and not sure how well this may translate to javascript.

    First have a reference to the existing style you want to copy and then create a new GUIStyle using the new keyword and pass it the reference.

    Code (csharp):
    2. InventoryHeaderStyle = new GUIStyle(DialogBoxStyle);
    3. InventoryHeaderStyle.alignment = TextAnchor.UpperLeft;
    4. InventoryHeaderStyle.padding.left = 10;
    5. = 0;
    6. InventoryHeaderStyle.normal.background = null;

    Here is a little bit more of that class for contex.

    Code (csharp):
    1. public class PlayerQuestionWindow
    2. {
    3.     float currentPlayerDialogHeight = 0;
    4.     public float targetPlayerDialogHeight;
    5.     float lineoffset;
    6.     float ScrollViewHeight;
    7.     float ScrollViewOffset;
    8.     float ScrollViewLineOffset;
    9.     int MaxQuestionInView = 6;
    10.     GUIStyle DialogBoxStyle;
    11.     GUIStyle InventoryHeaderStyle;
    12.     GUIStyle PlayerDialogTextStyle;
    13.     public Dictionary<int,InterviewQuestion> Questions;
    14.     public List<long> SelectedAnswers;
    15.     public Texture2D NextIcon;
    16.     int MaxNumOfChars = 110;
    17.     Vector2 scrollViewVector =;
    18.     string StepName = "";
    20.     public PlayerQuestionWindow(GUIStyle boxstyle, GUIStyle playertextstyle,Dictionary<int,InterviewQuestion>  questions,List<long> answers, Texture2D nexticon, string stepname )
    21.     {
    22.         DialogBoxStyle = boxstyle;
    23.         PlayerDialogTextStyle = playertextstyle;
    24.         Questions = questions;
    25.         SelectedAnswers = answers;
    26.         lineoffset = PlayerDialogTextStyle.fontSize + 10;
    27.         targetPlayerDialogHeight = (Questions.Count * lineoffset) + lineoffset;
    28.         NextIcon = nexticon;
    29.         ScrollViewHeight = lineoffset*MaxQuestionInView;
    30.         ScrollViewOffset = 0;
    31.         ScrollViewLineOffset = 0;
    32.         StepName = stepname;
    34.         InventoryHeaderStyle = new GUIStyle(DialogBoxStyle);
    35.         InventoryHeaderStyle.alignment = TextAnchor.UpperLeft;
    36.         InventoryHeaderStyle.padding.left = 10;
    37. = 0;
    38.         InventoryHeaderStyle.normal.background = null;
    39.     }
    Again this is C# and I'm not sure if the new keyword works the same way in javascript. But this solution is indeed working for me.

    I hope that helps.