Unity中常用的几种读取本地文件方式

使用的命名空间如下

using LitJson;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;

1、通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件

      /// 
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件
    /// 
    /// 文件名称
    /// 
    public string UnityWebRequestJsonString(string fileName)
    {
        string url;

        #region 分平台判断 StreamingAssets 路径
        //如果在编译器 或者 单机中  ……
#if UNITY_EDITOR || UNITY_STANDALONE

        url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
        //否则如果在Iphone下……
#elif UNITY_IPHONE

            url = "file://" + Application.dataPath + "/Raw/"+ fileName;
            //否则如果在android下……
#elif UNITY_ANDROID
            url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
        #endregion
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();//读取数据
        while (true)
        {
            if (request.downloadHandler.isDone)//是否读取完数据
            {
                return request.downloadHandler.text;
            }
        }
    }
```
View Code

2、通过UnityWebRequest和StreamReader读取本地StreamingAssets文件夹中的Json文件

      /// 
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件
    /// 
    /// 文件名称
    /// 
    public string UnityWebRequestJsonString(string fileName)
    {
        string url;

        #region 分平台判断 StreamingAssets 路径
        //如果在编译器 或者 单机中  ……
#if UNITY_EDITOR || UNITY_STANDALONE

        url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
        //否则如果在Iphone下……
#elif UNITY_IPHONE

            url = "file://" + Application.dataPath + "/Raw/"+ fileName;
            //否则如果在android下……
#elif UNITY_ANDROID
            url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
        #endregion
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();//读取数据
        while (true)
        {
            if (request.downloadHandler.isDone)//是否读取完数据
            {
                return request.downloadHandler.text;
            }
        }
    }
```
View Code

 3、通过StreamReader读取本地StreamingAssets文件夹中的Json文件 

      /// 
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件
    /// 
    /// 文件名称
    /// 
    public string UnityWebRequestJsonString(string fileName)
    {
        string url;

        #region 分平台判断 StreamingAssets 路径
        //如果在编译器 或者 单机中  ……
#if UNITY_EDITOR || UNITY_STANDALONE

        url = "file://" + Application.dataPath + "/StreamingAssets/" + fileName;
        //否则如果在Iphone下……
#elif UNITY_IPHONE

            url = "file://" + Application.dataPath + "/Raw/"+ fileName;
            //否则如果在android下……
#elif UNITY_ANDROID
            url = "jar:file://" + Application.dataPath + "!/assets/"+ fileName;
#endif
        #endregion
        UnityWebRequest request = UnityWebRequest.Get(url);
        request.SendWebRequest();//读取数据
        while (true)
        {
            if (request.downloadHandler.isDone)//是否读取完数据
            {
                return request.downloadHandler.text;
            }
        }
    }
```
View Code

 4、通过FileStream读取本地StreamingAssets文件夹中的文件

    /// 
    /// 通过FileStream读取本地StreamingAssets文件夹中的文件
    /// 
    /// 
    /// 
    public string GetAllFileInfos(string jsonName)
    {
        string jsonPath = Application.streamingAssetsPath + "/" + jsonName;
        try
        {
            using (FileStream fs = new FileStream(jsonPath, FileMode.Open, FileAccess.Read))
            {
                fs.Seek(0, SeekOrigin.Begin);
                var bytes = new byte[fs.Length];
                fs.Read(bytes, 0, (int)fs.Length);
                string jsonData = Encoding.UTF8.GetString(bytes);
                fs.Flush();
                fs.Dispose();
                fs.Close();
                return jsonData;
            }
            //Debug.Log("所有文件资源信息Assets下文件夹数量:" + fileInfos.Count);
        }
        catch (Exception e)
        {
            Debug.LogError(e.Message);
            Debug.LogError("文件读取异常:" + jsonPath);
            return string.Empty;
        }
    }
View Code

参考资源:https://www.cnblogs.com/unity3ds/p/11742487.html](https://www.cnblogs.com/unity3ds/p/11742487.html

转载链接:Unity 读取Json常用的几种方式_unity读取json_科幻之眼的博客-CSDN博客

你可能感兴趣的:(java,python,unity,linux,ajax)