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

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 the 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 heading My Profiler Sample, as the following screenshot shows:

主站蜘蛛池模板: 恩施市| 道孚县| 和林格尔县| 察雅县| 塔河县| 裕民县| 双流县| 衡山县| 象山县| 吴旗县| 余干县| 津南区| 光山县| 新乐市| 江都市| 娄底市| 安塞县| 沁水县| 肥城市| 新晃| 阳山县| 林周县| 普陀区| 蒙自县| 沙河市| 咸阳市| 偃师市| 涿州市| 兴国县| 驻马店市| 万山特区| 芜湖县| 德州市| 紫阳县| 鄂托克旗| 上犹县| 阿拉善盟| 九寨沟县| 仙桃市| 西丰县| 林周县|