Unity Performance and Optimization

7 min. read

Unity Performance and Optimization
Common Causes
Poorly structured code
Garbage collection
Expensive operations (/ vs X)
Demanding scripts
Code for readability -> Profile -> Optimize

Our Notes

Project Setup
BitBucket

Project Architecture
BitBucketFolder
Gamename-platform
Assets
Project Settings

Unity Profiler
For Scripts, only CPU Usage and Memory Usage is needed.

CPU Usage

Change the bottom window to (Hierarchy)

Programming
Multiple instead of divide.

Memory Management
https://www.gamasutra.com/blogs/WendelinReich/20131109/203841/C_Memory_Management_for_Unity_Developers_part_1_of_3.php

https://www.slideshare.net/williamyang3910/unitekorea2013-memory-profiling-in-unity

http://richg42.blogspot.com/2015/05/lessons-learned-while-fixing-memory.html

https://stackoverflow.com/questions/41768846/unity-profiling-my-scripts-memory-usage

https://forum.unity.com/threads/ios-large-memory-usage-apparently-feature-not-a-bug.637660/

https://forum.unity.com/threads/unityvideo-player-memory-leak-in-pc-builds.504617/

https://forum.unity.com/threads/video-player-memory-issues-can-i-get-some-info-about-memory-management.455060/

https://thoughtbot.com/blog/avoiding-out-of-memory-crashes-on-mobile

https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html

https://docs.unity3d.com/ScriptReference/Networking.DownloadHandlerBuffer.html

https://docs.unity3d.com/Manual/UnityWebRequest-CreatingDownloadHandlers.html

https://github.com/RenderHeads/UnityPlugin-AVProVideo/issues/39

https://stackoverflow.com/questions/42944713/unity-5-6-ram-usage-continues-to-grow

https://dzone.com/articles/avoiding-out-of-memory-crashes-on-mobile

https://www.reddit.com/r/Unity3D/comments/5p3q6w/are_memory_leaks_an_issue_with_unity_c/

https://www.quora.com/How-can-we-solve-the-iOS-app-crashing-issue-made-by-Unity-3D

https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream.read?view=netframework-4.8

https://stackoverflow.com/questions/16862782/streaming-large-video-files-net

https://docs.microsoft.com/en-us/dotnet/api/system.io.memorystream?redirectedfrom=MSDN&view=netframework-4.8

https://www.gamasutra.com/blogs/EmilianoPastorelli/20170627/300585/The_memory_optimization_struggle_in_Unity3d.php

Get Total Memory

long memoryUsed = System.GC.GetTotalMemory(true);

float megaBytes = Mathf.Pow(2, 20);

var memoryUsedInMb = memoryUsed / megaBytes;

Avoiding Out of Memory Crashes on Mobile

Refs:
https://thoughtbot.com/blog/avoiding-out-of-memory-crashes-on-mobile

Performance

https://www.gamedev.net/blogs/entry/2267151-battletech-developer-journal-04/?utm_source=newsletter&utm_medium=email&utm_name=gamedevdirect

https://catlikecoding.com/unity/tutorials/frames-per-second/

https://learn.unity.com/tutorial/fixing-performance-problems#

https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity.html

https://www.gamasutra.com/blogs/WendelinReich/20131109/203841/C_Memory_Management_for_Unity_Developers_part_1_of_3.php

https://www.gamasutra.com/blogs/GrhyllJDD/20160119/263849/Reducing_memory_allocations_to_avoid_Garbage_Collection_on_Unity.php

https://www.gamasutra.com/blogs/MeganHughes/20150727/249375/Unity_Garbage_Collection_Tips_and_Tricks.php

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing

https://learn.unity.com/tutorial/live-sessions-on-scripting#5c7f8528edbc2a002053b628

https://en.wikipedia.org/wiki/Object_pool_pattern

Profiling
https://github.com/ahmedmohi/iProfiler/blob/master/Assets/iProfiler/StatsMan.cs

Images
var textures = Resources.FindObjectsOfTypeAll(typeof(Texture2D));
int totalBytes = 0;
foreach (Texture2D t in textures)
{
int memorySize = Profiler.GetRuntimeMemorySize(t);
if (memorySize > 0.4f)
Debug.Log(“Texture object “ + t.name + “(“ + t.width + “x” + t.height + “|” + t.format + “) using: “ + memorySize + “Bytes”, t);
totalBytes += memorySize;
}
Debug.Log(“Total bytes: “ + totalBytes);

Build Size
Binary Size

https://www.youtube.com/watch?time_continue=344&v=4JLpJHIdx7E

https://files.unity3d.com/jonas/BuildReportInspector.zip

