Camera Set Rect To Aspect Ratio
Set the camera rect to match an aspect ratio, either Letterboxing or Pillarboxing the view.
Usually you would set this in editor, but this action lets you easily change the aspect ratio at runtime, for cut scenes, mini-games, etc.
Parameters
| Parameter | Description |
|---|---|
| Camera | The Camera component to modify. |
| Aspect Ratio | Vector2 representing the desired aspect ratio (e.g., 16:9 would be (16, 9)). Default value is (16, 9). |
Details
This action automatically adjusts the camera's viewport rectangle to maintain a specific aspect ratio regardless of screen dimensions:
- Letterboxing: When the current screen is wider than the target aspect ratio, black bars are added to the top and bottom of the view
- Pillarboxing: When the current screen is taller than the target aspect ratio, black bars are added to the left and right sides of the view
The action calculates the appropriate scaling factor by comparing the current screen aspect ratio with the desired aspect ratio, then modifies the camera's rect property accordingly.
Performance
This action performs lightweight mathematical calculations and should have minimal performance impact. It's typically executed once when the screen resolution changes or when switching between different camera views.
Debugging
- The camera's rect property can be inspected in the Camera component during play mode to verify the calculated values
- Ensure the Camera parameter is assigned to avoid null reference issues
Examples
Maintaining 16:9 aspect ratio for cinematic games:
- Set Aspect Ratio to (16, 9)
- Useful for story-driven games where consistent framing is important
Classic 4:3 aspect ratio for retro games:
- Set Aspect Ratio to (4, 3)
- Creates a nostalgic feel with pillarboxing on widescreen displays
Ultra-wide support:
- Set Aspect Ratio to (21, 9) for ultra-wide monitors
- Prevents UI elements from being stretched on different screen ratios
This approach ensures your game maintains its intended visual composition across all devices and screen configurations, providing a consistent player experience.