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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[C#] Acces Resources folder from build?

Discussion in 'Scripting' started by abelreyes, Nov 8, 2015.

  1. abelreyes

    abelreyes

    Joined:
    Sep 11, 2013
    Posts:
    28
    Hello there,
    I'm building an extension for editor that creates some .asset files and I need to recover in runtime. When playing on editor everything works perfect, but when I build the game (PC) and play it, it debugs can't find path of files on "Output_log.txt".

    DirectoryNotFoundException: Directory 'D:\Game\Assets\Resources\Extension\Folder' not found. (That's not exactly the path, I've prefere not to show much information).

    Anyone knows how to access Resources/Extension folder from build? (Generic for all platforms if possible) Or I must copy that folder to the build path when compiling? And if that is the case, how I can do that?

    Thank you everyone,
    Abel!
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    You can only access assets within the resources folder using the Resources api.
    Assets stored in the Resources folder is compiled into a Resources.assets file and can't really be accessed from outside. If you need external files copied with the executable look into StreamingAssets. Note that placing .asset files there will not make much sense as they wont be asset files unity will load natively, you will need to do that yourself.
     
    Kawecki and abelreyes like this.
  3. abelreyes

    abelreyes

    Joined:
    Sep 11, 2013
    Posts:
    28
    So... If I need to recover an know name file from Resources folder I should do it with Resources.Load(), but how can I get all files from that resources folder and Load them with Resources.Load() from build?

    My actual method works, but only on editor:


    Code (CSharp):
    1.         DirectoryInfo dirInfo = new DirectoryInfo ("Assets/Resources/Main/Misc/");
    2.         FileInfo[] fileInfo = dirInfo.GetFiles();
    3.  
    4.         Debug.Log ("Dir info: " + dirInfo);
    5.        
    6.         foreach (FileInfo file in fileInfo) {
    7.             if(file.Extension == ".asset"){
    8.                 Key newKey = (Key)Resources.Load<Key>("Main/Misc/" + file.Name.Substring(0, file.Name.Length - file.Extension.Length));
    9.                 //Do something with key
    10.             }
    11.         }
    12.  
    Thank you!
     
  4. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Use that method to write to a Text File in the Editor containing all the paths that gets shipped with your build. At runtime, read the file instead and load assets using the paths therein.
     
    abelreyes likes this.