行业知识
unityAB包管理(远程下载)
2026-11-30 16:52:43
来源:互联网转载
1.AB包生成脚本
()using UnityEngine;using UnityEditor;using System.IO;using System;using System.Collections.Generic;/// /// AB包创建/// public class CreateAssetBundles : MonoBehaviour{ public static string BuildAssetBundlePath = Application.dataPath + "/AssetsPach/AssetBundles"; public static void BuildAssetBundle_WeixinMiniGame() { string dir = BuildAssetBundlePath; //相对路径 if (!Directory.Exists(dir)) //判断路径是否存在 { Directory.CreateDirectory(dir); } BuildPipeline.BuildAssetBundles(dir, BuildAssetBundleOptions.None, BuildTarget.WeixinMiniGame); //这里是第一点注意事项,BuildTarget类型选择WebGL AssetDatabase.Refresh(); Debug.Log("打包完成"); } //需要打包的资源目录 public static string SetAssetBundlePath = Application.dataPath + "/AssetsPach/WortAsset"; public static void SetAssetBundle_WeixinMiniGame() { string dir = SetAssetBundlePath; //相对路径 AssetDatabase.RemoveUnusedAssetBundleNames();//移除无用的AssetBundleName //Debug.LogError(Application.dataPath);//上级路径 F:/TUANJIEProject/My project/Assets list_Files = new List(); ContinueCheck(dir); for (int a = 0; a 2.AB包管理类
搭建资源服务器使用(HFS软件(https://www.pianshen.com/article/54621708008/))
() using System;using System.Net.Mime;using System.Collections;using System.Collections.Generic;using UnityEngine;using Object = UnityEngine.Object;using Cysharp.Threading.Tasks; // 首先需要引入UniTask插件using UnityEngine.Networking;using UnityEngine.U2D;//已经加载的AB类public class LoadedAssetBundle{ public AssetBundle bundle; public string abName; public int referenceCount = 0; public void AddRefer() { referenceCount++; } public int Decrease() { referenceCount--; if (referenceCount new ABManager()); public static ABManager Instance { get { return lazy.Value; } } private ABManager() { Init(); } //AB包缓存---解决AB包无法重复加载的问题 也有利于提高效率。 private Dictionary abCache; private AssetBundle mainAB = null; //主包 private AssetBundleManifest mainManifest = null; //主包中配置文件---用以获取依赖包 private string basePath = "https://xxx/gh5_test/StreamingAssets/AssetBundles/"; private string mainABName = "AssetBundles"; //继承了单例模式提供的初始化函数 void Init() { //初始化字典 abCache = new Dictionary(); } public Dictionary AssetNameToABName = new Dictionary(); public async UniTask LoadAsset(string assetName) { string abName = assetName.ToLower() + ".ab"; AssetBundle ab = await LoadABPackage(abName); return ab.LoadAsset(assetName); } /// /// 加载图集里面的图片 /// 案例 /// Image a = nul;; /// if (a != null) /// ABManager.Instance.UnloadAsset(a); /// a = ABManager.Instance.LoadAtlasSprite(a); /// /// /// /// public async UniTask LoadAtlasSprite(string assetName,string textureName) { string abName = assetName.ToLower() + ".ab"; AssetBundle ab = await LoadABPackage(abName); SpriteAtlas spriteAtlas = ab.LoadAsset(assetName); return spriteAtlas.GetSprite(textureName); } //单个包卸载 public void UnloadAsset(string assetName) { string abName = assetName.ToLower() + ".ab"; //--引用 if (abCache.ContainsKey(abName)) { int reference = abCache[abName].Decrease(); if (reference == 0) { abCache[abName].bundle.Unload(true); //注意缓存需一并移除 abCache.Remove(abName); } } } //加载AB包 private async UniTask LoadABPackage(string abName) { //加载ab包,需一并加载其依赖包。 if (mainAB == null) { //获取ab包内容 mainAB = await DownloadABPackage(mainABName); //获取主包下的AssetBundleManifest资源文件(存有依赖信息) mainManifest = mainAB.LoadAsset("AssetBundleManifest"); } //加载目标包 -- 同理注意缓存问题 if (abCache.ContainsKey(abName)) { abCache[abName].AddRefer(); //++引用 return (abCache[abName].bundle); } else { //根据manifest获取所有依赖包的名称 固定API string[] dependencies = mainManifest.GetAllDependencies(abName); if (dependencies.Length > 0) { var tasks = new List(); // 创建一个任务列表来存储异步操作 //循环加载所有依赖包 for (int i = 0; i 3.使用案例
using Cysharp.Threading.Tasks;using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.Networking;public class GameEntrance : MonoBehaviour{ // Start is called before the first frame update async void Start() { GameObject go = await ABManager.Instance.LoadAsset("Assets/AssetsPach/Wortasset/Prefabs/Canvas.prefab"); GameObject.Instantiate(go); }}
上一篇:传说之下,下载指南