Copy Files from Resources/Streamingassets to Application.Persistentdatapath Upon Installation

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

How do i copy a Json file from the Assets/Resources/ to the Internal storage of an Android device ( persistent data)? [Unity Android]

To copy a file you only need to have the information of the file and then create a new one in another place.

So if you got your json file into your Resources directory you can retreive like the documentation say:

//Load text from a JSON file (Assets/Resources/Text/jsonFile01.json)
var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object

Then you only have to create your android file in your persistentDataPath as @Iggy says in the comments doing:

System.IO.File.WriteAllText(Path.Combine(Application.persistentDataPath, "file.txt"), jsonTextFile);

Note: Not really sure, but for other cases I think that StreamingAssets folder should be inside Assets, no inside Resources.

How to access files in your assets folder from a deployed HoloLens app?

You'll want to create a folder named Resources inside your Assets folder, and place your files in there. You then load your file using something like:

TextAsset asset = Resources.Load("myfile.bytes"); 

You may want to change the file extension into one that Unity recognizes, for instance '.bytes' or '.txt', to avoid serialization issues. For more info on loading resources, see https://docs.unity3d.com/560/Documentation/Manual/LoadingResourcesatRuntime.html

For your last question, you can access files from the Windows Device Portal, but they should then be placed in User Folders \ LocalAppData \ your-app \ LocalState \

They can then be retrieved by loading the content as a file stream:

filename = "yourfile.bytes";
string streamstring = null;
if (File.Exists(Application.persistentDataPath + "/" + filename))
{
FileStream fs = new FileStream(Application.persistentDataPath + "/" + filename, FileMode.Open);
byte [] bytes = new byte [fs.Length];
fs.Read(bytes, 0, (int) fs.Length);
streamstring = Encoding.UTF8.GetString(bytes);
fs.Dispose();
}

Include custom file on android build

Assuming you are including your midi files in the StreamingAssets folder:

On Android, files in StreamingAssets are compressed. You need to use the UnityWebRequest class to get them out.

From the manual:

It is not possible to access the StreamingAssets folder on WebGL and
Android platforms. No file access is available on WebGL. Android uses
a compressed .apk file. These platforms return a URL. Use the
UnityWebRequest class to access the Assets.

If your midi library can't read them like that, load them via UnityWebRequest and save them to another path (like Application.PersistentDataPath) so you can give a real file path to the library for loading.

Iterating and reading text files from folder on Android

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.

Read from and write to text file from iOS device

Never use simple string concatenation for system paths!

And note that especially

Application.dataPath + "Resources/TextFiles/textfile.txt"

without any separator makes no sense as this would be a file outside of the

Application.persistentDataPath

folder since it results in something like

/rootfolder/applicationfolderResources/TextFiles/textfile.txt

You need either a \ or a / between them like e.g.

/rootfolder/applicationfolder/Resources/TextFiles/textfile.txt

So rather use Path.Combine which uses the correct path separators according to the running OS.

Path.Combine(Application.persistentDataPath, "Resources", "TextFiles", "textfile.txt")

Then further a special note regarding Resources:

From Unity's own Best Practices for theResources folder

Don't use it!

There are a lot of reasons for this you can all find in the link but most importantly for your case: Both the dataPath and the Resources folder are read-only after a build!

So yes you can use it for loading stuff if you really insist but only using Resources.Load, not directly via the file path, how you already figured out - but you won't be able to write to it anyway.

Rather use the Application.persistentDataPath instead like e.g.

Path.Combine(Application.persistentDataPath, "TextFiles", "textFile.txt")

to store this file on the device - visible for the user though so you might want to use some sort of encryption.


Then it is still possible/probable that the folder TextFiles simply actually doesn't exist yet. So create it. I would also go through File.Open which is a bit more secure (e.g. create the file if it doesn't exist etc)

private string folderPath => Path.Combine(Application.persistentDataPath, "TextFiles");
private string filePath => Path.Combine(folderPath, "textFile.txt");

public void SaveDataToTextFile()
{
if(!Directory.Exists(folderPath)
{
Directory.CreateDirectory(folderPath);
}

if(File.Exists(filePath))
{
File.Delete(filePath);
}

using(var fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))
{
using(war writer = new StreamWriter(fs))
{
writer.Write("Some Text");
}
}

ReadSavedData();
}

And

public void ReadSavedData()
{
if(!File.Exists(filePath)) return;

using(var fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
{
using(var reader = new StreamReader(fs))
{
var fileContent = savedData.ReadToEnd();
var lines = fileContent.Split(new char[] { '\n' });

foreach(var line in lines)
{
var savedDataString = line.Split(new char[] { '\n' });
txtField.text = string.Join("\n", savedDataString.Skip(1));
}
}
}
}


Related Topics



Leave a reply



Submit