Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Important: How do I get the width of text in pixels of a UILabel's current text?

Discussion in 'UGUI & TextMesh Pro' started by goodnewsjimdotcom, Aug 26, 2022.

  1. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    I saw a post saying something like:

    GUIStyle is for normal font tho, not imGUI

    GUIStyle myStyle = new GUIStyle();
    myStyle.fontStyle = GTicker.GetComponent<UILabel>().fontStyle;
    myStyle.fontSize = GTicker.GetComponent<UILabel>().fontSize;
    myStyle.font= GTicker.GetComponent<UILabel>().font);//This line doesn't work with Imgui

    Vector2 size = myStyle.CalcSize(new GUIContent(GTicker.GetComponent<UILabel>().text));
     
  2. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    If I know the width of text, I can have images follow them along on a ticker tape.
     
  3. goodnewsjimdotcom

    goodnewsjimdotcom

    Joined:
    May 24, 2017
    Posts:
    342
    There is some component called imguistyle, but it won't populate. Does it need an include?