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 ...
随机推荐
- vue-particles粒子动画效果
1.安装vue-particles依赖包 npm install vue-particles --save-dev 2.在main.js文件中引入并使用 import Vue from 'vue' i ...
- Java并发程序设计(十)设计模式与并发之Future模式
设计模式与并发之Future模式 核心思想:异步调用. /** * @author: Tang Jiujia * @version: 2017/9/18 0018 15:22 */ public in ...
- python计算文件夹大小——os.walk
下面提供一种利用os.walk()快捷的计算指定文件夹大小的方法 话不多说,直接上代码 简略版: import os def get_file_size(file_path, size=0): for ...
- [模板][P3808]AC自动机(简单版)
Description: 求n个模式串中有几个在文本串中出现 Solution: 模板,详见代码: #include<bits/stdc++.h> using namespace std; ...
- react-native学习资源
转载链接: http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/react-native-learning-resources/ 这是 ...
- iphone程序适配ipad可以用下面的宏进行尺寸改写
//判断是否为iPad #define ISIPAD [[[UIDevice currentDevice].model substringToIndex:4] isEqualToString:@&qu ...
- 怎样使用ZOL一键安装器下载中关村在线的源安装包
怎样使用ZOL一键安装器下载中关村在线的源安装包 马根峰 (广东联合电子服务股份有限公司, 广州 510300) 摘要 中关村在线近期開始推出ZOL一键安装器,用户直 ...
- iOS 10.3下使用Fiddler抓取HTTPS请求
最近使用Fiddler抓取手机上的HTTPS请求时,遇到一个问题.设置完之后,访问HTTPS的页面,却得到一个错误 网络出错,轻触屏幕重新加载:-1202 (如下图所示) 我印象中,之前都好好的, ...
- 独热编码和dummy编码的作用
参考这篇文章: https://www.cnblogs.com/lianyingteng/p/7792693.html 总结:我们使用one-hot编码时,通常我们的模型不加bias项 或者 加上bi ...
- iframe实现Ajax文件上传效果示例
<!doctype html> <html> <head> <meta charset=utf-8> <head> <title> ...