Copy files from Resources/StreamingAssets to Application.persistentDataPath upon installation

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);

Leave a Comment