Skip to content

Commit

Permalink
Auto Resolution Script
Browse files Browse the repository at this point in the history
It's useful for overcoming very slight differences.

It is difficult to apply a large changes in which the resolution is reversed.

It can be used appropriately in situations where resolution is important in mobile-like environments.

Attach this script to the camera and enter the desired resolution.
  • Loading branch information
Falcon5077 committed Apr 28, 2023
1 parent 9b252f9 commit d4eeaeb
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions Assets/Scripts/Camera/AutoResolution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 적용
}
}
}

0 comments on commit d4eeaeb

Please sign in to comment.