x


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:

alt text

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.

more ▼

asked Oct 24 '10 at 08:07 PM

Richard Slater gravatar image

Richard Slater
13 2 2 6

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Here is one way to solve the persistence / duplication problem:

  1. Make you shared object into a prefab (or multiple prefabs)

  2. Make an empty game object ObjectSpawn, and put it in each scene.

  3. 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).

more ▼

answered Oct 25 '10 at 07:10 AM

Herman Tulleken gravatar image

Herman Tulleken
1.6k 25 36 56

Thanks, I shall give this a go.

Oct 25 '10 at 12:51 PM Richard Slater
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x344
x312
x84
x63

asked: Oct 24 '10 at 08:07 PM

Seen: 1685 times

Last Updated: Oct 24 '10 at 08:07 PM