I am trying to create a GUI with a button for each prefab I have in my Resources folder, to resemble an inventory of items. I want it to be dynamic, such that if I add a new prefab to the folder it is automatically created in the inventory when the game is run.
I am not sure how to go about this and I don’t know if this is even possible. Any help or suggestions is appreciated.
Also, I need this to work for iOS and Android devices.
//load all prefabs from path 'Resources/Prefabs'
var prefabs : Object[] = Resources.LoadAll("Prefabs", GameObject);
function Start(){
for (prefab in prefabs){
Instantiate(prefab, Vector3(0, 0, 0), Quaternion.identity);
}
}
System.IO will not be available on iOS/Android/Webplayer I believe, and even on platforms with System.IO the resources folder doesn’t exist at runtime as an actual folder relative to the project folder.
What you could do, though, is create an asset post processor editor script that automatically maintained a script containing an array of folder paths that you could access at runtime. You could have a monobehavior script on a prefab, or you could maintain (automatically) a text asset and pull the information from there.
Basically, your editor script would use System.IO to get the directory paths and store them in an array somewhere accessible at runtime.