• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by HellsPlumber · Jan 14, 2016 at 11:57 PM · resourcesfilesystemfilename

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HellsPlumber · Jan 15, 2016 at 02:49 AM 0
Share

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.

avatar image HellsPlumber · Jan 15, 2016 at 03:29 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

33 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How can I include the resources folder to my project and build it to play on android devices? 1 Answer

Resourses.LoadAll doesn't return value 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges