유니티

[ Unity ] UI를 사용해 FPS를 보는 방법.

Hexs 2023. 4. 9. 15:12
반응형

 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class FPS : MonoBehaviour
{
    [SerializeField] float fps;
    [SerializeField] float ms;
    float deltaTime;
    [SerializeField] GameObject UIM_fram;

    private void Awake()
    {
        Application.targetFrameRate = 60; // 프레임을 60으로 제한
        Screen.SetResolution(1920, 1080, false); // 화면크기 조정.
    }

    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        deltaTime += (Time.deltaTime - deltaTime) * 0.1f;
        fps_();
    }

    void fps_()
    {
        fps = 1.0f / deltaTime;
        ms = deltaTime * 1000.0f;
        string text = string.Format("({1:0.} ms) {0:0.0} fps", fps, ms);
        if (fps > 50)
        {
            UIM_fram.transform.GetComponent<TextMeshProUGUI>().text = "<color=#0000FF>" + text + "</color>";
        }
        else if (fps > 40)
        {
            UIM_fram.transform.GetComponent<TextMeshProUGUI>().text = "<color=#9400D3>" + text + "</color>";
        }
        else if (fps > 30)
        {
            UIM_fram.transform.GetComponent<TextMeshProUGUI>().text = "<color=#87CEFA>" + text + "</color>";
        }
        else
        {
            UIM_fram.transform.GetComponent<TextMeshProUGUI>().text = "<color=#FF0000>" + text + "</color>";
        }

    }
}

 

 

이런식으로.

모바일이나. 유니티 상에 게임플레이 화면에서 프레임을 볼 수 있다.

반응형