We are using Scriptable Objects as our game database. We let the Game Designer combine them to configure the game. In a case we need a scriptable object to have a reference to a prefab, i.e. a mission asset that contains a list of enemy prefabs to instantiate. Later, we have a World Asset that contains a list of missions, so we can drag all the missions to this world asset. Finally, we have a mission selection screen that loads dynamically simply iterating over the mission array of the world asset and creating a button per each mission.
I suspect that this approach have a serious problem, and i need someone to confirm if my concern is true. If we have an asset loaded in a scene (World), that have references to several assets (Mission), and each of them have references to prefabs, that means that the prefabs must be loaded in memory? If thats true i need to change this, because this behaviour would force the Mission Selection menu to load all the possible enemy assets in vain.
The solution im trying to implement is just storing the prefab name and search for it in the Resources folder just in the time is needed, but that force me to make some refactoring, and because im not sure the exact behaviour of unity in this case i prefer ask here.
Old post but there are 79 people following this Q so i’ll answer it.
I can confirm it will indeed load all referenced objects into memory.
The solution (or at least my solution), as you rightly suggested, would be to change the ScriptableObect prefab variable types from GameObject to strings, have a ‘dummy’ GameObject in the inspector script for your ScriptableObject and once it is not null (i.e. the designer has drag/dropped the prefab, save the path of the prefab. Then, at a later time when the designer clicks back on the ScriptableObject, it checks to see if the string path is !string.IsNullOrEmpty(yourPrefabPathString), then loads that prefab into the drag/drop slot. So visually it looks like you have a prefab object referenced while internally it’s loading it on the fly and not saving it when you click off the ScriptableObject.
During runtime, to load the prefab from the path don’t forget to clean up the path string by removing everything up to the end of ‘Resources/’ and the extension of the file with:
path = path.Remove (0, path.IndexOf (“Resources”)+10);
//+10 bc resources = 9 chars + 1 for ‘/’
path = path.Remove (path.IndexOf (“.”));
//Removes the extension
We had a dictionary of ids vs prefabs in a WorldTiles prefab. Once the worldtiles was loaded, all of the referenced prefabs were loaded in memory aswell.
In the end we had to change the values to strings pointing the paths in the resources folder, as stated.
I didn’t know if with the scriptableobjects happened the same, but i suspected it. In my next project i was going to try the scriptableobject approach, but you saved me a lot of time trying it out. thanks!
Anything in the Resources will be loaded into the memory when the app runs. I don’t understand how you gain any benefit by putting your prefabs there and referencing them by a string.