본문 바로가기
개발/Unity

Unity) GUI(IMGUI) 해상도 고정하기

by 테샤르 2021. 6. 28.

GUI(IMGUI) 해상도 고정하기

 

Unity GUI(IMGUI)는 OnGUI라는 스크립터로 GUI를 구현하는 기능이다.

대부분은 GUI 시스템은 다음과 같은 상황에서 많이 사용한다.

 

게임 내 디버깅 디스플레이 및 도구 만들기

커스텀 만들기 스크립트 구성 요소

Unity 자체 확장을 위한 새 편집기 창 및 도구 만들기

 

예시 코드는 다음과 같다.

 

<간단한 GUI 사용하는 코드>

/* Example level loader */
using UnityEngine;
using System.Collections;

public class GUITest : MonoBehaviour {
            
    void OnGUI ()
    {
        // Make a background box
        GUI.Box(new Rect(10,10,100,90), "Loader Menu");
    
        // Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
        if(GUI.Button(new Rect(20,40,80,20), "Level 1"))
        {
            Application.LoadLevel(1);
        }
    
        // Make the second button.
        if(GUI.Button(new Rect(20,70,80,20), "Level 2")) 
        {
            Application.LoadLevel(2);
        }
    }
}

GUI로 특정 수치 값을 입력하게 되면 해상도에 따라 Text 사이즈, 크기 등등이 달라진다. 

고정된 수치 비율로 나오기 위해서는 다음과 같은 코드 처리가 필요하다.

반응형

 

GUI.matrix의 값을 기준 해상도로 고정을 하면 해상도에 변하지 않고 고정적인 코드 처리가 가능하다.

  void OnGUI(){
	 GUI.matrix = Matrix4x4.TRS(new Vector3(0, 0, 0), Quaternion.identity, new Vector3(scaleFactor.x, scaleFactor.y, 1));
     ...
   }

    public Vector3 scaleFactor {
        get
        {
            float normalWidth = 720;
            float normalHgiht = 1280;
 
            return new Vector3(Screen.width / normalWidth, Screen.height / normalHgiht);
        }
    }

 

 

Unity IMGUI(Immediate Mode GUI) : [ 링크 ]

 

Unity - Manual: Immediate Mode GUI (IMGUI)

Creating Screen Transitions How do you use documentation throughout your workflow? Share your experience with us by taking this survey. Immediate Mode GUI (IMGUI) The “Immediate Mode” GUI system (also known as IMGUI) is an entirely separate feature to

docs.unity3d.com

 

★☆☆☆☆

 

반응형

댓글