【问题标题】:Read and Write file on streamingAssetsPath在 streamingAssetsPath 上读写文件
【发布时间】:2018-05-28 00:37:17
【问题描述】:

这就是我在 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

这就是我从电脑读取文本文件的方式。

#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文件

【问题讨论】:

  • 无法真正验证,但您的独立代码应该可以在 android 上运行。你在手机上做的方式相当于从网络上读取文件,而你不能写入网络。
  • 那么在 WWW 类上没有实际的方法吗? @Draco18s
  • StreamWriter writer = new StreamWriter(path, false); writer.WriteLine(json); writer.Close(); 当我尝试在移动设备上使用它时,我的 catch(Exception e) 说 System.IO.DirectoryNotFoundException: Could not find a part of the path "/jar:file:/data/app/com.steet383.rh.google-1/base.apk!/assets/notice.json".
  • 我的错误。我远离开发环境,无法检查。 WWW不太可能让你写,但我不知道你的替代品是什么。
  • hmmm 我什么都没有 :( 。我已经尝试了几个小时,我已经看到很多他们正在使用持久数据,但问题是我需要从aws 服务器并将其取回。这就是我的问题。

标签: c# android json unity3d web


【解决方案1】:

现在我的问题是我不知道如何在移动设备上写入文件 因为我在独立时这样做

您无法保存到此位置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

有关如何在 Unity 中保存数据的完整示例,请参阅 this 帖子。

【讨论】:

  • 对不起,我的错。我的眼睛现在变得模糊了,对不起。那么先生,我仍然可以通过这样做来调用我的 s3 服务器吗?因为在我的独立平台上,我像这样https://********.amazonaws.com/pc_version/ph/check/serverList.json 调用我的数据中心,它将下载 serverlist.json
  • 您明白,只需将您的full_path 值替换为Application.persistentDataPath,您问题中的读写代码就可以使用StreamWriter 吗?这就是程序员试图告诉你的。删除UNITY_ANDROIDUNITY_STANDALONE。你不需要那些。只需将StreamWriter 与此答案中提供的路径一起使用,您的问题就会得到解决。 StreamWriter 也适用于 Android。
  • @PassetCronUs 但问题是文件在我的流资产路径中?会受到影响吗?
  • 先生之类的。在我的 StreamingAsset 文件夹中,我有我的 json 文件
  • 顺便说一句,这个目录在 Android 上似乎是只读的,因为它被打包到一个 jar 中。但是,它可以在其他平台上写入。文档中的“只读”是指变量,而不是目录(因为您无法更改路径)。
【解决方案2】:

这是我在没有WWW 类的情况下这样做的方式(适用于 Android 和 iOS),希望它有用

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

}

【讨论】:

  • 它将与 WWW 类先生一起使用吗,因为我有一个 json 文件,其中包含我的数据中心 dataCenter:{ https://********.amazonaws.com } 并且我在我的独立平台上这样称呼它 serverListJsonURL = string.Format("{0}/pc_version/ph/check/serverList.json", DataCenter_BaseURL );
  • 是的,它确实适用于使用 WWW 类下载的 JSON 字符串
  • @NoobProgrammer 请记住,它将保存应用程序安装在设备上的文件(对于 Android,它在根数据文件夹中,对于 Windows,它在 AppData 文件夹中)
  • 您好,先生。我有点难以将它转换成这样的StreamWriter writer = new StreamWriter(path, false); writer.WriteLine(json); writer.Close();你能帮我吗
  • 究竟是什么问题,我没看懂问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多