一个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<string, AssetBundle> dicAssetBundle = new Dictionary<string, AssetBundle>();// filename : Assets全路径,比如Assets/Prefab/***.prefabpublic 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>("AssetBundleManifest");// 压缩包直接释放掉bundle.Unload(false);bundle = null;}// filename : Assets全路径,比如Assets/Prefab/***.prefabpublic 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/***.prefabpublic 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.assetbundleprivate string AssetsNameToBundleName(string file){string f = file.Replace('/', '.');f = f.ToLower();f += ".assetbundle";return f;}// assets.prefab.***.prefab.assetbundle --> C:/***path***/assets.prefab.***.prefab.assetbundleprivate string BundleNameToBundlePath(string bundleFilename){return System.IO.Path.Combine(Application.dataPath + "/StreamingAssets/", bundleFilename);}}

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

Unity5 AssetBundle系列——简单的AssetBundleManager相关推荐

  1. AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  2. (转)AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...

  3. H264系列--简单的视频编码历史介绍

    什么是视频编码 所谓视频编码方式就是指通过压缩技术,将原始视频格式的文件转换成另一种视频格式文件的方式 常见视频编码格式 编码格式有很多,如下图: 视频编码格式.png 目前比较常用的编码有 H26x ...

  4. python 绘图脚本系列简单记录

    简单记录平时画图用到的python 便捷小脚本 1. 从单个文件输入 绘制坐标系图 #!/usr/bin/python # coding: utf-8 import matplotlib.pyplot ...

  5. 1. VIM 系列 - 简单入门,拾起兴趣

    目录 1. 认识模式 1.1 正常模式 1.2 插入模式 1.3 命令模式 1.4 可视模式 2. 常用快捷键 1. 认识模式 vim 一共有四种模: 1. 正常模式 2. 插入模式 3. 命令模式 ...

  6. 频谱图水印matlab,插件系列--简单水印(watermark-dom)和算法水印(频域方式图片合并)实现...

    插件github地址:https://github.com/saucxs/wat... 有详细的使用步骤,可以参考,不会用请留言,感觉可以,请给个星星. 一.简单水印(watermark-dom) 阿 ...

  7. 设计模式系列-简单工厂模式

    工厂模式,首先看下Wikipedia的描述 Factory Pattern is "A factory is an object for creating other objects&quo ...

  8. (学习笔记)【目标检测】YOLO系列简单归纳

    文章目录 絮絮叨叨 一.YOLO发展史 二.逐篇学习 1.You Only Look Once: Unified, Real-Time Object Detection 2.YOLO9000 3.YO ...

  9. c语言小鱼的游泳时间,小鱼系列简单题参考代码

    有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上休息,每天都是如此,请问这样过了n天以后,一共累计游泳了多少公里呢? C++ #include int main () { int a; sc ...

最新文章

  1. 2011年图书馆技术发展方向
  2. Windows最经典应用大变脸:学生爽翻!
  3. Cocos2d-x 3.x plist+png 做动画
  4. ajax和cs的关系,fetch、axios 与Ajax之间关系
  5. base64加密原理及python、C语言代码实现
  6. 无限踩坑系列(5)-MySQLdb
  7. alias怎么每次登陆都保存_设置alias别名并使之重启或者注销用户之后依旧生效...
  8. 15-Scala使用Option、Some、None,避免使用null
  9. 图片居中裁剪_魔镜,魔镜,谁最美丽!利用PS图层混合模式打造图片幻觉效果
  10. DevEco Studio 使用和问题总结
  11. win10更新完提示未安装任何音频输出设备2019-11-13解决
  12. 计算机硬件配件怎么查,如何查看计算机主板型号?主板型号在哪里[方法]
  13. 插头DP 概率DP / 期望DP
  14. 应用电路笔记(1)-三极管8550和8050应用
  15. Theorem、Proposition、Lemma和Corollary等的解释与区别
  16. 基于Go语言Revel+Layui的OA办公系统
  17. 电脑视频加水印软件哪个好?这些软件值得收藏
  18. 天天向商为什么更名,更名为稿定设计之后有什么变化?
  19. Flink 入门教程
  20. 入力禁则文字check

热门文章

  1. 几道题目理解JavaScript作用域、作用域链、预解析规则、表达式
  2. TCP/IPICMP报文的分类
  3. hbase_异常_03_java.io.EOFException: Premature EOF: no length prefix available
  4. PHP的学习--Traits新特性
  5. UITableView——reloadData与reloadSection性能比较
  6. HDU 1004 Let the Balloon Rise
  7. Wake-On-LAN待机或休眠模式中唤醒
  8. CentOS 6.x 使用安装光盘作为yum源
  9. NLPIR-KGB知识图谱引擎突破传统数据挖掘束缚
  10. 友盟分享和cocos2dx符合重复duplicate symbol 解决方案