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

    darkLink_32

    Joined:
    Apr 22, 2018
    Posts:
    4
    Hello,

    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 GUI.skin.GetStyle to achieve this, but it doesn't appear to be accurate. To confirm this, I used the code below:

    Code (CSharp):
    1.  
    2. private void OnGUI()
    3.         {
    4.             _widthOfStringWithTab = GUI.skin.GetStyle("label").CalcSize(new GUIContent("a"+"\t"+"aa")).x;
    5.             _widthOfString = GUI.skin.GetStyle("label").CalcSize(new GUIContent("aaaaaaaaaaa")).x;
    6.  
    7.             Debug.Log("TAB = " + _widthOfStringWithTab + "STRING = " + _widthOfString);
    8.         }
    9.  
    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:

    upload_2023-2-2_10-12-45.png

    and "aaaaaaaaaaa" appears as:

    upload_2023-2-2_10-14-36.png

    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

    darkLink_32

    Joined:
    Apr 22, 2018
    Posts:
    4
    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.