Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by Dreamer · May 10, 2011 at 06:10 AM · textdisplayaligntab

How exactly "\t" works?

"\t" 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]+"      \t"+ life);
GUI.Label(name[1]+"            \t"+ life);
GUI.Label(name[2]+"      \t"+ life);

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
6
Best Answer

Answer by Bunny83 · May 10, 2011 at 11:26 AM

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\tWorld" 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:\tPlayerName"

//|       |       |       |       |
//|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[i]);
         GUILayout.Label(points[i],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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamer · May 10, 2011 at 11:37 AM 0
Share

This is the answer I am looking for! Excellent and thank you for your time. people come and vote this up~ lol

avatar image Glurth · Mar 13, 2015 at 12:12 AM 0
Share

This was NOT exactly what I was looking for, but the answer is so good, it helped me figure out what was going with TabSize and what the docs for Text$$anonymous$$esh mean by a "multiplum".

avatar image
3
Best Answer

Answer by CHPedersen · May 10, 2011 at 06:38 AM

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Dreamer · May 10, 2011 at 07:52 AM 0
Share

Still not quite sure. You see, tab will actually add dynamic number of spaces automatically. If difference of the sizes of strings is too big, it will not work

avatar image Bunny83 · May 10, 2011 at 11:45 AM 0
Share

That's not exactly how it works. Even back in CRT-monitor times and the good old textmode the tab always goes to the next tabstop. Nowadays it's even more complicated since we have variable-width-fonts. The function of the tab is still the same. It's not really bound to a character size. Normally a tab is a multiply of the space(ASCII 32) character. On my computer the tab size is 4. The variable width is the real problem. One "$$anonymous$$" is almost as large as 5 "i" and therefore they use the same space and results in the same tabstop.

avatar image Bunny83 · May 10, 2011 at 11:52 AM 0
Share

Oh, and the tab character (ASCII 9) is not a viewable character. It's a control-character that is interpreted by the system that is viewing the text.

avatar image
0

Answer by Dreamer · May 10, 2011 at 07:16 AM

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[i].setSize(12)+" \t"+ life); }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CHPedersen · May 10, 2011 at 11:51 AM 1
Share

No, follow Bunny83's advice. :) He's right. The above simply pads your strings with Space-characters until they contain the same amount of characters, which doesn't solve the problem unless your font is monospaced. Just as "iiiii " doesn't have the same length as "AAAA " right here, even though they contain the same amount of characters

avatar image
0

Answer by whydoidoit · Apr 12, 2012 at 08:35 PM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Script that shows a text to press "E" on the object? 6 Answers

Whats a good technique to create an in-game screen or display for text output? 1 Answer

Text Asset to be displayed: NOT WORKING!! HELP!! 0 Answers

text display on touch 1 Answer

Problem Displaying Score on GUI text 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges