unity创建和加载AssetBundle
先说一下为什么要使用AssetBundle吧,以前做东西一直忽略这个问题,现在认为这个步骤很重要,代码是次要的,决策和为什么这样搞才是关键。
一句话概括吧,AssetBundle实现了资源与服务分离,方便做热更新。
一、创建AssetBundle
两步:1.设置AssetBundleName;2.调用BuildPipeline.BuildAssetBundles。详细过程如下:
设置AssetBundleName有两种方式,分为手动和代码
先讲手动,找到你需要被打包的文件,然后如下图

方式2,代码设置这个AssetBundleName,代码如下,创建bundle的代码也一起贴上了:
using UnityEngine;
using System.IO;
#if UNITY_EDITOR
using UnityEditor;
#endif public class BuildBundleMenu : MonoBehaviour { public static string sourcePathPrefab = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Prefabs/";
public static string sourcePathMater = Application.dataPath + "/_Creepy_Cat/Realistic_Weather_Effects/_Materials/"; #if UNITY_EDITOR
[MenuItem( "Example/Build Asset Bundles" )]
static void BuildABs( )
{ ClearAssetBundlesName (); FindAllFile (sourcePathPrefab);
FindAllFile (sourcePathMater); // Put the bundles in a folder called "ABs" within the Assets folder.
BuildPipeline.BuildAssetBundles( "Assets/ABs", BuildAssetBundleOptions.UncompressedAssetBundle, BuildTarget.StandaloneOSXIntel64);
} /// <summary>
/// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
/// </summary>
static void ClearAssetBundlesName()
{
int length = AssetDatabase.GetAllAssetBundleNames ().Length;
Debug.Log (length);
string[] oldAssetBundleNames = new string[length];
for (int i = 0; i < length; i++)
{
oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
} for (int j = 0; j < oldAssetBundleNames.Length; j++)
{
AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j],true);
}
length = AssetDatabase.GetAllAssetBundleNames ().Length;
Debug.Log (length);
} /// <summary>
/// 遍历文件夹里面的所有文件夹和文件
/// </summary>
/// <param name="source"></param>
static void FindAllFile(string source)
{
DirectoryInfo folder = new DirectoryInfo (source);
FileSystemInfo[] files = folder.GetFileSystemInfos ();
int length = files.Length;
for (int i = 0; i < length; i++) {
if(files[i] is DirectoryInfo)
{
FindAllFile(files[i].FullName);
}
else
{
if(!files[i].Name.EndsWith(".meta"))
{
SetAssetName (files[i].FullName);
}
}
}
} /// <summary>
/// 为需要打包的文件设置assetName.
/// </summary>
/// <param name="source"></param>
static void SetAssetName(string source)
{
string _assetPath = "Assets" + source.Substring (Application.dataPath.Length);
//string _assetPath2 = source.Substring (Application.dataPath.Length + 1);
//在代码中给资源设置AssetBundleName
AssetImporter assetImporter = AssetImporter.GetAtPath (_assetPath);
//string assetName = _assetPath2.Substring (_assetPath2.IndexOf("/") + 1);
//assetName = assetName.Replace(Path.GetExtension(assetName),".unity3d");
//assetImporter.assetBundleName = assetName;
assetImporter.assetBundleName = "Weather.unity3d";
}
#endif
}
菜单栏会出现这个,点一下就可以了,bundle创建完成。注意打bundle前要现在Assets目录下新建一个ABs文件夹,打的bundle都在这个文件夹里面。

