使用的命名空间如下
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;
}
}
}
```
2、通过UnityWebRequest和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;
}
}
}
```
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;
}
}
}
```
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;
}
}
参考资源:https://www.cnblogs.com/unity3ds/p/11742487.html](https://www.cnblogs.com/unity3ds/p/11742487.html文章来源:https://www.toymoban.com/news/detail-761079.html
转载链接:Unity 读取Json常用的几种方式_unity读取json_科幻之眼的博客-CSDN博客文章来源地址https://www.toymoban.com/news/detail-761079.html
到了这里,关于Unity中常用的几种读取本地文件方式的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!