I recently moved my Player’s UI to be nested under the Player Object
I was wondering if this is bad for performance? Since my player is moving around a lot, I’d hate for the large amount of UI objects to be hindering performance.
Is it better if my UI stays separate from the player object? I hope not, as I kind of like having it on the player itself.
It certainly can matter because it can change the rules of when local meshes in the UI get rebuilt.
DO NOT OPTIMIZE “JUST BECAUSE…” If you don’t have a problem, DO NOT OPTIMIZE!
If you DO have a problem, there is only ONE way to find out. Always start by using the profiler:
Window → Analysis → Profiler
Failure to use the profiler first means you’re just guessing, making a mess of your code for no good reason.
Not only that but performance on platform A will likely be completely different than platform B. Test on the platform(s) that you care about, and test to the extent that it is worth your effort, and no more.
Remember that optimized code is ALWAYS harder to work with and more brittle, making subsequent feature development difficult or impossible, or incurring massive technical debt on future development.
Notes on optimizing UnityEngine.UI setups:
At a minimum you want to clearly understand what performance issues you are having:
running too slowly?
loading too slowly?
using too much runtime memory?
final bundle too large?
too much network traffic?
something else?
If you are unable to engage the profiler, then your next solution is gross guessing changes, such as “reimport all textures as 32x32 tiny textures” or “replace some complex 3D objects with cubes/capsules” to try and figure out what is bogging you down.
Each experiment you do may give you intel about what is causing the performance issue that you identified. More importantly let you eliminate candidates for optimization. For instance if you swap out your biggest textures with 32x32 stamps and you STILL have a problem, you may be able to eliminate textures as an issue and move onto something else.
This sort of speculative optimization assumes you’re properly using source control so it takes one click to revert to the way your project was before if there is no improvement, while carefully making notes about what you have tried and more importantly what results it has had.
I keep commonly moving UI parts in a seperate canvas, (such as moving an item to a new inventory slot). I was afraid that the UI was going to need to be recalculated whenever the player moves, but I don’t think that is the case. I checked the profiler and I didn’t notice any difference in putting the UI under the player object, so I’ll stick with that and run with it! and I totally agree with the pre-optimizations. It’s hard enough to make a game, no reason to get caught up in optimizations that won’t matter