Search Unity

[Solved] GUI.Label and Rect precision ?

Discussion in 'Immediate Mode GUI (IMGUI)' started by bournifle, Jan 21, 2010.

  1. bournifle

    bournifle

    Joined:
    Jan 13, 2010
    Posts:
    31
    Hello!

    For one of my project, I need to display a texture (of size 512 x 1) at the top of the screen. So I create an image 512x512 under Photoshop, set the pixels in the first line, and set alpha to 0 in the remaining lines.
    Then I import the psd asset into Unity (as a ARGB 32bits texture, with point filter mode and no mipmaps), and display it thanks to the following script that is linked to the main camera:

    Code (csharp):
    1.  
    2. var LineTexture : Texture2D;
    3.  
    4. function OnGUI () {
    5.     GUI.Label (Rect (0, 0, 512, 512), LineTexture );
    6. }
    7.  
    The problem I have is that the texture is displayed at the third line of the screen, and that its width is 506 instead of 512.

    Does anyone have an idea why ?

    I tried to tweak the camera settings, the import texture settings, ... but nothing solved the problem...

    Thanks for your help,
    Quentin
     
  2. bournifle

    bournifle

    Joined:
    Jan 13, 2010
    Posts:
    31
    Problem solved !

    The texture is included in an invisible box with borders (default GUI style), which causes the texture to be resized inside.

    The solution is to create a new custom GUI style, and set the borders to 0.

    Code (csharp):
    1.  
    2. var LineTexture : Texture2D;
    3. var customGuiStyle : GUIStyle;
    4.  
    5. function OnGUI () {
    6.     GUI.Label (Rect (0, 0, 512, 512), LineTexture, customGuiStyle);
    7. }
    8.  
     
  3. bournifle

    bournifle

    Joined:
    Jan 13, 2010
    Posts:
    31
    Better solution: use the GUI.DrawTexture function...

    Code (csharp):
    1.  
    2. var LineTexture : Texture2D;
    3.  
    4. function OnGUI () {
    5.    GUI.DrawTexture (Rect (0, 0, LineTexture.width , LineTexture.height), LineTexture );
    6. }
    7.