How exactly "\t" works?

" " is used to add a Tab in text display. But what is the underlying theory of how it works? Sometimes have to manually add more space to actually achieve the real aligning effects.

For example:

GUI.Label(name[0]+"      	"+ life);
GUI.Label(name[1]+"            	"+ life);
GUI.Label(name[2]+"      	"+ life);

So I think understanding exactly how it works will be very useful.

There's no way to get a acceptable result with variable-width fonts. Tab actually does add a variable length but it has a maximum length. You got the same problem with monospaced fonts. Just imagine tab-stops every 8 characters (just vertical lines where to stop is you use a tab)

If you use this strings "Hello World" it will move to the next tabstop after Hello

//|       |       |       |       |
//|Hello   World
//      |->

But if your first string is equal or greater than a single tabstop(8 in our case) it will take the next one.

“Welcome: PlayerName”

//|       |       |       |       |
//|Welcome:        PlayerName
//         |------>

I wouldn't struggle with that :D. Just use two seperate labels. With two labels you can even allign the two parts differently. For example playername and points. It looks much better when the points are right-aligned.

var playerNames : String[];
var points : String[];

function OnGUI()
{
    var rightAlignedLabel = new GUIStyle(GUI.skin.label);
    rightAlignedLabel.alignment = TextAnchor.MiddleRight;
    rightAlignedLabel.fixedWidth = 60;

    GUILayout.BeginVertical(GUILayout.Width(300));
    for (var i=0; i<playerNames.length; i++)
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label(playerNames*);*

GUILayout.Label(points*,rightAlignedLabel);*
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}

In this example i use a fix size of 60 for the points label. Thw whole area is set to 300 pixel width and can grow in height. If you want to test it make sure you set both arrays to an equal elementcount (i.e. 3x playerNames and 3x points)


Some related things: GUILayout, GUIStyle.alignment

The reason you're not getting a uniform alignment out of the tab character is that it doesn't advance the string until some common tabulator stop, after which it would add `life`, which is what an old fashioned typewriter would do (and what you seem to expect, judging from your code). :)

The tab character is a whitespace character (ASCII 9) with a fixed width which corresponds to some multiple of the Space-character. Just how much this multiple is varies from system to system and programming language to programming language.

Consider that since the strings in `name[0]`, `name[1]` and `name[2]` have different lengths, you're also going to get misaligned text on your GUI label if you add the same multiple of spaces (the tab character) to the string. The manual correction you did to achieve the alignment then simply corresponds to the difference in length between the strings in `name[]`.

Hope that clears it up a bit.

I am now thinking is there a way to set a String to certain size? For example like

name[0].setSize(12) will be "Jack      "
name[1].setSize(12) will be "Johnthan  " 

A way to uniform all string sizes. If have this String function, I think this will solve this aligning problem.

Hmm, maybe we could write by our own

function setSize(_s:String,_num:int):String{
  var temp_s=_s;
  if(_s.length>=_num)
    return _s;
  else{
  for(var i:int;i<_num-_s.length;i++){
     temp_s+=" ";
  }
    return temp_s;
  }
}

for(var i:int;i<name.length;i++){
GUI.Label(name*.setSize(12)+"  	"+ life);*
*}*
*```*

You can get the result by using a combination of tabs and spaces, so long as you know how large Unity will make the base string. I’ve created a class that extends String with the ability to do this. There are times when it is necessary (when putting things in a Popup for instance).

You can find the fixed width string class for download here