这是我在android中读取我的文本文件的方式。
#if UNITY_ANDROID
string full_path = string.Format("{0}/{1}",Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
// Android only use WWW to read file
WWW reader = new WWW(full_path);
while (!reader.isDone){}
json = reader.text;
// PK Debug 2017.12.11
Debug.Log(json);
#endif
这就是我如何从pc读取我的文本文件。
#if UNITY_STANDALONE
string full_path = string.Format("{0}/{1}", Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);
StreamReader reader = new StreamReader(full_path);
json = reader.ReadToEnd().Trim();
reader.Close();
#endif
现在我的问题是,我不知道如何在移动设备上编写文件,因为我在独立设备上这样做
#if UNITY_STANDALONE
StreamWriter writer = new StreamWriter(path, false);
writer.WriteLine(json);
writer.Close();
#endif
这是我需要获取的在我的streamingasset文件夹中的json文件
发布于 2018-06-14 18:33:50
你无法保存到此位置。Application.streamingAssetsPath
是只读的。编辑器是否工作并不重要。它是只读的,不能用于加载数据。
从StreamingAssets读取数据:
IEnumerator loadStreamingAsset(string fileName)
{
string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);
string result;
if (filePath.Contains("://") || filePath.Contains(":///"))
{
WWW www = new WWW(filePath);
yield return www;
result = www.text;
}
else
{
result = System.IO.File.ReadAllText(filePath);
}
Debug.Log("Loaded file: " + result);
}
让我们从你的屏幕截图中加载你的“datacenter.json”文件:
void Start()
{
StartCoroutine(loadStreamingAsset("datacenter.json"));
}
保存数据:
保存可在所有平台上工作的数据的路径是Application.persistentDataPath
。确保在保存数据之前在该路径中创建一个文件夹。在StreamReader
你的问题中可以用来读取或写入该路径。
保存到Application.persistentDataPath
路径:
使用 File.WriteAllBytes
从Application.persistentDataPath
路径中读取
使用File.ReadAllBytes
。
发布于 2018-06-14 19:48:27
public void WriteDataToFile(string jsonString)
{
if (!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
if (!File.Exists(filePath))
{
File.Create(filePath).Close();
File.WriteAllText(filePath, jsonString);
}
else
{
File.WriteAllText(filePath, jsonString);
}
}
https://stackoverflow.com/questions/-100005392
复制相似问题