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

Unity 简单打包脚本

打包脚本

这个打包脚本适用于做demo,脚本放在Editor目录下

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;public class BuildAB
{[MenuItem("Tools/递归遍历文件夹下的资源并打包")]public static void BuildDirectory(){//需要打包的文件夹路径string path = Application.dataPath + "/Res";List<string> list = new List<string>();GetFilePath(path, ref list);List<AssetBundleBuild> mult = new List<AssetBundleBuild>();for (int i = 0; i < list.Count; i++){string assetName = list[i].Replace(Application.dataPath, "Assets").Replace("\\", "/");string bundleName = Path.GetFileNameWithoutExtension(assetName);AssetBundleBuild ab = new AssetBundleBuild();ab.assetBundleName = bundleName;//assetNames记录bundle中所有资源的相对路径ab.assetNames = new string[] { assetName };mult.Add(ab);Debug.Log($"{assetName} {bundleName}");}string targetPath = Application.streamingAssetsPath;if (Directory.Exists(targetPath))Directory.Delete(targetPath, true);Directory.CreateDirectory(targetPath);BuildPipeline.BuildAssetBundles(targetPath, mult.ToArray(),BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);AssetDatabase.Refresh();}[MenuItem("Tools/遍历文件夹下的资源,处理依赖并打包")]public static void BuildDirectoryDependence(){string path = Application.dataPath + "/Res";List<string> list = new List<string>();GetFilePath(path, ref list);//key 资源路径,value 引用计数Dictionary<string, int> abDict = new Dictionary<string, int>();for (int i = 0; i < list.Count; i++){string assetName = list[i].Replace(Application.dataPath, "Assets").Replace("\\", "/");//获取依赖资源,返回相对路径string[] dependences = AssetDatabase.GetDependencies(assetName);for(int j = 0; j < dependences.Length; ++j){if (dependences[j].EndsWith(".cs")){continue;}Debug.Log($"{assetName} 依赖 {j} => {dependences[j]}");if (abDict.ContainsKey(dependences[j])){abDict[dependences[j]]++;}else{abDict.Add(dependences[j], 1);}}}var enumerator = abDict.GetEnumerator();List<AssetBundleBuild> mult = new List<AssetBundleBuild>();while (enumerator.MoveNext()){string fileName = enumerator.Current.Key;int refCount = enumerator.Current.Value;string bundleName = Path.GetFileNameWithoutExtension(fileName);if(refCount > 1){//引用计数大于1的为公共资源,单独打包bundleName = "common/" + bundleName;}AssetBundleBuild ab = new AssetBundleBuild();ab.assetBundleName = bundleName;//assetNames记录bundle中所有资源的相对路径ab.assetNames = new string[] { fileName };mult.Add(ab);}string targetPath = Application.streamingAssetsPath;if (Directory.Exists(targetPath))Directory.Delete(targetPath, true);Directory.CreateDirectory(targetPath);BuildPipeline.BuildAssetBundles(targetPath, mult.ToArray(),BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows64);AssetDatabase.Refresh();}/// <summary>/// 递归获取所有文件路径/// </summary>public static void GetFilePath(string path, ref List<string> list){string[] files = Directory.GetFiles(path);for (int i = 0; i < files.Length; i++){if (files[i].EndsWith(".cs") || files[i].EndsWith(".meta")){continue;}list.Add(files[i]);}string[] dirs = Directory.GetDirectories(path);for(int i = 0; i < dirs.Length; i++){GetFilePath(dirs[i], ref list);}}
}

在这里插入图片描述
前面的选项不会处理依赖关系,公共资源会打进不同的bundle里
后面的选项会处理依赖资源,引用计数大于1的认为是公共资源,单独打包到common文件夹

加载脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class LoadAB : MonoBehaviour
{private AssetBundleManifest _manifest;private List<string> _abList = new List<string>();private Dictionary<string, GameObject> _assetDict = new Dictionary<string, GameObject>();void Start(){LoadManifest();LoadGameObject("cube");}void LoadManifest(){AssetBundle assetBundle = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/StreamingAssets");_manifest = assetBundle.LoadAsset<AssetBundleManifest>("AssetBundleManifest");}private void LoadGameObject(string abName){if (string.IsNullOrEmpty(abName))return;abName = abName.ToLower();//先加载依赖资源string[] dependens = _manifest.GetAllDependencies(abName);for (int i = 0; i < dependens.Length; ++i){if (!_abList.Contains(dependens[i])){AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + dependens[i]);_abList.Add(dependens[i]);}}if (!_abList.Contains(abName)){AssetBundle ab = AssetBundle.LoadFromFile(Application.streamingAssetsPath + "/" + abName);_abList.Add(abName);_assetDict.Add(abName, ab.LoadAsset<GameObject>(abName));}if (_assetDict.ContainsKey(abName)){GameObject.Instantiate(_assetDict[abName]);}}
}

参考

untiy AssetBundle 依赖关系树

http://www.lryc.cn/news/251473.html

相关文章:

  • 基于社区电商的Redis缓存架构-缓存数据库双写、高并发场景下优化
  • Python提取PDF表格(基于AUTOSAR_SWS_CANDriver.pdf)
  • UVa1583生成元(Digit Generator)
  • 【Springboot+vue】如何运行springboot+vue项目
  • 拥抱变化,良心AI工具推荐
  • Tensorflow的日志log记录
  • C-语言每日刷题
  • 十五届海峡两岸电视主持新秀大会竞赛流程
  • 安全行业招聘信息汇总
  • 【如何学习python自动化测试】—— 浏览器驱动的安装 以及 如何更新driver
  • Spring Data Redis切换底层Jedis 和 Lettuce实现
  • wireshark自定义协议插件开发
  • 一文读懂MongoDB的全部知识点(1),惊呆面试官。
  • 仅仅通过提示词,GPT-4可以被引导成为多个领域的特定专家
  • 23.Oracle11g的UNDO表空间
  • Mybatis 操作续集2(结合上文)
  • LangChain 19 Agents Reason+Action自定义agent处理OpenAI的计算缺陷
  • 12.整数转罗马数字
  • 免费AI洗稿软件【2023最新】
  • PTA:平方回文数
  • 从“AI证件照”到“AI译制片”,爆款AIGC应用的商业化迷思
  • JAVA代码优化:Easy Excel(操作Excel文件的开源工具)
  • Linux Python ping3库使用教程(ping3命令、ping命令)
  • 分享一些基于php商城案例
  • SpringSecurity 三更草堂 学习笔记
  • 基于Java SSM仓库管理系统
  • 基于Spark对消费者行为数据进行数据分析开发案例
  • Docker镜像制作与推送
  • Pandas时序数据分析实践—基础(1)
  • 5.C转python