Search Unity

Calculate the length of a string, text or label in pixels

Discussion in 'Immediate Mode GUI (IMGUI)' started by montdidier, Oct 5, 2009.

  1. montdidier

    montdidier

    Joined:
    Aug 27, 2009
    Posts:
    22
    Is there an established reliable way to calculate how long a element of text will be in Pixels?

    I'm currently using the likes of GUI.Label() and while it accepts a rectangle to define the label boundary I've not see a way of calculating the size of a piece of text.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Many of the GUI functions have the option of using a GUIStyle parameter. GUIStyle has a method called CalcSize that gives you the rectangle that an element will occupy if rendered with that style.
     
  3. omrip32

    omrip32

    Joined:
    Jan 4, 2016
    Posts:
    105
    That worked for me, thanks!