I have a very large 2d array that I need to store in my game.
I flatten the array so that it can be serialized(store a 1d array in a class, and make an array of that class).
But the loading time of the scene is very long in editor(pretty fast in built).
Is it faster to flatten and serialize the 2d array compared to creating the 2darray and filling the data one by one in Awake?
For performance questions always start your investigation with 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.
What you are talking about is finding out what causes the performance issue. But I already know exactly what causes the issue. I just want to know whether there is a better way to do things to avoid the problem.
There may be a better way.
I don’t know if changing the shape of your data would be that better way.
If you prefer I could say “YES” or “NO” and then I would be lying unless I lucked out and was correct.
Remember: you have told us effectively ZERO about this. Are you moving this data in and out every frame? Is it changing every game? Is it static? Could it be preloaded and baked into code so you never read it, it just IPLs with the rest of the assembly? etc.
Only you know.
There are several 2d array. All of them need to read every frame. None of them would change its size.
One of them is completely static.
One of them needs to change part of its data, but only when each scene loads.
One of them might change every frame.
Well it goes without saying large amounts of serialised data are going to take longer to de-serialise.
How much longer depends on how it’s being de-serialised.
As you’re using a 2d array one can presume you’re using the ISerializationCallbackReciever to convert the serialised flattened data to a 2d array for inspector/runtime use. That’s probably adding extra time on top of that, and probably creating a bit of a bottleneck.
But as Kurt says you need to try different methods by using the profiler.
It’s not just about finding the problem but also about checking if other solutions are better. We can’t say for certain if flattening your data will provide a significant improvement.
Only you can find out by experimenting, testing, and comparing the results.
That said Unity’s basic serialisation is, well, basic for a reason. It’s fast, which is what games need.
As an alternative you can try alternate serialised like the open source Odin Serialiser.