使用的命名空间如下
using LitJson;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEngine;
using UnityEngine.Networking;
注意:安卓路径
本地路径:“/storage/emulated/0/” + fileName;
安装后:Application.persistentDataPath
注意事项:
安卓端运行apk提示”UnauthorizedAccessException:Access to the path “/xx/xx.xx” is denied.“
解决方法 ①在AndroidManifest.xml中的application标签中添加:android:requestLegacyExternalStorage=“true”
②安装在安卓手机上apk软件,需要设置软件权限管理,将读写设备存储改成始终允许
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

这篇博客介绍了在Unity引擎中通过UnityWebRequest和StreamReader等不同方式读取本地StreamingAssets文件夹内的Json文件,包括解决安卓端权限问题的方法,以及在不同平台下的路径调整。
最低0.47元/天 解锁文章
8989

被折叠的 条评论
为什么被折叠?



