Skip to content

Instantly share code, notes, and snippets.

@todorok1
Created May 30, 2018 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save todorok1/8c78751dffbeecdeb7e696016e2e91b5 to your computer and use it in GitHub Desktop.
Save todorok1/8c78751dffbeecdeb7e696016e2e91b5 to your computer and use it in GitHub Desktop.
プロファイラから前フレームの処理時間を読み取ってフレームレートの低下を検知するスクリプト
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