Several commonly used ways to read local files in Unity

The namespaces used are as follows

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

1. Get the Json file in the local StreamingAssets folder through UnityWebRequest

      /// <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. Read the Json file in the local StreamingAssets folder through UnityWebRequest and StreamReader

      /// <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

 3. Read the Json file in the local StreamingAssets folder through StreamReader 

      /// <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. Read files in the local StreamingAssets folder through FileStream

    /// <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

Reference resources:https://www.cnblogs.com/unity3ds/p/11742487.html](https://www.cnblogs.com/unity3ds/p/ 11742487.html

Reprint link: Several common ways to read Json in Unity_Read json in Unity_Science Fiction Eye's Blog-CSDN Blog

Guess you like

Origin blog.csdn.net/U3DCoder/article/details/124395973
Recommended