一个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. Python学习——Python线程

    一.线程创建 #方法一:将要执行的方法作为参数传给Thread的构造方法 import threading import time def show(arg): time.sleep(2) print ...

  2. 11.1 正睿停课训练 Day14

    目录 2018.11.1 正睿停课训练 Day14 A 字符串 B 取数游戏(贪心) C 魔方(模拟) 考试代码 B C 2018.11.1 正睿停课训练 Day14 时间:3.5h 期望得分:100 ...

  3. Python进制转换(二进制/八进制/十进制/十六进制)

    Python 进制转换 二进制 八进制 十进制 十六进制 作者:方倍工作室 地址:http://www.cnblogs.com/txw1958/p/python3-scale.html 全局定义 ba ...

  4. SharePoint 2019 图文安装教程

    前言 SharePoint 2019刚刚发布,很多群友在寻找安装教程,霖雨正好也下载了进行体验,就把完整的安装过程做成图文教程,分享给大家了,有需要的人可以有个参考. 文章从创建虚拟机开始,可能有点啰 ...

  5. docker使用大全 tomcat安装

    um install docker #安装docker docker search tomcat docker pull docker.io/tomcat # 安装tomcat镜像 docker im ...

  6. iOS开发-消息转发

    消息转发是OC运行时比较重要的特性,Objective-C运行时的主要的任务是负责消息分发,我们在开发中"unrecognized selector sent to instance xx& ...

  7. iOS开发-观察者模式

    观察者模式也被称作发布/订阅模式,观察者模式定义了一种一对多的依赖关系,让多个观察者对象同时监听某一个主题对象.这个主题对象在状态发生变化时,会通知所有观察者对象,使它们能够自动更新自己.观察者模式中 ...

  8. Windows上的git、github部署及基本使用方法

    1.介绍 Git是一个开源的分布式版本控制系统,可以有效.高速地处理从很小到非常大的项目版本管理.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本 ...

  9. 通俗理解word2vec

    https://www.jianshu.com/p/471d9bfbd72f 独热编码 独热编码即 One-Hot 编码,又称一位有效编码,其方法是使用N位状态寄存器来对N个状态进行编码,每个状态都有 ...

  10. phpBB3.2开发环境配置

    从Github导出项目 如果只是查看代码, 可以直接clone官方的git https://github.com/phpbb/phpbb.git . 如果需要开发, 就fork一下再从自己的Git里c ...