Unity5 AssetBundle系列——简单的AssetBundleManager
一个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的更多相关文章
- Unity5 AssetBundle系列——基本流程
Unity5的AssetBundle修改比较大,所以第一条建议是:忘掉以前的用法,重新来!要知道,Unity5已经没办法加载2.x 3.x的bundle包了…体会一下Unity5 AssetBundl ...
- Unity5 AssetBundle系列——资源加载卸载以及AssetBundleManifest的使用
下面代码列出了对于assetbundle资源的常用操作,其中有针对bundle.asset.gameobject三种类型对象的操作,实际使用中尽量保证成对使用. 这一块的操作比较繁琐,但只要使用正确, ...
- AssetBundle系列——场景资源之解包(二)
本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面: (1)加载场景 场景异步加载的代码比较简单,如下所示: private IEnumerator LoadLevelCoroutine( ...
- Unity5 AssetBundle资源管理架构设计
http://blog.csdn.net/qq_19399235/article/details/51702964 1:Unity5 资源管理架构设计(2017.4.22版本) 2:Android 热 ...
- AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- 【Unity3D技术文档翻译】第1.9篇 使用 Unity AssetBundle Browser tool (AssetBundle系列完结)
上一章:[Unity3D技术文档翻译]第1.8篇 AssetBundles 问题及解决方法 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced D ...
- (转)AssetBundle系列——共享资源打包/依赖资源打包
有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...
- Unity5 AssetBundle
设置assetBundleName AssetImporter importer = AssetImporter.GetAtPath(p); importer.assetBundleName = x; ...
- Unity5 AssetBundle 打包以及加载
using UnityEngine; using System.Collections; using System.Collections.Generic; using UnityEditor; us ...
随机推荐
- SSID 已经一个路由器设多个SSID
SSID(Service Set Identifier) SSID,AP唯一的ID码,许多人认为可以将SSID写成ESSID,其实不然,SSID是个笼统的概念,包含了ESSID和BSSID,用来区 ...
- Xamarin Android组件篇教程RecylerView动画组件RecylerViewAnimators(1)
Xamarin Android组件篇教程RecylerView动画组件RecylerViewAnimators(1) RecyclerView是比ListView和GridView更为强大的布局视图, ...
- BZOJ.4766.文艺计算姬(Prufer)
题目链接 这是完全二分图,那么在构造Prufer序列时,最后会剩下两个点,两点的边是连接两个集合的,这两个点自然分属两个集合 那么集合A被删了m-1次,每次从n个点中选:B被删了n-1次,每次都可以从 ...
- BZOJ.3252.攻略(贪心 长链剖分/线段树)
题目链接 贪心,每次选价值最大的一条到根的链.比较显然(不选白不选). 考虑如何维护这个过程.一个点的价值选了就没有了,而它只会影响它子树里的点,可以用DFS序+线段树修改.而求最大值也可以用线段树. ...
- COGS.1822.[AHOI2013]作业(莫队 树状数组/分块)
题目链接: COGS.BZOJ3236 Upd: 树状数组实现的是单点加 区间求和,采用值域分块可以\(O(1)\)修改\(O(sqrt(n))\)查询.同BZOJ3809. 莫队为\(O(n^{1. ...
- Sphinx(coreseek)一些记录
之前用Sphinx(coreseek)几次,相对正常 这次用到,记录些问题 1.coreseek好像没人维护了吗,有点可惜. 2.centOS7 编译和coreseek版本有点问题,需要修改编译文件 ...
- Python图形编程探索系列-08-再次认识标签
标签的各种属性 代码展示: import tkinter as tk root = tk.Tk() root.geometry = '500x300' label1 = tk.Label(root, ...
- [BZOJ2238]Mst
[BZOJ2238]Mst 题目大意: 给你一个\(n(n\le50000)\)个点,\(m(m\le10^5)\)条边的无向带权图.\(q(q\le10^5)\)次询问,每次询问去掉一条边后图能否连 ...
- ps怎么撤销的三种方法和ps撤销快捷键以及连续撤销多步快捷键
内容提要:文章综合介绍ps撤销快捷键相关的一些操作,包括PS怎么撤销.PS撤销多步.ps连续撤销快捷键.历史记录面板操作等等. 关于ps怎么撤销操作,有多种方法:使用PS撤销快捷键.编辑菜单.文件菜单 ...
- Android避免快速双击按钮最简单好用的方式
代码如下,直接放到工具类中即可.类可以实现Onclicklistener,然后重写onClick方法,直接将该函数写在onClick方法中即可,这样对于所有的点击事件都将生效. 避免了快速双击出现的异 ...