I have txt
file which contains data for my maps in game. Problem is that file is stored in Application.persistentDataPath
so I can change it even from my android device (created map creator) so how can I include txt file which I created on my PC with basic maps and make it appear in persistentDataPath
on my android device when I install application?
You can put the file in the Resources folder from the Editor folder then read with the Resources API.
TextAsset txtAsset = (TextAsset)Resources.Load("textfile", typeof(TextAsset));
string tileFile = txtAsset.text;
You can check if this is the first time the app is running with this. After that you can copy the loaded data to the Application.persistentDataPath
directory.
The Resources folder is known to increase loading times. I suggest you don't use it but it's an option that's worth knowing.
Put the file in StreamingAssets folder then read it with the WWW
or UnityWebRequest
API and Application.streamingAssetsPath
as the path then copy it to Application.persistentDataPath
.
Load from StreamingAssets:
IEnumerator ReadFromStreamingAssets()
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, "MyFile");
string result = "";
if (filePath.Contains("://") || filePath.Contains(":///"))
{
UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(filePath);
yield return www.SendWebRequest();
result = www.downloadHandler.text;
}
else
result = System.IO.File.ReadAllText(filePath);
}
then save it to persistentDataPath:
File.WriteAllText(Application.persistentDataPath + "data/MyFile.txt", result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With