Hi.
Currently my game has a selection of weapons available which are all stored in the “Resources/Weapons” folder.
To add each weapon to a menu, I have a script that runs through the “Resources/Weapons” folder and instantiates a new menu item for each “.prefab” it finds.
This works perfectly in the Editor, but if I build the game as Windows Standalone it no longer works.
Here’s my code:
List<string> tempList = new List<string>();
string[] splitName;
//For each .Prefab in "Resources/Weapons/", add to list and create button
foreach (string file in Directory.GetFiles(Application.dataPath+"/resources/weapons/")){
transform.Find("DebugText").GetComponent<Text>().text = "Found " +file;
if(Path.GetExtension(file)==".prefab")
{
splitName = Path.GetFileName(file).Split("."[0]);
tempList.Add(splitName[0]);
}
}
weapons = new string[tempList.Count];
for(int i = 0; i < tempList.Count; i++)
{
weapons _= tempList*;*_
_ createButton(weapons*);
}*
I should point out that I want to use this method as it makes it easy to add new weapons to the game as we simply add the prefab to the Resources folder and it’s automatically added in-game.
Is there a way to accomplish this outside of the editor, or a better way to do it?
Thanks for any help!_
To use the assets in resources folder you must use this class Unity - Scripting API: Resources when the game compiles the resources folders scatter around your project won’t be in the Application.dataPath but in a other location, maybe inside a Unity pack file (don’t remember the extension).
As a suggestion use this same code only inside the editor to create a object (MonoBehaviour or ScriptabeObject) to hold the contents of the resource folder in the way that speed up the query.
Other way than that you have to use Resources.FindObjectsOfTypeAll i never used, so i am not sure, but i think this will also load everything in the memory.