Search Unity

Question Read directory in build version

Discussion in 'Scripting' started by Viroz_, Jan 24, 2023.

  1. Viroz_

    Viroz_

    Joined:
    Dec 30, 2022
    Posts:
    17
    Hi,

    I want to get all files from directory, but in production the folder does not appear.

    Code (CSharp):
    1. private static readonly string themePath = Application.dataPath + "/data/";
    2.  
    3.     public static void ReadFiles()
    4.     {
    5.         if (dataRead) return;
    6.         DirectoryInfo dir = new(themePath);
    7.         FileInfo[] info = dir.GetFiles("*.xml");
    8.         foreach (FileInfo f in info)
    9.         {
    10.             XmlDocument doc = new();
    11.             doc.Load(f.FullName);
    12.  
    13.             XmlElement root = doc.DocumentElement;
    14.             string name = root.SelectSingleNode("/theme-data/name").InnerText;
    15.             Color32 background = HexToColor(root.SelectSingleNode("/theme-data/background-color").InnerText);
    16.             Dictionary<Regex, Color32> highlight = new();
    17.             foreach (XmlNode node in root.SelectSingleNode("/theme-data/highlights").ChildNodes)
    18.             {
    19.                 Regex regex = new(node.ChildNodes[0].InnerText);
    20.                 Color32 color = HexToColor(node.ChildNodes[1].InnerText);
    21.  
    22.                 highlight.Add(regex, color);
    23.             }
    24.  
    25.             themes.Add(new Theme(name, highlight, background));
    26.             themeNames.Add(name);
    27.         }
    28.         dataRead = true;
    29.         if (PlayerPrefs.HasKey("editorTheme"))
    30.             currentTheme = GetThemeByName(PlayerPrefs.GetString("editorTheme"));
    31.         else
    32.             currentTheme = GetThemeByName("Default");
    33.     }
    What can I do to make that folder appear, or somehow fix the issue?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    You cannot use anything from the System.IO namespace to read assets from a build.

    Here are three alternatives:

    You may use Resources.Load<T>() or Resources.LoadAll<T>() (simplest)

    You may use Addressables or Asset Bundles (most-powerful)

    You may stream non-Unity assets ONLY out of the StreamingAssets mechanism.
     
  3. Viroz_

    Viroz_

    Joined:
    Dec 30, 2022
    Posts:
    17
    Thanks, that helps