当前位置: 首页 > news >正文

Unity安卓Android从StreamingAssets加载AssetBundle

在安卓下无法获取StreamingAssets目录下所有目录和文件名,所以需要提前将文件名整理成一个文件filelist.txt。

1.用批处理命令将StreamingAssets下所有文件名输出到filelist.txt中

chcp 65001是使用UTF-8编码,否则中文是乱码。

@echo off
chcp 65001
dir /b /s /a-d > filelist.txt

2.将filelist.txt中绝对路径转换成相对路径

打开filelist.txt,去掉StreamingAssets

3.确何filelist.txt为UTF-8

4.复制StreamingAssets目录下所有文件到PersistentData

void Start () {
    CopySteamingAssetsFileToPersistentDataPath();
}

void CopySteamingAssetsFileToPersistentDataPath()
{
    string listFile = Path.Combine(Application.streamingAssetsPath, "filelist.txt");
    WWW reader = new WWW(listFile);
    while (!reader.isDone) { }

    string textString = reader.text;
    Debug.Log(textString.Length);
    List<string> striparr = textString.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList();
    List<string> lines = striparr.Where(s => !string.IsNullOrEmpty(s)).ToList();

    for (int i = 0; i < lines.Count; i++)
    {
        string srcfile = Path.Combine(Application.streamingAssetsPath, lines[i]);
        WWW reader1 = new WWW(srcfile);
        while (!reader1.isDone) { }

        string dstFile = Path.Combine(Application.persistentDataPath, lines[i]);

        //安卓的路径只能是/,反斜杠\无效
        dstFile = dstFile.Replace('\\','/');
        string dir = Path.GetDirectoryName(dstFile);

        if (!Directory.Exists(dir))
        {
            Directory.CreateDirectory(dir);
            Debug.Log(dstFile);
        }
        File.WriteAllBytes(dstFile, reader1.bytes);
    }
}

5.AssetBundle资源需要针对Android单独打包,与Windows的不通用。

 打包完将资源复制到StreamingAssets下

ROBOCOPY Android  ..\Assets\StreamingAssets\Android  /E

 加载AssetBundle

//存放AssetBundle的根目录名字
    private static string rootFolder = "Android";
    //本地资源路径
    public string AbPath
    { get
        {
            string path =
#if UNITY_ANDROID && !UNITY_EDITOR
             Application.persistentDataPath + "/";
#else
             Application.streamingAssetsPath + "/";
#endif
            return string.Format("{0}{1}/", path, rootFolder);
        }
    }
AssetBundle assetBundle = AssetBundle.LoadFromFile(string.Format("{0}{1}", AbPath, rootFolder));
......

相关文章:

  • Redis的高可用
  • 深入解析K8s VolumeMounts中的subPath字段及其应用
  • 怎么使用数据集微调大模型LLM
  • DeepSeek技术名词全解析:一场属于中国AI的“觉醒时刻”
  • Manus演示案例: 英伟达财务估值建模 解锁投资洞察的深度剖析
  • Trae IDE:解锁 AI 驱动的高效编程体验
  • 网络安全之RSA算法
  • 鸿道Intewell工业操作系统通过100%自主可控测评
  • BurpSuite插件jsEncrypter使用教程
  • 如何下载MySQL和如何下载MySQL的JDBC驱动包
  • 73. 矩阵置零
  • SQL29 计算用户的平均次日留存率
  • (每日一题) 力扣 860 柠檬水找零
  • pyspark RDD相关常用函数使用案例
  • ubuntu20.04_vscode_snap安装方式
  • 执行NVIC_SystemReset程序不复位的解决办法
  • ca证书和服务端证书两者之间的关系
  • vue3 + xlsx 实现导入导出表格,导出动态获取表头和数据
  • 【AI】让deepseek_r1 671b输出draw.io可导入的xml图表数据
  • golang算法滑动窗口
  • 李公明 | 一周画记:德里达哲学还是接地气的
  • 中国旅游日|上天当个“显眼包”!体验低空经济的“飞”凡魅力
  • 中国田径巡回赛西安站完赛:男子跳远石雨豪夺冠
  • C919上海虹桥-深圳航线开通,东航今年计划再接收10架C919
  • 广西桂林、百色、河池等地表态:全力配合中央对蓝天立的审查调查
  • 专利申请全球领先!去年我国卫星导航与位置服务产值超5700亿