I am making a game which has around 25 prefabs. I want to generate these prefabs randomly one after another and also I am destroying the last prefab. One way is to make an array of gameobjects and then reference the prefabs to them. But this way a lot of memory is being used(around 190 MB) because of which my game crashes in android device. Another alternative is using the resources folder but I want to make asset bundle and I will not be able to make assetbundle of my resource folder. So is there any other way to Instantiate prefabs so that memory usage is also less and aset bundling is also possible.
2 Answers
The Resources folder is only needed instead of an asset bundle, not in addition to. If something is going in an asset bundle, then you load it from the bundle and it doesn't need to be in Resources.
You can load the asset bundle. Load a randomly selected object from that bundle and get a reference to it and then unload the asset bundle to conserve memory. This how ever means that you will have to load the asset bundle every time you have a new object. So a so called pooling system might be what you need. I will explain the steps.
1.Load the asset bundle
2.Load a chunk of the object in the bundle into a list(you will have to work out what number has good memory usage in your case)
3.Use that list to instantiate a randomly selected object. When you instantiate it you might want to keep a seperate list with something like object ID's to make sure we dont load/instantiate the same object twice.
4.Remove the object from the list when no longer needed.
5.When the list is empty repeat with step 1 and load another chunk of objects.
-
\$\begingroup\$ Asker notes that they are not able to use asset bundles, in the body of their question. \$\endgroup\$– GnemlockCommented Dec 6, 2016 at 22:28