官术网_书友最值得收藏!

  • 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.

Note that the delimiter methods, BeginSample() and EndSample(), are only compiled in development builds, and, as such, they will not be compiled or executed in release builds where  Development Mode is unchecked. This is commonly known as non-operation, or no-op, code.

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();
}
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files emailed directly to you.

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:

主站蜘蛛池模板: 华池县| 韩城市| 上思县| 新密市| 兴安县| 中山市| 遂平县| 顺义区| 武川县| 邯郸市| 苗栗县| 达日县| 丹棱县| 通许县| 佳木斯市| 和平区| 犍为县| 梨树县| 报价| 田东县| 灌南县| 永善县| 开平市| 右玉县| 清镇市| 山东| 汝阳县| 马龙县| 林西县| 绥宁县| 紫云| 兴和县| 抚顺市| 嘉定区| 克什克腾旗| 城口县| 重庆市| 宁海县| 古田县| 信阳市| 江陵县|