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

Adjust Camera ViewPort based on Screen Resolution?

Discussion in 'Scripting' started by littlelingo, Jan 7, 2007.

  1. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    I have a camera with a viewport rect that I want to change based on screen resolution but can't seem to figure out how to convert the normalized viewport rect to screen coordinates and vice versa. Looking through the documentation I couldn't find anything that looked to work. I focussed on the ScreenToViewPortPoint and ViewPortToScreenPoint but those didn't look to work.

    I am figuring being able to do this, I can solve a screen border/offset, convert to the normalized coordinates then apply to the viewport rect.

    Thanks in advance for any help!

    Regards,

    -- Clint
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
  3. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    That will let me view but how do I do the conversion between rect and pixelRect?

    Thanks.

    -- Clint
     
  4. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    you just multiply by Screen.width screen.height for x y
     
  5. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Cool, Thanks!

    -- Clint
     
  6. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Okay, so looks to work... Thanks!

    But... (there seems to always be a but) ;-)

    Here is the code to what I am doing:

    Code (csharp):
    1. var cam : Camera = mapCamGO.GetComponent(Camera);
    2. var xMin : float = frameGUI.pixelInset.xMin/Screen.width;
    3. var xMax : float = frameGUI.pixelInset.xMax/Screen.width - frameGUI.pixelInset.xMin/Screen.width;
    4. var yMin : float = frameGUI.pixelInset.yMin/Screen.height;
    5. var yMax : float = frameGUI.pixelInset.yMax/Screen.height - frameGUI.pixelInset.yMin/Screen.height;
    6. cam.rect = Rect( xMin, yMin, xMax, yMax );
    What I am looking to do is to take a camera and set it's rect to the same exact spot as a guiTexture.

    Thanks for the help!

    -- Clint
     
  7. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Forgot to mention that this is not working. ;-)

    Regards,

    -- Clint
     
  8. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Here is a more clean version of what I am doing:

    Code (csharp):
    1. var cam : Camera = mapCamGO.GetComponent(Camera);
    2. var xMin : float = parseFloat(xFrameMin)/Screen.width;
    3. var xMax : float = parseFloat(xFrameMin + frameGUI.texture.width)/Screen.width - xMin;
    4. var yMin : float = parseFloat(yFrameMin)/Screen.height;
    5. var yMax : float = parseFloat(yFrameMin + frameGUI.texture.height)/Screen.height - yMin;
    6. cam.rect = Rect( xMin, yMin, xMax, yMax );
    This code is in fact adjusting the camera rect but it is not placing it in the same position the frameGUI is at. It is actually quite a bit.

    Attached is a picture that shows the frame and where the camera rect is positioning.

    Thanks!

    -- Clint
     

    Attached Files:

  9. littlelingo

    littlelingo

    Joined:
    Jul 18, 2006
    Posts:
    372
    Okay with some great help from Joe we came up with a couple things... Here is a description of it (some obvious and hopefully some not so obvious to people other than just me ;-p ).

    With GUITextures and pixelInset the transform.position can have a dramatic effect on your results and essentially the transform.position will turn into an offset of the pixelInset. Sort of obvious, except to me. If you have GUITextures that are a parent of a GO and not of the Root you should make sure that transform.position is set to something you expect (or 0) as this could give similar offset results.

    When wanting to set a Camera.rect equal to a GUITexture.pixelInset you should use GetScreenRect(). Then divide by Screen.width for x and Screen.height for y (thanks, Nicholas) to get a converted normalized rect that can be used for the camera. For instance the code used in my example looks like this:

    Code (csharp):
    1. mapCamGO = GameObject.Find("mapCamera");
    2. var cam : Camera = mapCamGO.GetComponent(Camera);
    3.  
    4. var frameGUI : GUITexture = frame.GetComponent(GUITexture);
    5. var guiRect : Rect = frameGUI.GetScreenRect();
    6.    
    7. var normalized : Rect;
    8. normalized.xMin = guiRect.xMin/Screen.width;
    9. normalized.xMax = guiRect.xMax/Screen.width;
    10. normalized.yMin = guiRect.yMin/Screen.height;
    11. normalized.yMax = guiRect.yMax/Screen.height;
    12.    
    13. cam.rect = normalized;
    Lastly, there seems to be a bug if using Maximize on Play where the items will not line up as they should but in normal mode and compiled it should all work as expected.

    Hope some people find this useful and as always thanks for all the help.

    Regards,

    -- Clint