본문 바로가기
개발/코드

코드) Unity-FPS 표기 (Frame Per Second)

by 테샤르 2022. 10. 27.

Unity-FPS 표기 (Frame Per Second)

 

개발을 하다 보면 FPS (Frame Per Second)를 출력해서 프레임 드롭이 생기는지 포인트를 확인해야 할 경우가 생긴다.

이럴 때 기본적으로 FPS 코드를 GUI로 표기하면 확인이 가능하다.

반응형

실제 서비스 빌드에서는 해당 디버깅 코드를 출력하지 않고 테스트용도 사용하는것을 추천드린다.

 

public class FPS : MonoBehaviour
{
    [Range(1,100)]
    public int m_fFontSize = 40;

    [Range(0,1)]
    public float Red, Green, Blue;

    float deltaTime =0.0f;

    #region Unity Method

    void Start()
    {
        this.m_fFontSize= (this.m_fFontSize == 0) ? 50 : this.m_fFontSize;
        this.Red = (this.Red == 0 )? 50 : this.Red;
    }
    
    void Update()
    {
        deltaTime +=(Time.unscaledDeltaTime-deltaTime)*0.1f;
    }

    #endregion

    #region Private Method

    #endregion
    
    #region Public Method
    void OnGUI() {
        if(true  == Define.BUILD_DEBUG){
            int width = Screen.width;
            int height = Screen.height;

            GUIStyle style = new GUIStyle();
            Rect rect = new Rect(0,0,width,height*2/100);
            style.alignment = TextAnchor.UpperLeft;
            style.fontSize = height  * 2 / this.m_fFontSize;
            style.normal.textColor = new Color(Red,Green,Blue, 1.0f);
            float mesc = deltaTime * 1000.0f;
            float fps = 1.0f/ deltaTime;
            string text = string.Format("{0:0.0} ms ({1:0.} fps)", mesc,fps);
            GUI.Label(rect,text,style);
        }
    }
    #endregion
}

 

 

반응형

댓글