0

I made a word game app for android in Unity, where the player has to find a word from a category previously loaded to the game.

The way I load the categories is: There is a folder named Categories, inside Assets, I run through the folder and read each text file as a category.

The categories are stored in a dictionary where the key is name the of the file and the value is every line of the file as an array element.

It worked well on the PC however no luck on android. Tried changing the path to "public string categoriesDirectoryPath = Application.persistentDataPath +"Categories";" still does not work.

Original path was "Assets/Categories"

Code for initiating the dictionary with the file values is (Happens on GameManager's Awake()):

 private Dictionary<string, string[]> createCategories(string directoryPath)
    {
        Dictionary<string, string[]> categories = new Dictionary<string, string[]>();
        string[] categoryPaths = Directory.GetFiles(directoryPath);
        foreach (string path in categoryPaths)
        {
            if (!path.EndsWith("meta")) {
                Debug.Log(path);
                string categoryName = Path.GetFileNameWithoutExtension(path);
                Debug.Log(categoryName);
                string[] categoryData = File.ReadAllLines(path).ToArray();
                categories.Add(categoryName, categoryData);
            }
        }
        return categories;
    }

Is there a way of iterating the folder and reading the text files that were in Assets/Categories after building the APK?

Dor Moshe
  • 13
  • 5

1 Answers1

3

Is there a way of iterating the folder and reading the text files that were in Assets/Categories after building the APK?

No.

If you want to access from the project, in a build, you have two options:

1.Put the file a folder named "Resources" then use the Resources to read the file and copy it to the Application.persistentDataPath path. By copying it to Application.persistentDataPath, you'll be able to modify it. Anything in the "Resources" is read only.

2.Put the file in the StreamingAssets folder then use UnityWebRequest, WWW or the System.IO.File API to read it. Atfer this, you can copy it to the Application.persistentDataPath.

Here is a post with code examples on how to do both of these.


3.AssetBundle(Recommended due to performance and loading reaons).

You can build the file as AssetBundle then put them in the StreamingAssets folder and use the AssetBundle API to read it.

Here is a complete example for building and reading AssetBundle data.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I'm using the same method. It's working. You should also use WWW to read files from StreamingAssets folder if you are on webgl, while ios and pc (mac and linux) can just use regular file copy to move a file from StreamingAssets folder to persistentDataPath – LiefLayer Aug 03 '18 at 15:40
  • Resources sounds like a good option however I do not wish to specify every category (file) as it must be hard coded. Is it possible to upload EVERY resource in the resources folder? – Dor Moshe Aug 03 '18 at 15:49
  • 1
    @RonTang That's true for the Resources folder on all platforms but true for StreamingAssets on some platforms. Notice how I mentioned `UnityWebRequest`, `WWW` and the `System.IO.File`. The `UnityWebRequest` and `WWW` are basically the-same now but `UnityWebRequest` is now recommended. `UnityWebRequest` should be used on Android and WebGL, `System.IO.File` should be on other platforms. – Programmer Aug 03 '18 at 15:52
  • @RonTang the link I provided in the answer shows that you can determine which one to use by checking the path for the presence of `://` or `:///`. – Programmer Aug 03 '18 at 15:54
  • @DorMoshe *"I do not wish to specify every category (file) as it must be hard coded"* then go with StreamingAssets which is also mentioned in the answer. If you have many resources data, I recommend that you use AssetBundle which is #3 in my answer. – Programmer Aug 03 '18 at 16:03