Skip to content

Instantly share code, notes, and snippets.

@Suzeep
Last active May 26, 2017 07:36
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 Suzeep/b73571aec086df731495 to your computer and use it in GitHub Desktop.
Save Suzeep/b73571aec086df731495 to your computer and use it in GitHub Desktop.
FPS、メモリ使用量等を画面に表示するスクリプト(訂正版)。
using UnityEngine;
using UnityEngine.Profiling;
using System.Collections;
using System.Text;
using System.Linq;
[ExecuteInEditMode()]
//=====================================================================================
//=====================================================================================
// class ProfileStats
//=====================================================================================
public class ProfileStats : MonoBehaviour
{
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
// constant
//-------------------------------------------------------------------------------------
public const int SIZE_MB = 1024 * 1024;
//-------------------------------------------------------------------------------------
// Start
//-------------------------------------------------------------------------------------
void Start()
{
useGUILayout = false;
m_SlicedTimer = 0.50f;
}
//-------------------------------------------------------------------------------------
// OnGUI
//-------------------------------------------------------------------------------------
void OnGUI()
{
// profile
profileStats();
// draw
if( m_IsShow ){
if( Application.isPlaying ){
drawStats();
}else{
if( m_IsShowEditor ){
drawStats();
}
}
}
}
//-------------------------------------------------------------------------------------
// profile stats
//-------------------------------------------------------------------------------------
private void profileStats()
{
// check GC
int collCount = System.GC.CollectionCount( 0 );
if( m_LastCollectNum != collCount )
{
m_LastCollectNum = collCount;
m_CollectDeltaTime = Time.realtimeSinceStartup - m_LastCollectTime;
m_LastCollectTime = Time.realtimeSinceStartup;
m_LastCollectDeltaTime = Time.deltaTime;
m_CollectMemSize = m_UsedMemSize;
}
// check memory
m_UsedMemSize = System.GC.GetTotalMemory( false ); // byte
if( m_UsedMemSize > m_MaxUsedMemSize ){
m_MaxUsedMemSize = m_UsedMemSize;
}
// calc alloc rate
if( (Time.realtimeSinceStartup - m_LastAllocSet) > 0.3f )
{
long diff = m_UsedMemSize - m_LastAllocMemSize;
m_LastAllocMemSize = m_UsedMemSize;
m_LastAllocSet = Time.realtimeSinceStartup;
if( diff >= 0 ){
m_AllocRate = diff;
}
}
// FPS
m_StrFPS = (1f / Time.deltaTime).ToString("0.000");
// stok sliced FPS
m_SlicedTimer -= Time.deltaTime;
if( m_SlicedTimer < 0.0f )
{
m_SlicedTimer = 0.50f;
m_StrSlicedFPS = m_StrFPS;
}
}
//-------------------------------------------------------------------------------------
// draw stats
//-------------------------------------------------------------------------------------
private void drawStats()
{
string left_text = "";
string right_text = "";
// FPS
string str_fps = string.Format( "{0} FPS ({1})\n", m_StrFPS, m_StrSlicedFPS );
// Base
string str_membase = "";
str_membase += "Used\nMax Used\nUsed Heap(Unity)\nMono Used\nMono Heap\n";
string str_membase_val = "";
str_membase_val = string.Format(
"{0} MB\n{1} MB\n{2} MB\n{3} MB\n{4} MB\n",
((float)m_UsedMemSize / SIZE_MB).ToString("0.00"),
((float)m_MaxUsedMemSize / SIZE_MB).ToString("0.00"),
((float)Profiler.usedHeapSize / SIZE_MB).ToString("0.00"),
((float)Profiler.GetMonoUsedSize() / SIZE_MB).ToString("0.00"),
((float)Profiler.GetMonoHeapSize() / SIZE_MB).ToString("0.00")
);
// Sub
string str_memsub = "";
string str_memsub_val = "";
if( m_IsShowSubMemory ){
str_memsub += "Total Alloc\n Unused-Reserved\n Reserved\n";
str_memsub_val = string.Format(
"{0} MB\n{1} MB\n{2} MB\n",
((float)Profiler.GetTotalAllocatedMemory() / SIZE_MB).ToString("0.00"),
((float)Profiler.GetTotalUnusedReservedMemory() / SIZE_MB).ToString("0.00"),
((float)Profiler.GetTotalReservedMemory() / SIZE_MB).ToString("0.00")
);
}
// for GC
string str_gc = "";
string str_gc_val = "";
if( m_IsShowGC ){
str_gc += "Allocation Rate\n LastCollectionDelta\n Collection Freq\n";
str_gc_val = string.Format(
"{0} MB\n{1} s ({2} fps)\n{3} s\n",
((float)m_AllocRate / SIZE_MB).ToString ("0.00"),
m_LastCollectDeltaTime.ToString("0.000"),
(1f / m_LastCollectDeltaTime).ToString("0.0"),
m_CollectDeltaTime.ToString("0.00")
);
}
// System
string str_sys = "";
string str_sys_val = "";
if( m_IsShowSystem ){
str_sys += "Graphic\nSystem\n";
str_sys_val = string.Format(
"{0} MB\n{1} MB\n",
SystemInfo.graphicsMemorySize.ToString(),
SystemInfo.systemMemorySize.ToString()
);
}
// cat string
left_text = string.Format( "{0}{1}{2}{3}{4}", str_fps, str_membase, str_memsub, str_gc, str_sys );
right_text = string.Format( "\n{0}{1}{2}{3}", str_membase_val, str_memsub_val, str_gc_val, str_sys_val );
// draw background box
int lineCount = left_text.ToList().Where(c => c.Equals('\n')).Count();
GUI.Box( new Rect(5,5,270,(int)(m_FontSizeBase * lineCount) + 5), "" );
// draw left label
GUI.Label( new Rect(10,5,Screen.width,Screen.height), left_text.ToString() );
// draw right label
GUI.Label( new Rect(160,5,Screen.width,Screen.height), right_text.ToString() );
}
//=====================================================================================
// member
//=====================================================================================
public bool m_IsShow = true;
public bool m_IsShowSubMemory = false;
public bool m_IsShowGC = false;
public bool m_IsShowSystem = false;
public bool m_IsShowEditor = false;
public float m_FontSizeBase = 15.175f;
private long m_UsedMemSize;
private long m_MaxUsedMemSize;
private long m_CollectMemSize;
private float m_LastCollectTime;
private float m_LastCollectNum;
private float m_CollectDeltaTime;
private float m_LastCollectDeltaTime;
private long m_AllocRate;
private long m_LastAllocMemSize;
private float m_LastAllocSet = -9999.0f;
private string m_StrFPS = "0.000";
private string m_StrSlicedFPS = "0.000";
private float m_SlicedTimer;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment