使用的命名空间如下
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文件
/// <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、通过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