博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Unity5 AssetBundle系列——简单的AssetBundleManager
阅读量:6637 次
发布时间:2019-06-25

本文共 4288 字,大约阅读时间需要 14 分钟。

  一个AssetBundle同时只能加载一次,所以实际使用中一般会伴随着AssetBundle包的管理。

  下面是一个简单的AssetBundle管理器,提供了同步和异步加载函数:

using UnityEngine;using System.Collections;using System.Collections.Generic;public class AssetBundleManager{    public static AssetBundleManager Instace    {        get        {            if (_instace == null) _instace = new AssetBundleManager();            return _instace;        }    }    private static AssetBundleManager _instace = null;    private AssetBundleManifest manifest = null;    private Dictionary
dicAssetBundle = new Dictionary
(); // filename : Assets全路径,比如Assets/Prefab/***.prefab public AssetBundle GetAssetBundle(string filePath) { AssetBundle ab = null; dicAssetBundle.TryGetValue(AssetsNameToBundleName(filePath), out ab); return ab; } // 加载manifest,用来处理关联资源 public void LoadManifest() { AssetBundle bundle = AssetBundle.LoadFromFile(MainifestFilePath()); manifest = bundle.LoadAsset
("AssetBundleManifest"); // 压缩包直接释放掉 bundle.Unload(false); bundle = null; } // filename : Assets全路径,比如Assets/Prefab/***.prefab public AssetBundle Load(string filename) { string bundleName = AssetsNameToBundleName(filename); if (dicAssetBundle.ContainsKey(bundleName)) { return dicAssetBundle[bundleName]; } string[] dependence = manifest.GetAllDependencies(bundleName); for (int i = 0; i < dependence.Length; ++i) { LoadInternal(dependence[i]); } return LoadInternal(bundleName); } // filename : Assets全路径,比如Assets/Prefab/***.prefab public IEnumerator LoadAsync(string filename) { string bundleName = AssetsNameToBundleName(filename); if (dicAssetBundle.ContainsKey(bundleName)) { yield break; } string[] dependence = manifest.GetAllDependencies(bundleName); for (int i = 0; i < dependence.Length; ++i) { yield return LoadInternalAsync(dependence[i]); } yield return LoadInternalAsync(bundleName); } public void Unload(string filename, bool force = false) { string bundleName = AssetsNameToBundleName(filename); AssetBundle ab = null; if (dicAssetBundle.TryGetValue(bundleName, out ab) == false) return; if (ab == null) return; ab.Unload(force); ab = null; dicAssetBundle.Remove(bundleName); } public void UnloadUnusedAssets() { Resources.UnloadUnusedAssets(); System.GC.Collect(); } public void Release() { foreach(var pair in dicAssetBundle) { var bundle = pair.Value; if(bundle != null) { bundle.Unload(true); bundle = null; } } dicAssetBundle.Clear(); } private AssetBundle LoadInternal(string bundleName) { if (dicAssetBundle.ContainsKey(bundleName)) { return dicAssetBundle[bundleName]; } AssetBundle bundle = AssetBundle.LoadFromFile(BundleNameToBundlePath(bundleName)); dicAssetBundle.Add(bundleName, bundle); return bundle; } private IEnumerator LoadInternalAsync(string bundleName) { if (dicAssetBundle.ContainsKey(bundleName)) { yield break; } AssetBundleCreateRequest req = AssetBundle.LoadFromFileAsync(BundleNameToBundlePath(bundleName)); yield return req; dicAssetBundle.Add(bundleName, req.assetBundle); } // 名字依赖于存放目录 private string MainifestFilePath() { return Application.dataPath + "/StreamingAssets/StreamingAssets"; } // Assets/Prefab/***.prefab --> assets.prefab.***.prefab.assetbundle private string AssetsNameToBundleName(string file) { string f = file.Replace('/', '.'); f = f.ToLower(); f += ".assetbundle"; return f; } // assets.prefab.***.prefab.assetbundle --> C:/***path***/assets.prefab.***.prefab.assetbundle private string BundleNameToBundlePath(string bundleFilename) { return System.IO.Path.Combine(Application.dataPath + "/StreamingAssets/", bundleFilename); }}

  当然bundle也可以通过WWW或其他的方式来加载,这一块Unity5到没有什么变化,具体使用方式可以参考我以前的博客。

转载地址:http://ecpvo.baihongyu.com/

你可能感兴趣的文章
KVM虚拟化技术之网卡流量聚合
查看>>
CentOS查看内核版本、系统版本、系统位数
查看>>
11G Oracle RAC添加新表空间时数据文件误放置到本地文件系统的修正
查看>>
Anroid Studio第七期 - 语音动画
查看>>
用户空间访问I2C设备驱动
查看>>
Android第五期 - 更新自己的apk本地与网络两种方法
查看>>
/usr/bin/which: no ssh-copy-id in 和ssh: Could not resolve hostname
查看>>
性能测试报告编写技巧
查看>>
【博客话题】项目的成果,是整个项目团队的体现
查看>>
二十年后的回眸(2)——顺风顺水的前三年
查看>>
6.Azure负载均衡(下)
查看>>
32.Azure制作自定义的Liunx虚拟机映像
查看>>
谁来创建中国的SpaceX
查看>>
新浪微博:移动化、商业化
查看>>
Nginx之虚拟目录-root与alias的区别
查看>>
关于交换机网络通信故障排除
查看>>
CMS之图片管理(5)
查看>>
《统一沟通-微软-实战》-6-部署-5-边缘服务器-2012-07-12-5
查看>>
使用Cocos2d-x 和粒子编辑器实现“天天爱消除”场景特效
查看>>
rhel7.3下的网卡Teaming
查看>>