-
Notifications
You must be signed in to change notification settings - Fork 450
/
Copy pathAutoResolution.cs
58 lines (49 loc) · 2.04 KB
/
AutoResolution.cs
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AutoResolution : MonoBehaviour
{
public int setWidth = 1440;
public int setHeight = 2560;
private void Start()
{
// Get the main camera and its current dimensions
Camera camera = Camera.main;
Rect rect = camera.rect;
// Calculate the scale height and width of the screen
float scaleHeight = ((float)Screen.width / Screen.height) / ((float)9 / 16);
float scaleWidth = 1f / scaleHeight;
// Adjust the camera's dimensions based on the scale height and width
if (scaleHeight < 1)
{
rect.height = scaleHeight;
rect.y = (1f - scaleHeight) / 2f;
}
else
{
rect.width = scaleWidth;
rect.x = (1f - scaleWidth) / 2f;
}
camera.rect = rect;
SetResolution();
}
public void SetResolution()
{
// Get the current device's screen dimensions
int deviceWidth = Screen.width;
int deviceHeight = Screen.height;
// Set the screen resolution to the desired dimensions, while maintaining aspect ratio
Screen.SetResolution(setWidth, (int)(((float)deviceHeight / deviceWidth) * setWidth), true);
// Adjust the camera's dimensions based on the new resolution
if ((float)setWidth / setHeight < (float)deviceWidth / deviceHeight)
{
float newWidth = ((float)setWidth / setHeight) / ((float)deviceWidth / deviceHeight);
Camera.main.rect = new Rect((1f - newWidth) / 2f, 0f, newWidth, 1f);
}
else
{
float newHeight = ((float)deviceWidth / deviceHeight) / ((float)setWidth / setHeight);
Camera.main.rect = new Rect(0f, (1f - newHeight) / 2f, 1f, newHeight); // 새로운 Rect 적용
}
}
}