先说一下为什么要使用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. 1.2 控制器 view 的创建和加载

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”       加载优先顺序: 1.用系统的loadView方法创建控制器的视图 2.如果指定 ...

  2. 创建控制器的3种方式、深入了解view的创建和加载顺序

    转载自:http://blog.csdn.net/weisubao/article/details/41012243 (1)创建控制器的3种方式 - (BOOL)application:(UIAppl ...

  3. Unity最新版打包AssetBundle和加载的方法

    一.设置assetBundleName二.构建AssetBundle包三.上传AssetBundle到服务器四.把AssetBundle放到本地五.操作AssetBundle六.完整例子七.Asset ...

  4. Unity 打AssetBundle和加载方案

    一.如何组织assetBundle: unity5以前,打包需要自己去找依赖,然后需要按照拓扑图顺序压入AB栈,这样在最后打AB时才能有效利用依赖(栈内已有的AB才能作为依赖). unity5.x后, ...

  5. AssetBundle压缩/内部结构/下载和加载

    一.AssetBundle的压缩方式   Unity支持三种AssetBundle打包的压缩方式:LZMA, LZ4, 以及不压缩.    1.LZMA压缩方式  是一种默认的压缩形式,这种标准压缩格 ...

  6. 【Unity游戏开发】AssetBundle杂记--AssetBundle的二三事

    一.简介 马三在公司大部分时间做的都是游戏业务逻辑和编辑器工具等相关工作,因此对Unity AssetBundle这块的知识点并不是很熟悉,自己也是有打算想了解并熟悉一下AssetBundle,掌握一 ...

  7. Unity资源解决方案之AssetBundle

    1.什么是AssetBundle AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene.Mesh.Material. ...

  8. unity动态加载(翻译) .

    AssetBundles are files which you can export from Unity to contain assets of your choice. These files ...

  9. Unity动态加载和内存管理(三合一)

    原址:http://game.ceeger.com/forum/read.php?tid=4394#info 最近一直在和这些内容纠缠,把心得和大家共享一下: Unity里有两种动态加载机制:一是Re ...

随机推荐

  1. 使用Navicat导入.csv文件(过程和注意点)

    1.创建一个数据库,右键点击表,选择导入向导. 2.在跳出的弹窗中选择.CSV文件,点击下一步 3.选择文件来源和编码规格,点击下一步 如果发现上传后中文出现乱码请使用10008这个编码规则 4.选择 ...

  2. 基于MySQL + Node.js + Leaflet的离线地图展示,支持百度、谷歌、高德、腾讯地图

    1. 基本说明 本项目实现了离线展示百度.谷歌.高德.腾讯地图.主要功能如下: 实现了地图瓦片图下载.存储.目前支持存储至MySQL Node.js服务调用MySQL中的瓦片图 Leaflet展示地图 ...

  3. 简单购物车程序(Python)

    #简单购物车程序:money_all=0tag=Trueshop_car=[]shop_info={'apple':10,'tesla':100000,'mac':3000,'lenovo':3000 ...

  4. [转] linux系统中如何进入退出vim编辑器,方法及区别

    原文链接:http://www.cnblogs.com/crazylqy/p/5649860.html 在linux家族中,vim编辑器是系统自带的文本编辑器,其功能强大自不必说了. 偶有小白,刚接触 ...

  5. 深入理解php底层:php生命周期

    1.PHP的运行模式: PHP两种运行模式是WEB模式.CLI模式.无论哪种模式,PHP工作原理都是一样的,作为一种SAPI运行. 1.当我们在终端敲入php这个命令的时候,它使用的是CLI. 它就像 ...

  6. CentOS7 nginx简单配置pathinfo模式(ThinkPHP)

    location ~ \.php {    #去掉$ root          H:/PHPServer/WWW; fastcgi_pass   127.0.0.1:9000; fastcgi_in ...

  7. poj 2905 双向队列(待补充)

    Parallel Computer Simulator   Description Programs executed concurrently on a uniprocessor system ap ...

  8. Struts2学习笔记整理(一)

    最近在学习框架,很多人建议我直接学SSM,SSM看了一段时间后发现很多东西虽然可以用了,但是并不是很了解,所以我打算重新来过.从SSH开始学习,前面已经大致的学习了Hibernate,对于Hibern ...

  9. Git 经常使用命令总结

    一 关于加入.删除和回退 1 git rm --cached file  想要git不再跟踪这个文件,可是又不想在硬盘中删除该文件 2 在被git管理的文件夹中删除文件时,能够选择例如以下两种方式: ...

  10. 《Javascript_Dom 编程艺术》(第2版)读书笔记

    第1章 Javascript 简史 Dom : 平稳退化.渐进增强,以用户为中心的设计 第2章 Javascript 语法 1.程序设计语言分为:解释性(javascript)和编译型(java,C+ ...