Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Screen space vs GUI space

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ryuuguu, Nov 26, 2008.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    387
    I am trying to convert some things between GUI Space to world position. I have the screen to world working OK but am having problems getting from GUI to Scren space. I thought the only difference was GUI space has (0,0) in top,left and Screen space it is bottom,left. So to convert a gp (GUI Position) to sp (Screen Position) I would do the code below getting sp2, I tried using the GUIUtility.GUIToScreenPoint function below also.

    Code (csharp):
    1.  
    2. gp = Vector2(windowRect.x+r.xMax,windowRect.x+r.yMin)
    3. sp=GUIUtility.GUIToScreenPoint(gp)
    4. sp2 = Vector2(gp.x,Screen.height-gp.y)
    5. Debug.Log("sp:${sp}:sp2:${sp2}:gp:${gp}:hieght:${Screen.height}:width:${Screen.width}")
    6.    
    the log shows
    sp:(760.0, 492.0):sp2:(575.0, 242.0):gp:(575.0, 381.0):hieght:623:width:770

    Is GUIUtility.GUIToScreenPoint buggy?
    or is there something wrong with my calculation of sp2?
    Or does GUIUtility.GUIToScreenPoint do something else?

    Cheers,
    Grant
     
  2. HiggyB

    HiggyB

    Unity Product Evangelist

    Joined:
    Dec 8, 2006
    Posts:
    6,183
    I don't believe anything is buggy here and I sense there is some confusion about what exactly "screen space" is when using GUIToScreenPoint and ScreenToGUIPoint. When using either of those, "screen position" refers to the overall position in UnityGUI screen space, so the upper-left is still (0,0) and the units are in pixels. Try this on for size:

    Code (csharp):
    1. function OnGUI () {
    2.  
    3.   GUI.BeginGroup( Rect(5.0, 5.0, 100.0, 100.0) );
    4.    
    5.     var gp = Vector2(2.0, 2.0);
    6.     Debug.Log( GUIUtility.GUIToScreenPoint(gp) );
    7.        
    8.     var sp = Vector2(7.0, 7.0);
    9.     Debug.Log( GUIUtility.ScreenToGUIPoint(sp) );
    10.        
    11.   GUI.EndGroup();
    12.    
    13. }
    You'll notice that I'm calling both functions from within the group. So when I declare gp it's the position (2,2) within that group. Without knowing the group's rect, or the rects of any other parent groups or windows, I can convert that point to a net/overall/global position in "screen space". Thus the output is (7,7) (the group is in 5, then the point is in another 2 from there). When I declare sp it's that same net/overall/global position and when called within the group the result is the "local" position for that same position. Thus the output is (2,2).

    If you use either function while not in a group, window or other contained context then it does no coversion.


    None of the above converts your Unity GUI position into a frame of reference needed by ScreenToWorldPoint which is what you'll need to get a world position. ScreenToWorldPoint requires you to specify a position (x,y), using the bottom-left at (0,0). So you have to do a small conversion of the y value yourself.
     
    kozyrevsergey likes this.
  3. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    387
    Thanks,

    It was something wrong with my understanding of how GUIUtility.GUIToScreenPoint works. When thinking about how GUI layout works it make sense that it would be relative to the block, but there will probably be many newbies soon who like me don't bother to reread the GUI manual and think about the implication, so I will put a documentation bug/enhancement report about mentioning GUIToScreenPoint is relative to current block in its scripting refernce.

    cheers,
    Grant
     
  4. joshua-jebadurai

    joshua-jebadurai

    Joined:
    Jul 3, 2009
    Posts:
    14
    This should convert the Y to screen space to GUI space. :)

    Code (csharp):
    1.  
    2. function ScreenToGUIPoint(input:Vector2):Vector2
    3. {
    4.     return Vector2(input.x, Interpolate(input.y, Screen.height, 0, 0, Screen.height));
    5. }
    6.  
    7. function Interpolate(xVar, xMin, xMax, yMin, yMax):float
    8. {
    9.     return ( (xVar - xMin) / (xMax - xMin) ) * (yMax - yMin) + yMin;
    10. }
    11.  
     
  5. Kroko

    Kroko

    Joined:
    Jul 22, 2014
    Posts:
    1
    waaw, so complicated if you express that 'Interpolate' you will see its equals to Screen.height - input.y