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

GUIText and GUITexture placement

Discussion in 'Immediate Mode GUI (IMGUI)' started by CoherentInk, Jul 17, 2006.

  1. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    I'm sure that someone has already posted about this, but I can't find it at the moment.
    1. I'm working on a GUI layout and have the problem that GUIText elements appear with different amount of space between them depending on what resolution is used by the player.
    2. Additionally, when trying to position text in relation to a GUITexture, it is a real challenge to position them with one positioned using pixels and the other positioned only using percentages along the canvas height and width.
    Any suggestions?
     
  2. CoherentInk

    CoherentInk

    Joined:
    Jul 16, 2006
    Posts:
    216
    One solution that I've thought of is to set the Pixel Offset values based on font height and anchor all of the text to the same point using the Transform. This may actually solve both problems since you can use the same co-ordinates for all of the related GUI items.
     
  3. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    Using pixelOffset in the guitext and pixelInset in the guitexture indeed solves those problems.

    Usually you just set the localScale of the transform and the position to zero. This way, the size of the screen does not control the gui texture size and position at all.

    Sometimes you want an offset that scales with the screen size and then you just change the position of all gui elements to that same value.
     
  4. Orion

    Orion

    Joined:
    Mar 31, 2008
    Posts:
    261
    Sorry for bumping this, but I'm running into pretty much the same issue -
    just that the answer doesn't work for me.

    I use a script to scale my GUITexture objects with the resolution, while keeping their position relative to the edges they're aligned to (so e.g. I set the position to 1,1,0 to anchor it in the corner and adjust the position perfectly with the inset -
    the script then scales the textures up or down and multiplies the inset with the same factor, to match whatever resolution the user is using (with some limits).

    The problem is that GUIText does not work like GUITexture - I can only either scale the text, or use the offset, not both. And while with GUITexture I can just scale the inset width and height, the GUIText already scales with the resolution. So far no matter what I try, the text is always off in scale, ratio and/or position. And when I use pixel perfect, the size of course is unscaleable, making things more complicated.

    This is what I do with GUITexture:
    Code (csharp):
    1. static private var minAtScreenHeight = 384;
    2. static private var maxAtScreenHeight = 768;
    3. static private var maxFactor = 2;
    4.  
    5. var temp = Mathf.InverseLerp(minAtScreenHeight, maxAtScreenHeight, Screen.height);
    6. var factor = Mathf.Lerp(1, maxFactor, temp);
    7. var gui = GetComponent(GUITexture) as GUITexture;
    8. gui.pixelInset.x *= factor;
    9. gui.pixelInset.y *= factor;
    10. gui.pixelInset.width *= factor;
    11. gui.pixelInset.height *= factor;
    Any help to achieve the same with GUIText would be very appreciated!