Now that I exhausted all possible solutions on this, I have to ask this very simple question. Why is NONE of these methods giving me the file which lies in my Assets/Resources folder?
Resources.Load("lang.xml");
string s = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "Resources/lang.xml");
s = Application.dataPath + "/Resources/lang.xml";
s = Path.Combine(Application.persistentDataPath, "Resources/lang.xml");
FileInfo fi = new FileInfo(Application.dataPath + "Resources/lang.xml");
fi = new FileInfo(Application.persistentDataPath + "Resources/lang.xml");
fi = new FileInfo("Resources/lang.xml");
NONE of these are working!! Seriously, what is this? File.Exists() always returns false on Android.
I also tried putting the files directly under the Assets folder and although my main scene is in there (and it is found on Android), the lang.xml file is not. I give up, please help
The first call probably fails because you don’t specify the correct type of resource, it probably is a TextAsset, see examples Unity - Scripting API: Resources.Load
The second one fails because Assets directory is current in Editor, but not in player. Other ones just look at random locations.
Additionally, on Android your application is an .apk file which is a zip archive. System.IO APIs only work with actual files on the file system, not with stuff inside zip archives.
I wonder if I am not explicit enough - how do I refer to that file (whatever type it may be) under Android?
Whatever path I try, the result is always that the file can not be found.
The problem is not during Edit mode
You refer to them by jar:file URI, but you won’t be able to access it using System.IO APIs. You can read those files using UnityWebRequest, then you can save them to Application.persistentDataPath (or temp cache path), this is a file system path and can be accessed via file APIs.
Also in this thread, I don’t see clear answer from you either, @Aurimas-Cernius
Is it too much to give a simple, working example of how to access any type of file (e.g. text file) that is located under Assets/folderX on Android?
If I use the code of @Ukounu it doesn’t work either. I even tried with a “lang” and a “lang.xml” file in my Resources folder.
This is crazy, I think the Unity developers screwed sth up here. It should be super simple to access a file which is under the Assets folder and I can’t believe it takes more than 5min to solve this.
You cannot access files under Assets directory in built player. The directory only exists in Editor, in built players all things from it are something else. For example files from Resources folder are stored into data files and can be accessed via Resources class or you can have public variable of type TextAsset in your script and put the resource into it in your scene.
Well, it’s not like I didn’t try to use Resources.Load<TextAsset>("lang");
Log entry from Android: DirectoryNotFoundException: Could not find a part of the path "/storage/emulated/0/Android/data/com.abc.project/files/Resources/lang".
Plus, why can’t I access a file which I put into some folder?
Moreover, you’re just saying what is not possible but not how to fix this. Or maybe I missed sth?
The error looks to come from System.IO API, not from Resources.Load.
And like I said, file layout in Unity editor and in player is not the same. If you want file to appear in player, you should put it in StreamingAssets directory. Yet, on Android streaming assets are insize a zip file, so you can access them using UnityWebRequest but not System.IO.
To access file in StreamingAssets:
Uri uri = new Uri(Path.Combine(Application.streamingAssetsPath, "lang.xml"));
OK, after producing the cross product of all possibilites [file locations x endings x different paths] I found the following:
The file inside the Resources folder MUST HAVE an xml ending, but when referring to it, you have to OMIT it. Great job, makes perfect sense. I want to add that ANY other possibility does not work!
So the proper way of addressing a file inside your Resources folder under Android is to put the file under Assets/Resources/lang.xml (with ending!)
and access it by
var xml_data = Resources.Load<TextAsset>("lang"); // no ending!
xml.LoadXml(xml_data.text);