Copy files from Resources/StreamingAssets to Application.persistentDataPath upon installation
Solution 1:
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);
Solution 2:
You can store this file in your Resources folder and copy it to Application.persistentDataPath
at your first app launch. Or read the file from Resources at first launch and create file with the same content in Application.persistentDataPath
.