2. Valentin Simonov — Field Engineer
Email: val@unity3d.com
• I help studios make games better
• I teach people how to use Unity efficiently
(conferences, trainings, blog posts, translated a book on shaders)
• I maintain https://github.com/TouchScript/
7. 1. Get data about your project (tools)
2. Interpret that data (knowledge)
3. Make a list of possible optimizations (management)
4. Perform necessary* optimizations (business)
8. 1. Get data about your project (tools)
2. Interpret that data (knowledge)
3. Make a list of possible optimizations (management)
4. Perform necessary* optimizations (business)
9. How to get correct data and eliminate guess work.
1. Unity Profiler
2. Unity Frame Debugger
3. Unity Memory Profiler
( https://bitbucket.org/Unity-Technologies/memoryprofiler )
4. Platform tools
10. How to make correct decisions based on collected data.
1. Blogs, forums, learning materials
( https://unity3d.com/learn/tutorials/topics/best-practices )
2. Videos from conferences
3. Trainings
11.
12. • Shows code execution time and GC allocations
• Can be connected to a remote device
• Deep Profile in Editor
31. Using Resources folder is generally not advised:
1. All “Resources” folders are packed in a monolithic binary
2. Rebuilt every time even if nothing has changed
3. 4 Gb size limit, uncompressed
4. More assets -> slower loading
5. At start time Unity builds index and always keeps it in memory
32. • 12 000 1px images in Resources
• Look for ResourceManager::Transfer
• CPU time depends on device (iPhone 4s on the pic below)
36. Keep in mind platform specific behaviors.
For example, that iOS caches compiled shaders.
37. Question: What is the fastest way to dynamically create 1000 complex
objects?
Complex object — 10 children with 10 components each.
1. Create 10 children, add 10 components to each, repeat.
2. Create 1 template object with 10 children, clone it 999 times.
3. Instantiate a prefab, repeat.
Target device: iPhone 6, Unity 5.4.1
38. 1. Create 10 children, add 10 components to each, repeat.
1562.250 ms
2. Create 1 template object with 10 children, clone it 999 times.
451.394 ms
3. Instantiate a prefab, repeat.
485.250 ms
4. Clone a prefab, instantiate the clone 999 times.
432 ms
39. 1. Create 10 children, add 10 components to each, repeat: 1562.250 ms
40. 2. Create 1 template object with 10 children, clone it 9999 times.
451.394 ms
41. 1. Create 1 template object with 10 children, clone it 999 times.
440.593 451.394 ms
2. Clone a prefab, instantiate the clone 999 times.
422.5 432 ms
3. Instantiate a DISABLED prefab, repeat.
412.5 422.5 ms
44. 1. There’s native (C++) and managed (C#) memory
2. Assets live in native memory
3. Managed memory has light wrappers for assets
Texture2D myTexture = …
(a) myTexture = null; — wrapper is GCd, texture data is stuck
(b) Destroy(myTexture); — wrapper is stuck, texture data is freed
(c) Resources.UnloadUnusedAssets(); — destroys stuck assets
(d) GC.Collect(); — GCs stuck wrappers (at this point destructors are called)
46. • Unity is a C++ engine with a .NET virtual machine
• Calling Managed functions from Native involves some overhead
• Every Awake/Start/Update* call is a call from Native to Managed
• ... even empty ones o.O
• At some point you will start noticing this overhead
48. • Find, FindObjectOfType — super slow!
• Camera.main doesn’t cache the main camera
• APIs which return arrays allocate memory every time
49. 1. Unity open source projects
https://bitbucket.org/Unity-Technologies/ui
https://github.com/Unity-Technologies/PostProcessing
2. UnityEngine.dll -> ILSPY
http://va.lent.in/unity-make-your-lists-functional-with-reorderablelist/
3. Time Profiler instrument and other tools.