Save files to Android internal storage in Unity
code
using System.IO;
public static DirectoryInfo SafeCreateDirectory(string path)
{
//Generate if you don't check if the directory exists
if (Directory.Exists(path))
{
return null;
}
return Directory.CreateDirectory(path);
}
public void Score_Save(string Directory_path,string date)
{
//Data storage
SafeCreateDirectory(Application.persistentDataPath + "/" + Directory_path);
string json = JsonUtility.ToJson(date);
Writer = new StreamWriter(Application.persistentDataPath + "/" + Directory_path + "/date.json");
Writer.Write(json);
Writer.Flush();
Writer.Close();
}
public Score Score_Load(string Directory_path)
{
//Data acquisition
var reader = new StreamReader(Application.persistentDataPath + "/" + Directory_path + "/date.json");
string json = reader.ReadToEnd();
reader.Close();
return json;//Convert for ease of use
}
Introduction
In the first place, Android has shared storage that can be shared with other applications and internal storage that can be used only by that application.
|type|Contents|
|:-:|:-:|
|App-specific storage|Save the app-specific files to a dedicated directory in the internal storage volume or another dedicated directory in the external storage. Use directories in internal storage to store sensitive information that other apps cannot access.|
|Shared storage|Save files that your app shares with other apps, such as media and documents.|
|Setting|Key private primitive data-Save as a Value pair.|
|Database|Use the Room persistent library to store structured data in a private database.|
Overview of data storage and file storage
It seems that if you can access getFilesDir () or getCacheDir ()
on Unity, you can save it.
With Unity
It seems that you can get the path of application-specific storage just by using Application.persistentDataPath.
By the way, this can also be used for internal storage of ios and windwos
By the way, Application can be used for application processing, so if you are interested, you can read it.
Application.persistentDataPath
After all, what is ʻApplication.persistentDataPath`?
A. A function that returns the path of internal storage
using System.IO;
StreamWriter Writer = new StreamWriter(Application.persistentDataPath + "Arbitrary file path");
Writer.Write("Arbitrary data");
Writer.Flush();
Writer.Close();
Can be saved with
using System.IO;
var reader = new StreamReader(Application.persistentDataPath + ""Arbitrary file path");
string string = reader.ReadToEnd();
Read here