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! :smiley:

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

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.

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.

Call me a designer but what about this setting in the Gameview?183045-unity-set-screen-res.png