본문 바로가기
개발/C#

유니티 C# 가변 해상도 대응 Android , IOS 간단 구현

by SPNK 2022. 8. 25.
반응형
  • 코드 작성
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraResolution : MonoBehaviour
{
    private void Start()
    {
        SetResolution();
    }

    public void SetResolution()
    {
        int setWidth = 1080;
        int setHeight = 1920;

        int deviceWidth = Screen.width;
        int deviceHeight = Screen.height;

        Screen.SetResolution(setWidth, (int)(((float)deviceHeight / deviceWidth) * setWidth), true);

        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);
        }
    }
}

참고할만한 글

 

 

유니티 C# 마우스 좌표 Mouse Position 간단 구하기

코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MousePosition : MonoBehaviour { private void Start() { } void Update() { if (Input.GetMouseButtonDown(0)) { Vector3 mousePos = C

parksh3641.tistory.com

 

 

유니티 C# Enum Count 길이 간단 구하기

코드 작성 using System.Collections; using System.Collections.Generic; using UnityEngine; public enum MoneyType { Gold = 0, Crystal } public class ExampleEnum : MonoBehaviour { public MoneyType moneyType = MoneyType.Gold; void GetEnumCount() { int count

parksh3641.tistory.com

 

반응형

댓글