-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSetAspectRatio.cs
More file actions
41 lines (27 loc) · 795 Bytes
/
SetAspectRatio.cs
File metadata and controls
41 lines (27 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
using System.Collections;
public class SetAspectRatio : MonoBehaviour {
public Vector2 aspectRatio = new Vector3(16, 9);
void Start() {
float targetAspect = aspectRatio.x / aspectRatio.y;
float windowAspect = (float) Screen.width / (float) Screen.height;
float scaleHeight = windowAspect / targetAspect;
Camera camera = GetComponent<Camera>();
if (scaleHeight < 1.0f) {
Rect rect = camera.rect;
rect.width = 1.0f;
rect.height = scaleHeight;
rect.x = 0;
rect.y = (1.0f - scaleHeight) / 2.0f;
camera.rect = rect;
} else {
float scaleWidth = 1.0f / scaleHeight;
Rect rect = camera.rect;
rect.width = scaleWidth;
rect.height = 1.0f;
rect.x = (1.0f - scaleWidth) / 2.0f;
rect.y = 0;
camera.rect = rect;
}
}
}