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

使用的命名空间如下

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

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

      /// <summary>
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件
    /// </summary>
    /// <param name="fileName">文件名称</param>
    /// <returns></returns>
    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文件

 1      /// <summary>
 2     /// 通过UnityWebRequest和StreamReader读取本地StreamingAssets文件夹中的Json文件
 3     /// </summary>
 4     /// <param name="jsonName">json文件名</param>
 5     /// <returns></returns>
 6     public string StreamReaderJsonFromSSPath(string jsonName)
 7     {
 8         string url;
 9         #region 分平台判断 StreamingAssets 路径
10         //如果在编译器 或者 单机中  ……
11 #if UNITY_EDITOR || UNITY_STANDALONE
12         url = Application.dataPath + "/StreamingAssets/" + jsonName;
13         //否则如果在Iphone下……
14 #elif UNITY_IPHONE
15 
16             url = Application.dataPath + "/Raw/"+ fileName;
17             //否则如果在android下……
18 #elif UNITY_ANDROID
19             url =  Application.dataPath + "!/assets/"+ fileName;
20 #endif
21         #endregion
22         UnityWebRequest webRequest = UnityWebRequest.Get(url);
23         webRequest.SendWebRequest();//读取数据
24         while (true)
25         {
26             if (webRequest.isNetworkError)
27             {
28                 Debug.LogError(url + "请求错误:" + webRequest.error);
29             }
30             else
31             {
32                 if (webRequest.downloadHandler.isDone)//是否读取完数据
33                 {
34                     //读取
35                     Encoding endoning = Encoding.GetEncoding("utf-8");//识别Json数据内容中文字段
36                     StreamReader streamReader = new StreamReader(url, endoning);
37                     string str = streamReader.ReadToEnd();
38                     streamReader.Close();
39                     return str;
40                 }
41             }
42         }
43     }
View Code

 

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

      /// <summary>
    /// 通过UnityWebRequest获取本地StreamingAssets文件夹中的Json文件
    /// </summary>
    /// <param name="fileName">文件名称</param>
    /// <returns></returns>
    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文件夹中的文件

    /// <summary>
    /// 通过FileStream读取本地StreamingAssets文件夹中的文件
    /// </summary>
    /// <param name="jsonName"></param>
    /// <returns></returns>
    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

转载链接:https://blog.csdn.net/U3DCoder/article/details/115464140

posted @ 2021-05-20 14:25  U3DEngineer  阅读(5672)  评论(0编辑  收藏  举报