title: How to Dynamically script viewport rect in unity
body: <p>I am wanting to dynamically allocate the display of 3 aircraft in unity according to the id of the vehicle in 3 corners of the screen and the main camera view in the top right. I am just unsure how to script the viewport rect as its a piece of the camera prefap. </p>
  
  <p><a href="https://i.stack.imgur.com/rNry2.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rNry2.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <h2>EDIT</h2>
  
  <p>To do this you have to search camera.rect and you can dynamically allocate it using a basic if statement. </p>
  
  <pre><code>using UnityEngine;
  
  // Change the width of the viewport each time space key is pressed
  
  public class ExampleClass : MonoBehaviour
  {
      private Camera cam;
  
      void Start()
      {
          cam = Camera.main;
      }
  
      void Update()
      {
          if (Input.GetKey("space"))
          {
              // choose the margin randomly
              float margin = Random.Range(0.0f, 0.3f);
              // setup the rectangle
              cam.rect = new Rect(margin, 0.0f, 1.0f - margin * 2.0f, 1.0f);
          }
      }
  }
  </code></pre>