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 ...
随机推荐
- BZOJ 4552 [Tjoi2016&Heoi2016]排序 线段树的分裂和合并
https://www.lydsy.com/JudgeOnline/problem.php?id=4552 https://blog.csdn.net/zawedx/article/details/5 ...
- 潭州课堂25班:Ph201805201 并发(进程与线程池) 第十四课 (课堂笔记)
循环执行一个线程 # -*- coding: utf-8 -*- # 斌彬电脑 # @Time : 2018/7/20 0020 5:35 import threading import queue ...
- [清橙A1210]光棱坦克
[清橙A1210]光棱坦克 题目大意: 平面上放置了\(n(n\le7000)\)个反射装置,光纤将从某个装置出发,在经过一处装置时发生反射,若经过的装置坐标依次为\((x_1,y_1),(x_2,y ...
- 虚拟机性能监控与故障处理工具------JDK的命令行工具
①jps:虚拟机进程状况工具 功能:列出正在运行的虚拟机进程,并显示1.虚拟机执行主类名称以及2.这些进程的本地虚拟机唯一ID(LVMID). 使用频率最高的JDK命令行工具,其他的JDK工具大多需要 ...
- 移动端html页面优化
对于访问量大的网站来说,前端的优化是必须的,即使是优化1KB的大小对其影响也很大,下面来看看来自ISUX的米随随讲讲移动手机平台的HTML5前端优化,或许对你有帮助和启发. 概述 1. PC优化手段在 ...
- Windows10系统重置网络设置
使用Windows10系统户很可能会遇到网络异常,连接不上网的情况? 如此,简易方法可以尝试下.重置网络,教程如下: 1.//按下WIN+X(或右键点击开始按钮),然后选择“命令提示符(管理员)”; ...
- WPF(C#)与MATLAB混合编程
WPF(C#)与MATLAB混合编程 WPF可以为开发者提供便捷地构建用户交互界面的解决方法,而matlab则在科学计算方面有着无与伦比的优势,因此在一些需要将科学算法转换为应用软件的项目中,需要应用 ...
- db2 codepage
首先分两个层面,DB2 CODEPAGE和OS CODEPAGE(DB2SET DB2CODEPAGE相当于设定了当前实例的OS的CODEPAGE) Linux系统查看CODEPAGE的方法:在终端输 ...
- Linux学习笔记13—Vi编辑器的学习
文本编辑工具vim.vi1. vim与vi的最大区别是vim编辑的时候是带颜色显示的.Vi不带颜色显示.2. yum install -y vim-enhanced 如果没有安装VIM 使用上面的命令 ...
- 针对UDP丢包问题,进行系统层面和程序层面调优
转自:https://blog.csdn.net/xingzheouc/article/details/49946191 1. UDP概念 用户数据报协议(英语:User Datagram Proto ...