Framerate and VSync

Set VSync to 0

Optimization Tips

https://unity3d.com/how-to/fast-optimizations-for-AR?utm_campaign=saas_global_nurture_2019-04-Paid-subs-CLC-monthly&utm_content=2019-CLC-Monthly-Optimization-tips-for-AR&utm_medium=email&utm_source=Eloqua

https://unity3d.com/how-to/unity-ui-optimization-tips?fbclid=IwAR0tYJSpcjLoRUwVAXw3-fWGUmlStILiNSonpTNdGNGr6GEqsbqOKxFCWII

Project Setup
BitBucket

Project Architecture
BitBucketFolder
Gamename-platform
Assets
Project Settings

Unite Europe 2017 - Squeezing Unity: Tips for raising performance
https://www.youtube.com/watch?v=_wxitgdx-UI

Make a game fun, then make it fast

UI is shit!!!!
https://www.slideshare.net/secret/xLig7llwdlRQIp
https://www.youtube.com/watch?v=_wxitgdx-UI
https://www.slideshare.net/secret/xLig7llwdlRQIp
https://www.youtube.com/watch?v=c6sLiFIFd8o

https://www.youtube.com/watch?v=eH-PdFKgctE

https://www.youtube.com/watch?v=1e5WY2qf600
https://www.youtube.com/watch?v=j4YAY36xjwE&t=1765s
https://www.youtube.com/watch?v=1e5WY2qf600

References

https://www.youtube.com/watch?v=kML67qB9Chk

https://blog.unity.com/technology/customize-your-memory-use-with-unity-2021-lts

https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php

https://www.gamasutra.com/blogs/EnriqueJGil/20160808/278440/Unity_Builds_Scripting_Basic_and_advanced_possibilities.php

https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php

http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/

https://www.reddit.com/r/gamedev/comments/4xyxeh/50_tips_and_best_practices_for_unity_2016_edition/

http://portal.babelx3d.net/content/50-tips-and-best-practices-unity

http://wiki.unity3d.com/index.php?title=General_Performance_Tips

https://www.3dbuzz.com/forum/threads/193421-50-Tips-for-Working-with-Unity-(Best-Practices)

http://unitytip.com/

https://www.scribd.com/document/327675440/50-Tips-and-Best-Practices-for-Unity-2016-Edition-pdf

https://www.pinterest.com/pin/540713498993949780/

https://www.reddit.com/r/Unity3D/comments/5qwdhy/top_10_unity_tips_2017/

https://blogs.unity3d.com/2017/08/10/spotlight-team-best-practices-project-setup/

https://blogs.unity3d.com/2017/06/29/best-practices-from-the-spotlight-team-optimizing-the-hierarchy/

https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php

https://www.gamasutra.com/blogs/EnriqueJGil/20160808/278440/Unity_Builds_Scripting_Basic_and_advanced_possibilities.php

https://www.gamasutra.com/blogs/HermanTulleken/20160812/279100/50_Tips_and_Best_Practices_for_Unity_2016_Edition.php

http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/

https://www.reddit.com/r/gamedev/comments/4xyxeh/50_tips_and_best_practices_for_unity_2016_edition/

http://portal.babelx3d.net/content/50-tips-and-best-practices-unity

http://wiki.unity3d.com/index.php?title=General_Performance_Tips

https://www.3dbuzz.com/forum/threads/193421-50-Tips-for-Working-with-Unity-(Best-Practices)

http://unitytip.com/

https://www.scribd.com/document/327675440/50-Tips-and-Best-Practices-for-Unity-2016-Edition-pdf

https://www.pinterest.com/pin/540713498993949780/

https://www.reddit.com/r/Unity3D/comments/5qwdhy/top_10_unity_tips_2017/

https://blogs.unity3d.com/2017/08/10/spotlight-team-best-practices-project-setup/

https://blogs.unity3d.com/2017/06/29/best-practices-from-the-spotlight-team-optimizing-the-hierarchy/

Unite Austin 2017 - The Walking Dead: March to War - Rendering 10K Objects on Low-End Mobile Devices
https://youtu.be/hUZbkqLRYus

https://www.gamasutra.com/blogs/GrhyllJDD/20160119/263849/Reducing_memory_allocations_to_avoid_Garbage_Collection_on_Unity.php

https://docs.microsoft.com/en-us/windows/mixed-reality/develop/unity/performance-recommendations-for-unity

From Rider:
https://github.com/JetBrains/resharper-unity/wiki/Performance-critical-context-and-costly-methods

https://github.com/JetBrains/resharper-unity/wiki/Possible-unintended-bypass-of-lifetime-check-of-underlying-Unity-engine-object