在streamingAssetsPath上读写文件 [英] Read and Write file on streamingAssetsPath

查看:51
本文介绍了在streamingAssetsPath上读写文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我在 android 中读取文本文件的方式.

#if UNITY_ANDROIDstring full_path = string.Format("{0}/{1}",Application.streamingAssetsPath, path_with_extention_under_streaming_assets_folder);//Android 只使用 WWW 来读取文件WWW reader = new WWW(full_path);while (!reader.isDone){}json = reader.text;//PK调试 2017.12.11调试日志(json);#万一

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

#if UNITY_STANDALONEstring 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();#万一

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

#if UNITY_STANDALONEStreamWriter writer = new StreamWriter(path, false);writer.WriteLine(json);writer.Close();#万一

帮助任何人

更新的问题

这是我需要获取的流资产文件夹中的 json 文件

解决方案

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

您无法保存到此位置.

从 StreamingAssets 中读取数据:

IEnumerator loadStreamingAsset(string fileName){string filePath = System.IO.Path.Combine(Application.streamingAssetsPath, fileName);字符串结果;if (filePath.Contains("://") || filePath.Contains(":///")){WWW www = new WWW(filePath);收益率返回 www;结果 = www.text;}别的{结果 = System.IO.File.ReadAllText(filePath);}Debug.Log("加载的文件:" + 结果);}

用法:

让我们从屏幕截图中加载datacenter.json"文件:

void Start(){StartCoroutine(loadStreamingAsset("datacenter.json"));}

<小时><小时>

保存数据:

保存一个适用于所有平台的数据的路径是Application.persistentDataPath.在将数据保存到该路径之前,请确保在该路径中创建一个文件夹.您问题中的 StreamReader 可用于读取或写入此路径.

保存到Application.persistentDataPath路径:

使用File.WriteAllBytes

Application.persistentDataPath 路径读取

使用File.ReadAllBytes.

参见这个 发布有关如何在 Unity 中保存数据的完整示例.

This is how i read my textfile in 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

and this is how i read my textfile from 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

Now my problem is that i don't know how to write the file on mobile cause i do it like this on the standalone

#if UNITY_STANDALONE
        StreamWriter writer = new StreamWriter(path, false);
        writer.WriteLine(json);
        writer.Close();
 #endif

Help anyone

UPDATED QUESTION

This is the json file that it is in my streamingasset folder that i need to get

解决方案

Now my problem is that i don't know how to write the file on mobile cause I do it like this on the standalone

You can't save to this location. Application.streamingAssetsPath is read-only. It doesn't matter if it works on the Editor or not. It is read only and cannot be used to load data.

Reading data from the 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);
}

Usage:

Let's load your "datacenter.json" file from your screenshot:

void Start()
{
    StartCoroutine(loadStreamingAsset("datacenter.json"));
}



Saving Data:

The path to save a data that works on all platform is Application.persistentDataPath. Make sure to create a folder inside that path before saving data to it. The StreamReader in your question can be used to read or write to this path.

Saving to the Application.persistentDataPath path:

Use File.WriteAllBytes

Reading from the Application.persistentDataPath path

Use File.ReadAllBytes.

See this post for a complete example of how to save data in Unity.

这篇关于在streamingAssetsPath上读写文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