- Home /
Modify Camera pixelWidth and pixelHeight
How do I modify Camera's read only fields pixelWidth and pixelHeight? I intend to configure my camera to contain 1024 pixels in width and 768 pixels in height. Thanks! :D
Answer by tnetennba · May 18, 2012 at 11:12 AM
Use the screen api:
http://unity3d.com/support/documentation/ScriptReference/Screen
You can use this to get the available resolutions and then call:
http://unity3d.com/support/documentation/ScriptReference/Screen.SetResolution.html
There is also this which you can use to set the pixel rect for the camera http://unity3d.com/support/documentation/ScriptReference/Camera-pixelRect.html
I'm sorry, but that doesn't answer my question. You refer to changing the resolution of the screen, not the camera. I want to change these values:
http://unity3d.com/support/documentation/ScriptReference/Camera-pixelWidth.html
http://unity3d.com/support/documentation/ScriptReference/Camera-pixelHeight.html
Thank you. :D
I dont think that it will get modified. These values are read only.
Is that so? Can you recommend a workaround to my problem? I want to use ScreenToViewportPoint() and get an accurate translation given an arbitrary position, but since the values of pixelWidth and pixelHeight are fixed, I can't do it.
Answer by Zherynn · Oct 15, 2012 at 08:44 PM
If you're still looking for the answer, this worked for me:
camera.rect = Rect((Screen.width-512.0)/Screen.width,(Screen.height-256.0)/Screen.height,384.0/Screen.width,96.0/Screen.height);
That positions it at 512px left of the right edge and 256px below the top, 384px wide and 96px tall. You can of course adjust all the numbers to whatever you want.
Answer by shadowdiver · Jan 22, 2017 at 09:44 PM
I found a working Solution depends on Zhernn's answer.
public float cameraWidth = 206f;
public float cameraHeight = 189f;
// Update is called once per frame
void Update () {
// Camera has fixed width and height on every screen solution
float x = (100f - 100f / (Screen.width / cameraWidth)) / 100f;
float y = (100f - 100f / (Screen.height / cameraHeight)) / 100f;
GetComponent<Camera>().rect = new Rect(x, y, 1, 1);
}
The clue is to change the Rect's x- and y-value as percentage and not the Rect's width and height.
One last Tip:
Pose and scale the camera first with viewport rect and then debug the size with GetComponent().pixelWidth and GetComponent().pixelHeight to get the cameraWidth and camereHeight values.
Answer by AgathoSAreS · Jul 05, 2021 at 07:58 PM
Call me a designer but what about this setting in the Gameview?