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 ...
随机推荐
- DIV居中的经典方法
1. 实现DIV水平居中 设置DIV的宽高,使用margin设置边距0 auto,CSS自动算出左右边距,使得DIV居中. 1 div{ 2 width: 100px; 3 height: 100px ...
- Jersy、Jetty和Servlet
1.Jersy框架 Jersey RESTful WebService框架是一个开源的.产品级别的JAVA框架,是JAX-RS的参考实现.Jersey提供自己的API,其API继承自JAX-RS,提供 ...
- 关于pipelineDB调用GetLocalStreamReaders的BUG
如果想获取一个stream所有的reader,那么必须调用这个函数: Bitmapset *targets = GetLocalStreamReaders(relid); 如果stream下面没有re ...
- uva11059(最大乘积)
Problem D - Maximum Product Time Limit: 1 second Given a sequence of integers S = {S1, S2, ..., Sn}, ...
- hdu4932 Miaomiao's Geometry (BestCoder Round #4 枚举)
题目链接:pid=4932" style="color:rgb(202,0,0); text-decoration:none">http://acm.hdu.edu ...
- 【分享】iTOP4412开发板-Bluetooth移植文档
[分享]iTOP4412开发板-Bluetooth移植文档 最近须要把Bluetooth移植到iTOP-4412 开发平台.查阅了相关资料,经过一段时间的研究.调试,最终成功的将蓝牙功能移植到了开发板 ...
- Zend_Json 简介 --(手冊)
1. 简单介绍 Zend_Json 提供一个方便的方式来串联(native的)PHP(的变量)和JSON,并将JSON(对象)解码到PHP中. 2. 基本使用方法 Zend_Json的使用包含使用 ...
- 自学Zabbix3.6.2-触发器triggers severity严重程度
触发器严重性定义了触发器的重要性. 1. zabbix支持以下触发级别: SEVERITY DEFINITION 颜色 Not classified 未知. 灰色 Information 一般信息. ...
- 《C++程序设计语言(十周年纪念版)》【PDF】下载
<C++程序设计语言(十周年纪念版)>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230382171 内容简介 <C++程序设计 ...
- Android开发——使用LitePal开源数据库
前言:之前使用Android内置的数据库,感觉一大堆SQL语句,一不小心就错了,很难受,学习了这个LItePal的开源数据库,瞬间觉得Android内置的数据库简直是垃圾般的存在 LitePal Gi ...