Unity FPS カウンター

ビデオ ゲーム では、1 秒あたりのフレーム数 (または略して fps) は、コンピューターが 1 秒間にレンダリングするフレーム数を表す値です。

1 秒あたりのフレーム数 はパフォーマンスの優れた指標であり、最適化 プロセス中に使用したり、単にゲームの実行速度/スムーズさに関するフィードバックを得るために使用したりできます。

このチュートリアルでは、Unity のゲームに簡単な fps カウンターを追加する方法を説明します。

ステップ

game で fps を表示するには、フレームをカウントして画面に表示するスクリプトを作成する必要があります。

  • という新しいスクリプトを作成し、"SC_FPSCounter" という名前を付けて、その中に以下のコードを貼り付けます。

SC_FPSCounter.cs

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

public class SC_FPSCounter : MonoBehaviour
{
    /* Assign this script to any object in the Scene to display frames per second */

    public float updateInterval = 0.5f; //How often should the number update

    float accum = 0.0f;
    int frames = 0;
    float timeleft;
    float fps;

    GUIStyle textStyle = new GUIStyle();

    // Use this for initialization
    void Start()
    {
        timeleft = updateInterval;

        textStyle.fontStyle = FontStyle.Bold;
        textStyle.normal.textColor = Color.white;
    }

    // Update is called once per frame
    void Update()
    {
        timeleft -= Time.deltaTime;
        accum += Time.timeScale / Time.deltaTime;
        ++frames;

        // Interval ended - update GUI text and start new interval
        if (timeleft <= 0.0)
        {
            // display two fractional digits (f2 format)
            fps = (accum / frames);
            timeleft = updateInterval;
            accum = 0.0f;
            frames = 0;
        }
    }

    void OnGUI()
    {
        //Display the fps and round to 2 decimals
        GUI.Label(new Rect(5, 5, 100, 25), fps.ToString("F2") + "FPS", textStyle);
    }
}
  • SC_FPSCounter スクリプトをシーン内の任意のオブジェクトにアタッチし、Play を押します。

1 秒あたりのフレーム数

fps が左上隅に表示されるはずです。

おすすめの記事
ゾーン コントローラー プロ - Unity アセット ストア パッケージ
Unityでライトスイッチを作成するためのスクリプト
Unity での 2D コイン収集
Unity で新しい HDRP 給水システムを使用する方法
FPC Swimmer - 没入型水環境向けの包括的な Unity アセット
Ultimate Spawner 2.0 - 革新的な資産
Unity 用のマウスの外観スクリプト