- Home /
How to keep assets in memory without duplication?
I am working on a project which contains four scenes the user moves from the first scene to one of the three others and back again, this is probably explained best with a picture:
Scene 1 and 2 contain a large shared asset (Several Meshes, 44MB at the moment but totally unoptimized) Scene 3 and 4 do not contain any shared assets. When exported to the web player moving between Scene 1 and Scene 2 is rapid there is a slight perceptible pause but not enough to even be able to read the word "Loading" on my loading screen.
However when moving from Scene 4 to Scene 1 or Scene 3 to Scene 1 (Red Arrows) there is a long pause. My first thought was to put the asset in Scene 3 and 4 which works but feels dirty. I also create a script which I could attach to the asset using DontDestroyOnLoad(this) this removed the delay but caused the asset to be duplicated on each subsequent load.
I have seen several scripts which apply DontDestroyOnLoad then Destroy(GameObject) the asset manually. This also feels quite dirty.
Is there a better way of keeping a large asset in memory and prevent an object from being instantiated multiple times.
Answer by Herman-Tulleken · Oct 25, 2010 at 07:10 AM
Here is one way to solve the persistence / duplication problem:
Make you shared object into a prefab (or multiple prefabs)
Make an empty game object ObjectSpawn, and put it in each scene.
Create a script that first check whether the object exists, and if not, instantiates it. Call 'DontDestroyOnLoad' on the object. Attach the script to ObjectSpawn, and remove the shared objects from the scene.
I don't know whether this is cleaner than any other method, but it's worth considering (we usually use for our "logic" objects, not meshes).