Object Pool*

Look at the code from the Abstract Factory task. Investigate the Room.Load() method. You can see that there is a MapTile.Load() method, which will be called on each of the tiles in the room. That method does Instantiate() on the prefabs associated with the corresponding tile. Similarly Destroy() is called when a room is unloaded.

Calling Instantiate() at run-time is a red flag in Unity development, especially if it is done in a loop on many objects at once. Currently our rooms are quite small, but even now, there is a noticeable delay, when moving between the rooms through the portals.

1) Measure the time it takes to load a room. For that use Time.realtimeSinceStartup or the System.Diagnostics.Stopwatch (with Elapsed.Ticks) inside the Room.Load() method. Output the loading time in milliseconds to the console. Write down an average time.

2) Optional. Make a binary branch inside the MapTile.Load() method. In one case use the current Instantiate() call and in the other case fetch objects from a pool. Do the same in the MapTitle.Unload() method. This is optional, because it will allow you to compare the loading times later on, but is only for your own testing purposes.

We need to implement object pooling here using several pools, because there are several different prefabs instantiated – ground, riverRight, riverForward, portal. The correct one depends on the specific map title. So:

3) Create a class, which holds different pools. Example name: Pools.

4) Create a class, which holds one pool. Example name: Pool.

5) Create a class, which is a decorator around the objects we want to pool (around GameObjects). Example name: PooledObject.
Find out what would be a good approach in Unity when you want a GameObject to exist, but not be visible or do anything (the dead state of our PooledObject).

6) Implement the "better way" implementation (described in the material) of object pooling inside the Pool class. Ie use one list and an index.

7) Substitute the Instantiate() and Destroy() calls in the MapTitle.Load() and MapTitle.Unload() methods such that it would use the created pools. If you also did 2) then write the pool usage calls in another branch next to them.

8) Measure the time loading the rooms when using object pooling. Write it down and compare it with the previous required time. It should be about 5 times faster.

In the submission write also the actual times you got in steps 1) and 8).

Here is a class diagram of a possible implementation (your classes may vary):

Although there are key steps mentioned here and even a class diagram, feel free to be creative about the solution. It is totally fine if you do things differently than described here. Main idea is for you to implement object pooling on the GameObjects in a way that you get about 5 times performance gain for loading different rooms.

;