title: Unity3D canvas scaler for different aspect ratios
body: <p>In Unity, I'm trying to build a mobile game for both android and ios platforms.
  I have been trying some different screen resolutions for testing purposes before I release the game.
  I have addressed the resolution issues using the code,</p>
  <pre><code>[ExecuteInEditMode]
  [RequireComponent(typeof(Camera))]
  public class ControllingCameraAspectScript : MonoBehaviour
  {
      public float sceneWidth = 21f;
      float targetaspect = 16.0f / 9.0f;
      float windowaspect = (float)Screen.width / (float)Screen.height;
      Camera camera;
      public Vector2 targetAspect = new Vector2(16, 9);
      
      void Start()
      {
          camera = GetComponent&lt;Camera&gt;();
          UpdateCrop();
      }
  
      public void UpdateCrop()
      {
          // Determine ratios of screen/window &amp; target, respectively.
          float screenRatio = Screen.width / (float)Screen.height;
          float targetRatio = targetAspect.x / targetAspect.y;
  
          if (Mathf.Approximately(screenRatio, targetRatio))
          {
              // Screen or window is the target aspect ratio: use the whole area.
              camera.rect = new Rect(0, 0, 1, 1);
          }
          else if (screenRatio &gt; targetRatio)
          {
              // Screen or window is wider than the target: pillarbox.
              float normalizedWidth = targetRatio / screenRatio;
              float barThickness = (1f - normalizedWidth) / 2f;
              camera.rect = new Rect(barThickness, 0, normalizedWidth, 1);
          }
          else
          {
              // Screen or window is narrower than the target: letterbox.
              float normalizedHeight = screenRatio / targetRatio;
              float barThickness = (1f - normalizedHeight) / 2f;
              camera.rect = new Rect(0, barThickness, 1, normalizedHeight);
          }
      }
  }
  </code></pre>
  <p>This code works very well with the aspect ratios below 2.0. (16/9, 16/10, 5/4 etc).
  When it comes to the screen resolutions such as 3200x1440 even the level seems to be finely cut off to match the view, canvas does drop out from the screen as per the screenshots below.</p>
  <p>Aspect ratio : 16/9 (same result for 1280x768)
  <a href="https://i.stack.imgur.com/ZL4jf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ZL4jf.png" alt="enter image description here" /></a></p>
  <p>screen resolution 3200x1440 (Aspect ratios above 2.0)
  <a href="https://i.stack.imgur.com/VkbFZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/VkbFZ.png" alt="enter image description here" /></a></p>
  <p>Canvas settings</p>
  <p><a href="https://i.stack.imgur.com/4AjHv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4AjHv.png" alt="enter image description here" /></a></p>
  <p>Does anybody have an idea about the issue here? Any suggestions would be highly appreciated.</p>