Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Get an Asset's name in the Resources via Script

Discussion in 'Scripting' started by andres29091, Sep 28, 2014.

  1. andres29091

    andres29091

    Joined:
    Mar 1, 2014
    Posts:
    4
    Hello, I would like to get all the name of each asset from the Resources folder.

    The code that I'm using:

    Code (CSharp):
    1.   private List<string> assetNameList = new List<string>;
    2.  
    3.   //Get the path to all the .png files on the project
    4.   string[] pngFiles = System.IO.Directory.GetFiles(Application.dataPath, "*.png", SearchOption.AllDirectories);
    5.   string textureName;
    6.   foreach (string file in pngFiles)
    7.   {
    8.       //Check if the file is in the Textures folder inside the Resources
    9.       if (file.StartsWith(Application.dataPath + "\\_Game\\Resources\\Textures\\"))
    10.       {
    11.           //If it is, store only the name of the asset
    12.           textureName = file.Replace(Application.dataPath + "\\_Game\\Resources\\Textures\\", "").Replace(".png", "");
    13.           //Add it to the asset name list
    14.           assetNameList.Add(textureName);
    15.       }
    16.   }
    The problem is that when I build my game I get 2 error messages saying:

    *error CS0234: The type or namespace name 'Directory' does not exist in the namespace 'System.IO' (are you missing an assembly reference?)

    *error CS0103: The name 'SearchOption' does not exist in the current context*

    What should I do?
     
  2. daemon3000

    daemon3000

    Joined:
    Aug 13, 2012
    Posts:
    139
    What platform are you building for? Some platforms like the WebPlayer don't have access to the filesystem for security.
    If your target is one of those platforms consider having an XML file with the asset names in the Resources folder. You would load the XML file as a text asset then parse it's content to get the asset names. You could even create an editor window that gets the content of the Resources folder and builds the XML file automatically.
     
  3. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    That shouldn't have to be an issue for assets in a resources folder. You can't use the .net directory class though. And I don't think that's a good way of finding assets. Unity has it's own functions for that.

    Take a look at http://docs.unity3d.com/ScriptReference/Resources.FindObjectsOfTypeAll.html
     
  4. daemon3000

    daemon3000

    Joined:
    Aug 13, 2012
    Posts:
    139
    That's what I meant by "don't have access to the filesystem". I guess I wasn't clear enough.

    Also I don't think FindObjectsOfTypeAll will work in this case. The documentation mentions that it returns "any type of Unity object that is loaded", including internal assets, and it doesn't constrain the result to only the Resources folder or finds resources that are not loaded.
     
  5. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    You're right. I meant this one:
    http://docs.unity3d.com/ScriptReference/Resources.LoadAll.html

    For example, this will load all textures in resources/textures folders
    Code (csharp):
    1. Texture[] textures = Resources.LoadAll<Texture>("textures");