Unity Code Optimization - Do you know them all?
Video Statistics and Information
Channel: Tarodev
Views: 34,099
Rating: undefined out of 5
Keywords: unity optimization, code optimization, unity code, unity tips, speed up code, speed up unity, unity efficient code, coding tips, unity coding tips, unity code optimizations, sendmessage, vector3.distance, game development, unity tutorial, unity 3d, unity performance, improve performance
Id: Xd4UhJufTx4
Channel Id: undefined
Length: 15min 49sec (949 seconds)
Published: Sun Mar 13 2022
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
Before anyone now goes through their Unity project and spends hours upon hours on applying each of these things and in the process completely messes up the readability of their codebase and introduces a dozen bugs: Don't optimize what doesn't need to be optimized!
Use the Profiler to find out which parts of your code are actually responsible for the most processing time. Then see what you can do performance-wise for those particular code sections. When there is some code which only runs once every couple updates, then it makes no difference whether it takes 200 microseconds or 500 microseconds.
"... kaesh ... kaesh ... kaesh"
Say it one more time i dare you!
Linq gets lots of hate for no reason. If you're sorting/ordering something, its not usually at a performance critical state in your game, at least shouldnt be.
The comparison between finding by tag and finding by type is a bit misleading because usually if you're finding by type it's because you're interested in a specific component, not a specific game object. The fastest way to get a specific component might still be to use tags on GameObjects and then use GetComponent from there, but I expect it depends on a few other factors.
If you're serious about this, you have to compare the performances in builds not in the editor. I don't know about FindObjectsByType specifically, but similar functions are significally slower in the editor, and generate garbage too.
You should also try these in a build with il2cpp. Caching in particular, will yield different results.
One to test is TryGetComponent(out component) vs GetComponent where you are also checking if the component is on the GameObject after the call.
Since you need to call
if(component != null)
in this case is it faster to use TryGetComponent?How is assigning
transform
to a local variable ("caching it") mitigating the performance cost of the property accesses? It's still an instance ofTransform
and the implementation isn't changing, no?You put out good content, Taro! Keep it up.