注:只要设置了AssetBundleName的,都会进行打包,不论在什么目录下。
二、bundle的加载
直接贴代码吧
using System.Collections;
using System.Collections.Generic;
using UnityEngine; public class LoadAssetbundle : MonoBehaviour { private string pathurl = "";
// Use this for initialization
void Start () {
string pathPrefab = "file://" + Application.dataPath + "/ABs/weather.unity3d"; StartCoroutine (LoadALLGameObject(pathPrefab));
} //读取全部资源
private IEnumerator LoadALLGameObject(string path)
{ WWW bundle = new WWW(path); yield return bundle; //通过Prefab的名称把他们都读取出来
Object obj0 = bundle.assetBundle.LoadAsset("CloudStorm_02_8x8-64.prefab"); //加载到游戏中
yield return Instantiate(obj0);
bundle.assetBundle.Unload(false);
}
}
unity创建和加载AssetBundle的更多相关文章
- 1.2 控制器 view 的创建和加载
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 加载优先顺序: 1.用系统的loadView方法创建控制器的视图 2.如果指定 ...
- 创建控制器的3种方式、深入了解view的创建和加载顺序
转载自:http://blog.csdn.net/weisubao/article/details/41012243 (1)创建控制器的3种方式 - (BOOL)application:(UIAppl ...
- Unity最新版打包AssetBundle和加载的方法
一.设置assetBundleName二.构建AssetBundle包三.上传AssetBundle到服务器四.把AssetBundle放到本地五.操作AssetBundle六.完整例子七.Asset ...
- Unity 打AssetBundle和加载方案
一.如何组织assetBundle: unity5以前,打包需要自己去找依赖,然后需要按照拓扑图顺序压入AB栈,这样在最后打AB时才能有效利用依赖(栈内已有的AB才能作为依赖). unity5.x后, ...
- AssetBundle压缩/内部结构/下载和加载
一.AssetBundle的压缩方式 Unity支持三种AssetBundle打包的压缩方式:LZMA, LZ4, 以及不压缩. 1.LZMA压缩方式 是一种默认的压缩形式,这种标准压缩格 ...
- 【Unity游戏开发】AssetBundle杂记--AssetBundle的二三事
一.简介 马三在公司大部分时间做的都是游戏业务逻辑和编辑器工具等相关工作,因此对Unity AssetBundle这块的知识点并不是很熟悉,自己也是有打算想了解并熟悉一下AssetBundle,掌握一 ...
- Unity资源解决方案之AssetBundle
1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material. ...
- unity动态加载(翻译) .
AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...
- Unity动态加载和内存管理(三合一)
原址:http://game.ceeger.com/forum/read.php?tid=4394#info 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Re ...
随机推荐
- AngularJS 拦截器实现全局$http请求loading效果
日常项目开发中,当前端需要和后端进行数据交互时,为了友好的UI效果,一般都会在前端加个loading的状态提示(包括进度条或者icon显示),数据传输或交互完成之后,再隐藏/删除loading提示. ...
- python爬虫入门--beautifulsoup
1,beautifulsoup的中文文档:https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/ 2, from bs4 import Be ...
- CCF-201503-3-节日
问题描述 试题编号: 201503-3 试题名称: 节日 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 有一类节日的日期并不是固定的,而是以"a月的第b个星期c&q ...
- CCF-201312-2-ISBN号码
问题描述 试题编号: 201312-2 试题名称: ISBN号码 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包 ...
- javascript面向对象系列第四篇——OOP中的常见概念
前面的话 面向对象描述了一种代码的组织结构形式——一种在软件中对真实世界中问题领域的建模方法.本文将从理论层面,介绍javascript面向对象程序程序(OOP)中一些常见的概念 对象 所谓对象,本质 ...
- git操作之上传gitthub
push 失败解决方法: 分支操作: 分支操作之覆盖: 主master操作:
- [设计模式]PHP设计模式之单例模式
面试最常见的问题之一就是 请问您知道哪些设计模式这次先介绍最经典的单例模式.单例模式分3种:懒汉式单例.饿汉式单例.登记式单例.单例模式有以下3个特点:1.只能有一个实例.2.必须自行创建这个实例. ...
- 分布式监控系统Zabbix3.2对数据库的连接数预警
在前篇分布式监控系统Zabbix3.2监控数据库的连接数 中已经对数据库的端口3306进行了监控,可以看到数据库的连接数历史变化有高有低,那如果达到了数据库连接数的阀值是不是主动通知给运维人员去检查问 ...
- oracle恢复已删除的表
drop 误删除表之后使用flashback table tablename to before drop 可恢复或者使用flashback table "BIN$gcfME7ObTx+n0 ...
- myBatis数据库常用标签
<sql id=""></sql>:封装sql语句,被其他sql调用 <include refid=""></incl ...