- Home /
Loading Resources folder contents in StandAlone Player
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[i] = tempList[i];
createButton(weapons[i]);
}
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!
Answer by lassade · Jan 15, 2016 at 01:51 AM
To use the assets in resources folder you must use this class http://docs.unity3d.com/ScriptReference/Resources.html 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.
Thankyou for the response.
The trouble is that none of the Resources class function seem to be what I need. Resources.Load requires a filename to load, and as I'm trying to dynamically find the filenames it's of no use to me.
Resources.LoadAll may be of use if the resources keep their folder names, but I'm not sure I understand exactly what "Loading" a Resource does. Does a Loaded object apear in the scene or have it's scripts run? Or is it's data simply read? All I need to do is grab it's name so the player can choose which to instantiate.
I've finally managed it with Resources.LoadAll, as suggested!
Here's the code for anyone else who may need it:
foreach(GameObject g in Resources.LoadAll("Weapons", typeof(GameObject)))
{
Debug.Log("prefab found: " + g.name);
//createButton(g.name);
}
Your answer
Follow this Question
Related Questions
How to make a File System? 1 Answer
How do you have a file in your build folder not be compressed by Unity? 1 Answer
Include a file to build - and keep it editable 0 Answers
feature request: detect resource filename collisions at compile-time 3 Answers
The method Resources.Load does not accept strings after splitting. 0 Answers