一个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/***.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>("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 = ; 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 = ; 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到没有什么变化,具体使用方式可以参考我以前的博客。

Unity5 AssetBundle系列——简单的AssetBundleManager的更多相关文章

  1. Unity5 AssetBundle系列——基本流程

    Unity5的AssetBundle修改比较大,所以第一条建议是:忘掉以前的用法,重新来!要知道,Unity5已经没办法加载2.x 3.x的bundle包了…体会一下Unity5 AssetBundl ...

  2. Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用

    下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...

  3. AssetBundle系列——场景资源之解包(二)

    本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面: (1)加载场景 场景异步加载的代码比较简单,如下所示: private IEnumerator LoadLevelCoroutine( ...

  4. Unity5 AssetBundle资源管理架构设计

    http://blog.csdn.net/qq_19399235/article/details/51702964 1:Unity5 资源管理架构设计(2017.4.22版本) 2:Android 热 ...

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

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

  6. 【Unity3D技术文档翻译】第1.9篇 使用 Unity AssetBundle Browser tool (AssetBundle系列完结)

    上一章:[Unity3D技术文档翻译]第1.8篇 AssetBundles 问题及解决方法 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced D ...

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

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

  8. Unity5 AssetBundle

    设置assetBundleName AssetImporter importer = AssetImporter.GetAtPath(p); importer.assetBundleName = x; ...

  9. Unity5 AssetBundle 打包以及加载

    using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; us ...

随机推荐

  1. 使用js生成二维码和条形码

    1.生成二维码 使用github开源项目qrcode. 1.引入方式一(js cdn引入): ①.引入qrcode cdn: 自行下载..没有合适的cdn,地址 <script src=&quo ...

  2. 算法进阶面试题02——BFPRT算法、找出最大/小的K个数、双向队列、生成窗口最大值数组、最大值减最小值小于或等于num的子数组数量、介绍单调栈结构(找出临近的最大数)

    第二课主要介绍第一课余下的BFPRT算法和第二课部分内容 1.BFPRT算法详解与应用 找到第K小或者第K大的数. 普通做法:先通过堆排序然后取,是n*logn的代价. // O(N*logK) pu ...

  3. 8,EasyNetQ-多态发布和订阅

    您可以订阅一个接口,然后发布该接口的实现. 我们来看一个例子. 我有一个接口IAnimal和两个实现猫和狗: public interface IAnimal { string Name { get; ...

  4. android studio 汉化 svn插件汉化。布局文件 属性 汉化 public.xml

    android studio 汉化 SvnBundle.properties D:\Android Studio\plugins\svn4idea\lib resources_en.jar\org\j ...

  5. window10 还原精灵 破解版 冰点

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha=======

  6. BZOJ.1468.Tree(点分治)

    BZOJ1468 POJ1741 题意: 计算树上距离<=K的点对数 我们知道树上一条路径要么经过根节点,要么在同一棵子树中. 于是对一个点x我们可以这样统计: 计算出所有点到它的距离dep[] ...

  7. python面向对象笔记

    一.封装(属性/私有方法/公有方法/静态方法/构造函数...) # 定义一个类 class Animal: # 私有成员(用_开头的约定为私有成员 - 注:仅仅是君子协定) _age = 0 # 构造 ...

  8. windows Server 2008 R2 开关机取消登录时要按Ctrl+Alt+Delete组合键登录的方法

    1.点桌面任务栏的“开始-->运行”在弹出的窗口中输入gpedit.msc . 2.找到如下图所示的位置 右键属性进行设置如下

  9. iOS开发-获取属性和方法

    iOS开发数据存储有两种方式,属性列表和对象编码,属性列表可以通过NSArray,NSMutableArray,NSMutableDictionary,存储对象我们可以通过归档和解档来完成.如果我们想 ...

  10. AS 自定义 Gradle plugin 插件 案例 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...