Search Unity

Using GUI in subframes ie Cameras with normalized View ports

Discussion in 'Immediate Mode GUI (IMGUI)' started by Ryuuguu, Mar 13, 2008.

  1. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    I have mulitple Cameras displaying at the same time using Normalized view port rects. I know that eventually I will want to let users resize these rects and currently want it to work on mulitple resolutions.

    The problem is that camera rects (camera.rect and Camera.pixelRect) are flipped vertically to GUI co-ordinates.

    The solution
    A) calculate window height using camera.rect and camera.pixelRect

    B)calculate carmera pixelRect in GUI oriented co-ordinates

    C)use these co-ordinates to position GUI elements inside the camera subframe.

    This is all do-able without much code (10? lines) usually when do something like this someone points out to me later I could have used an existing unity function to it in 1 line of code.

    So does anyone know a direct way to get a) or B)?

    If not I will write my code and post to Wiki

    Cheers,
    Grant
     
  2. Ryuuguu

    Ryuuguu

    Joined:
    Apr 14, 2007
    Posts:
    391
    Here is the script I attach to a camaera that holds a Rect with the cameras pixels in GUI co-ordinate space.

    Code (csharp):
    1. import UnityEngine
    2.  
    3. class GUICam (MonoBehaviour):
    4.  
    5.     public GUIRect as Rect
    6.     cam as Camera
    7.    
    8.     def Start():
    9.         cam = transform.camera
    10.  
    11.     def Update ():
    12.         pr=cam.pixelRect
    13.         r = cam.rect
    14.         screenHeight = pr.height / r.height
    15.         GUIRect = Rect(pr.left,screenHeight-(pr.top+pr.height),pr.width,pr.height)
    16.        
    17.  
    to use I put this on GUI generation script which is a camera component and draw button in the top left of the camera viewport.
    Code (csharp):
    1.  
    2.     aGUICam as GUICam
    3.  
    4.     def Start ():
    5.         aGUICam = transform.camera.GetComponent(GUICam)
    6.  
    7.     def ButtonGUI():
    8.         r=aGUICam.GUIRect
    9.         GUI.Button ( Rect (r.xMin+10,r.yMin+50,50, 30), "button"))     

    http://www.unifycommunity.com/wiki/index.php?title=GUICam