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

Offset of GUITexture based on Screen Size

Discussion in 'Scripting' started by SeeleyBooth, Apr 24, 2014.

  1. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    Ok so in one of my scripts i am drawing a guitexture which is sized width and height based on screen size. But I am also placing it on the screen 10 from the top and ten from the left.

    As screen size changes that 10, 10 is not the same. Is there a way to calculate that "offset" based on the screen size?

    Code (csharp):
    1. void OnGUI(){
    2.  
    3.         GUILayout.BeginArea(new Rect(10, 10, _newFrameWidth, _newFrameHeight), frame);
    4.         GUI.DrawTexture (new Rect (10, 10, _displayWidth, _newBarHeight), forground);
    5.         GUILayout.EndArea();
    6.     }
    Everything is calculated else where and works perfect. It is only the offset I am having a issue with.
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    You mean you want a constant fraction of the screen's height/width? Try (0.1f * _newFrameWidth) for example?
     
  3. SeeleyBooth

    SeeleyBooth

    Joined:
    Jan 23, 2014
    Posts:
    30
    Absolutly perfect. Thank you!
     
  4. Suddan

    Suddan

    Joined:
    Jan 12, 2013
    Posts:
    21
    To be exact, the offset of 10 in x and 10 in y direction will still be the same, just in case you go fullscreen it will appear to be wider than before, but still 10 pixels. :p

    Either you take the suggested solution or in order to make it dependant on the screen's resolution and not from the drawn rectangle (maybe you wanna adjust its size later, which will also affect the offset) you could do the following:

    - Choose a resolution at which you would like to have an offset of x=10, y=10 (let's say 1920x1080)
    - now calculate the offset relative to it : (10f/1920)*Screen.width

    In case the offset of both (x and y) should be the same, use that calculation for both offsets as the X offset will not be the same as the Y offset if you calculated it with width for the x-offset and height for the y-offset.