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
That will let me view but how do I do the conversion between rect and pixelRect?
Thanks.
– Clint
you just multiply by Screen.width screen.height for x y
Okay, so looks to work… Thanks!
But… (there seems to always be a but)
Here is the code to what I am doing:
var cam : Camera = mapCamGO.GetComponent(Camera);
var xMin : float = frameGUI.pixelInset.xMin/Screen.width;
var xMax : float = frameGUI.pixelInset.xMax/Screen.width - frameGUI.pixelInset.xMin/Screen.width;
var yMin : float = frameGUI.pixelInset.yMin/Screen.height;
var yMax : float = frameGUI.pixelInset.yMax/Screen.height - frameGUI.pixelInset.yMin/Screen.height;
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
Forgot to mention that this is not working.
Regards,
– Clint
Here is a more clean version of what I am doing:
var cam : Camera = mapCamGO.GetComponent(Camera);
var xMin : float = parseFloat(xFrameMin)/Screen.width;
var xMax : float = parseFloat(xFrameMin + frameGUI.texture.width)/Screen.width - xMin;
var yMin : float = parseFloat(yFrameMin)/Screen.height;
var yMax : float = parseFloat(yFrameMin + frameGUI.texture.height)/Screen.height - yMin;
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
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:
mapCamGO = GameObject.Find("mapCamera");
var cam : Camera = mapCamGO.GetComponent(Camera);
var frameGUI : GUITexture = frame.GetComponent(GUITexture);
var guiRect : Rect = frameGUI.GetScreenRect();
var normalized : Rect;
normalized.xMin = guiRect.xMin/Screen.width;
normalized.xMax = guiRect.xMax/Screen.width;
normalized.yMin = guiRect.yMin/Screen.height;
normalized.yMax = guiRect.yMax/Screen.height;
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