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 ...
随机推荐
- mysql left join 几个意思
left join 用于多表 >1个表比如select a.*,b.* from ta as a left join tb as b on a.aid=b.bid咱们就以实际的代码来查看一下. ...
- staticmethod、classmethod的使用
staticmethod 首先要明白两个概念 绑定方法:但凡是定义在类的内部,并且没有被任何装饰器修饰过的方法,就是绑定方法,并且有自动传值功能.类直接调用该方法时,改方法叫做类的函数属性:对象在调用 ...
- Nginx事件处理中的connection和read、write事件的关联
/********************************************************************* * Author : Samson * Date ...
- python模块 - re模块使用演示样例
http://blog.csdn.net/pipisorry/article/details/46619179 re模块匹配规则见:http://blog.csdn.net/pipisorry/art ...
- POJ 2127 最长公共上升子序列
动态规划法: #include <iostream> #include <cstdio> #include <fstream> #include <algor ...
- html页面高度自适应
本文实现的效果是依据浏览器分辨率的不同.页面底端背景色自适应浏览器高度,也就是能够自己主动填充背景色. <script type="text/javascript"> ...
- STL之list容器的实现框架
说明:本文仅供学习交流,转载请标明出处.欢迎转载! list的底层採用数据结构是环形的双向链表. 相对于vector容器.list容器插入和删除操作付出的代价要比vector容器小得多,可是list带 ...
- java并发编程的艺术——第一章总结
并发编程的挑战 1.1上下文切换 1.2死锁 1.3资源限制的挑战 1.4本章小结 1.1上下文切换 1.1.1多线程一定快吗 1.1.2测试上下文切换次数和时长 1.1.3如何减少上下文切换 1.1 ...
- shell脚本小案例
1.获取远程ftp数据到本地目录 #!/bin/bash ftp -n<<! open 135.0.24.19 user exchange exchange binary cd /idep ...
- TortoiseSVN/TortoiseGIT文件夹或图标不显示,非Overlay Icon
网上搜索TortoiseSVN/TortoiseGIT的图标不显示,大部分的方法都是修改Overlay Icon的名字,让Tortoise排在前面. 然后我却遇到的问题上述方法并不能解决. 我的解决办 ...