- 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:

- Java面向?qū)ο筌浖_(kāi)發(fā)
- C# Programming Cookbook
- Hands-On Image Processing with Python
- INSTANT Sencha Touch
- 營(yíng)銷(xiāo)數(shù)據(jù)科學(xué):用R和Python進(jìn)行預(yù)測(cè)分析的建模技術(shù)
- Visual Basic程序設(shè)計(jì)與應(yīng)用實(shí)踐教程
- Bootstrap 4:Responsive Web Design
- 青少年P(guān)ython編程入門(mén)
- Jenkins Continuous Integration Cookbook(Second Edition)
- Java EE 8 Application Development
- UVM實(shí)戰(zhàn)
- Java圖像處理:基于OpenCV與JVM
- Java 從入門(mén)到項(xiàng)目實(shí)踐(超值版)
- Secret Recipes of the Python Ninja
- STM8實(shí)戰(zhàn)