For some time, my game has taken about 10 seconds to load, and it has been left as a blank screen while that was happening. I’ve long wanted to do something about that, and this week, I was able to start tackling the problem.
There were a few different ways that I thought to try to make this happen. The first that I tried was using the Asynchronous loading for scene management. This works great if you have a large number of objects that needed to be loaded, that each object doesn’t take a long time to load. That didn’t mix well with my game’s system, where the loading time essentially happens with one slow object being created.
The next system I tried was using a coroutine. This didn’t work, as the UI thread was still locked. There was a somewhat related option left, although it proved to be quite difficult in the end. The solution was to thread the world creation process.
I should note that the entire process is a single thread. This made a few things easier. The largest difficulty was that Unity’s random number generator only supports the creation of random numbers on the UI thread. I managed to work around this by using the System.Random engine, but it took quite a bit of work to get it all to work properly.
Having this system in place is somewhat meaningless if I’m not giving the player an idea of what the system is actually doing. In order to make that happen, I added debug code to my logs to start to test the system before I start to work on the UI. While so doing, I effectively profiled the world creation process, and I noticed that one particular part was taking 90% of the time. I was able to speed up this process considerably, and now the time takes only 3 seconds, at least on my computer.
This week I’m planning on working on the UI part of the loading system. I am hoping to also get a better game over screen in place, which I was also able to start working on in this week.