- Unity Game Optimization
- Dr. Davide Aversa Chris Dickinson
- 279字
- 2021-06-24 12:13:00
Profiler script control
The Profiler can be controlled in script code through the Profiler class. There are several useful methods in this class that we can explore within the Unity documentation, but the most important methods are the delimiter methods that activate and deactivate profiling at runtime. These can be accessed through the UnityEngine.Profiling.Profiler class through its BeginSample() and EndSample() methods.
The BeginSample() method has an overload that allows a custom name for the sample to appear in the CPU Usage area's Hierarchy mode. For example, the following code will profile invocations of this method and make the data appear in Breakdown View under a custom heading, as follows:
void DoSomethingCompletelyStupid() {
Profiler.BeginSample("My Profiler Sample");
List<int> listOfInts = new List<int>();
for(int i = 0; i < 1000000; ++i) {
listOfInts.Add(i);
}
Profiler.EndSample();
}
We should expect that invoking this poorly designed method (which generates a List containing a million integers, and then does absolutely nothing with it) will cause a huge spike in CPU Usage, chew up several megabytes of memory, and appear in the Profiler Breakdown View under the My Profiler Sample heading, as shown in the following screenshot:

- 深入理解Android(卷I)
- Learn Blockchain Programming with JavaScript
- 劍指JVM:虛擬機實踐與性能調優
- 青少年美育趣味課堂:XMind思維導圖制作
- 網頁設計與制作教程(HTML+CSS+JavaScript)(第2版)
- Learning JavaScript Data Structures and Algorithms
- Python忍者秘籍
- PHP從入門到精通(第4版)(軟件開發視頻大講堂)
- 零基礎Java學習筆記
- Visual Foxpro 9.0數據庫程序設計教程
- 零代碼實戰:企業級應用搭建與案例詳解
- JavaScript+jQuery網頁特效設計任務驅動教程
- 小程序從0到1:微信全棧工程師一本通
- Building Slack Bots
- ABAQUS6.14中文版有限元分析與實例詳解