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. Join us on Thursday, June 8, for a Q&A with Unity's Content Pipeline group here on the forum, and on the Unity Discord, and discuss topics around Content Build, Import Workflows, Asset Database, and Addressables!
    Dismiss Notice

Question How to get Display Width of Tab Character "\t"

Discussion in 'Immediate Mode GUI (IMGUI)' started by darkLink_32, Feb 2, 2023.

  1. darkLink_32


    Apr 22, 2018

    I would like to know if there is way to get the display width of the tab character "\t" in unity, or in other words, the length of the tab stop currently being used.

    Right now I am using to achieve this, but it doesn't appear to be accurate. To confirm this, I used the code below:

    Code (CSharp):
    2. private void OnGUI()
    3.         {
    4.             _widthOfStringWithTab ="label").CalcSize(new GUIContent("a"+"\t"+"aa")).x;
    5.             _widthOfString ="label").CalcSize(new GUIContent("aaaaaaaaaaa")).x;
    7.             Debug.Log("TAB = " + _widthOfStringWithTab + "STRING = " + _widthOfString);
    8.         }
    This outputs the following lengths:

    _widthOfStringWithTab = 78
    _widthOfString = 77

    So I would think that if I display a label with the first string, it would look longer than the second right?

    Well the string "a"+"\t"+"aa" appears as:


    and "aaaaaaaaaaa" appears as:


    So as you can see, the length of the tab character is not being calculated accurately.

    Is this not possible?

    Thank you,
  2. darkLink_32


    Apr 22, 2018
    So to provide an update:

    I decided to start using Text Mesh Pro which helped to bypass this issue. I ended up using Rich Text alignment tags to get the tabular look I was looking for. Also it looks like you can define your own tab value in the font file you're using with TMP so this may help with alternative solutions.