プロファイラから前フレームの処理時間を読み取ってフレームレートの低下を検知するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using UnityEngine.Profiling; | |
public class CollisionPerformanceChecker : MonoBehaviour { | |
GenerateMolecule gm; | |
Dictionary<int, bool> flags = new Dictionary<int, bool>(); | |
int[] fpsBases = new int[]{30, 15, 10, 5, 1}; | |
Recorder fixedUpdateRecorder; | |
void Start(){ | |
// 気体分子を生成するスクリプトへの参照をセット | |
gm = gameObject.GetComponent<GenerateMolecule>(); | |
// Recorderへの参照をセット | |
fixedUpdateRecorder = Recorder.Get("Physics.Processing"); | |
fixedUpdateRecorder.enabled = true; | |
// フラグ辞書の初期化 | |
foreach (int fps in fpsBases){ | |
flags.Add(fps, false); | |
} | |
} | |
void Update(){ | |
// 開始直後は処理時間が安定しないのでreturn | |
if (Time.frameCount < 10){ | |
return; | |
} | |
// Profilerが使える環境でなければreturn | |
if (!fixedUpdateRecorder.isValid){ | |
return; | |
} | |
// 前フレームの処理時間を取得(ナノ秒で取得するので、秒に変換する) | |
float time = fixedUpdateRecorder.elapsedNanoseconds / 1000000000f; | |
foreach(int fps in fpsBases){ | |
// 処理時間が基準値を超えているかの確認 | |
if (time > GetProcessingBaseTime(fps) && !flags[fps]){ | |
// Consoleにメッセージを出力する | |
OutputMessage(fps); | |
// フラグをtrueにセット。1回だけメッセージを出力する | |
flags[fps] = true; | |
} | |
} | |
} | |
float GetProcessingBaseTime(int fps){ | |
float time = 1.0f / fps; | |
return time; | |
} | |
void OutputMessage(int fps){ | |
Debug.Log(fps.ToString() + "FPSを下回った。フレームカウントは : " + Time.frameCount + " / オブジェクト数は : " + gm.moleculeCount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment