- Unity 2017 Game Optimization(Second Edition)
- Chris Dickinson
- 277字
- 2021-07-02 23:21:05
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:

- Visual C++串口通信開(kāi)發(fā)入門(mén)與編程實(shí)踐
- Magento 2 Theme Design(Second Edition)
- Python高級(jí)編程
- C語(yǔ)言程序設(shè)計(jì)
- Java加密與解密的藝術(shù)
- Node.js Design Patterns
- Go語(yǔ)言精進(jìn)之路:從新手到高手的編程思想、方法和技巧(1)
- HTML 5與CSS 3權(quán)威指南(第3版·上冊(cè))
- Lighttpd源碼分析
- Mastering Git
- Getting Started with Python
- 寫(xiě)給青少年的人工智能(Python版·微課視頻版)
- Docker on Windows
- jQuery EasyUI從零開(kāi)始學(xué)
- R High Performance Programming