unity獲取某個方法執行時間
阿新 • • 發佈:2019-02-07
1、Time.time
在一幀內,Time.time只會賦值更新一次,所以一幀內獲取不到時間差值。
2、Stopwatch
例項可以測量一個時間間隔的執行時間,也可以測量多個時間間隔的總執行時間。呼叫Start方法時,開始累積執行時間計數;呼叫Stop方法時,結束當前時間間隔測量,並凍結累積執行時間值;呼叫Reset方法可以清除現有例項中的累積執行時間。通過屬性Elapsed、ElapsedMilliseconds、ElapsedTicks查詢執行時間值。
3、Profiler
開啟Profiler介面,使用Profiler.BeginSample和Profiler.EndSample檢視一幀執行時間。
<span style="font-family:SimSun;font-size:14px;"><span style="color:#333333;">void Start() { float t = Time.time; Check(); Debug.Log( string.Format( "total: {0} ms", Time.time - t ) ); Stopwatch sw = new Stopwatch(); sw.Start(); Check(); sw.Stop(); Debug.Log( string.Format( "total: {0} ms", sw.ElapsedMilliseconds ) ); Profiler.BeginSample( "TestMethod" ); Check(); Profiler.EndSample(); } void Check() { for( int i = 0; i < 1000000; i++ ) { } }</span></span>