Resource efficient way of storing a large amount of prefabs to dynamically instantiate at run-time?

First and foremost,
thank you very much for taking the time to help me out and sorry if this question feels redundant.

After reading through several threads dealing with this issue, I am still left with some questions unanswered which hinders my decision on what method to adopt.

My problem :

Our game is an online third person action RPG that runs with an authoritative server setup. We have a building mechanic that allows players to construct a large number of different structures at run-time. Each structure goes through 3 phases.

First phase : The placement phase which indicates if the structure is build-able or not at the player selected emplacement.
Second Phase : The construction phase which demonstrates the progress towards completion.
Third Phase : The final structure, ready to use.

Since each phase use different prefabs, this means that each structure requires a total of 9 prefabs. (3 for server, 3 for owner and 3 for proxy - using uLink)

As of now, I have been storing every prefab in public GameObject references on a single script on the server. Obviously this approach is going to require a decent amount of resources and I feel like there are better suited methods of solving the problem.

Questions :

When using the resources folder to load and unload prefabs, does unloading delete already instantiated prefabs or only the reference that has been loaded?

When using asset bundles, does unloading delete already instantiated prefabs or only the reference that has been loaded?

Is there any other way to dynamically instantiate large amounts of prefabs at run-time?

Any input on the matter is appreciated :slight_smile:

Thanks again!

Cordially,
David

Here is what I found out doing some profiling.

The most important finding is that I am dumb for profiling in the editor, please don’t be like me, do profiling in a standalone.
I have read a few posts with people saying that any prefab put in the resources folder is automatically loaded into memory. That is incorrect. If profiling in the editor, any prefab in any folder will be loaded into memory. If profiling in a standalone, the same prefabs will not be loaded into memory unless referenced or loaded manually.

Calling Resources.Load () to load the same asset several times does not create extra memory allocation.

Calling Resources.UnloadUnusedAssets () will not delete instances of loaded assets nor will it free the memory allocation if said instances are present. It will free the memory allocation if a loaded asset is deemed unused. Here is a quote from the API : “An asset is deemed to be unused if it isn’t reached after walking the whole game object hierarchy, including script components. Static variables are also examined.” That might seem obvious to most, but I really thought that I could Load-Instantiate-Unload and keep my instance.

In my particular case, I think that I will be using the resources method instead of public references, because it will free up lots of memory in the earlier stages of the game. The game world is persistent and so are player built structures, so eventually the same memory allocation will be reached on the server.

I hope this helps someone :slight_smile:

David

1 Like