1.首先引用using UnityEditor;
2.用MenuItem:[MenuItem("Assets/Build AssetBundles")]创建一个unity的编辑器按钮
3.在编辑器按钮中添加打包assetbundle的代码
BuildPipeline.BuildAssetBundles("Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
4.比如可以在预设中设置assetbundle的属性,然后点击新增的编辑器中的打包选项。
1、什么是AssetBundle
AssetBundle是Unity pro提供的一种用来存储资源的文件格式,它可以存储任意一种Unity引擎能够识别的资源,如Scene、Mesh、Material、Texture、Audio、noxss等等,同时,AssetBundle也可以包含开发者自定义的二进制文件,只需要将自定义文件的扩展名改为.bytes,Unity就可以把它识别为TextAsset,进而就可以被打包到AssetBundle中。Unity引擎所能识别的资源我们称为Asset,AssetBundle就是Asset的一个集合。
AssetBundle的特点:
压缩(缺省)、动态载入、本地缓存;
2、AssetBundle VS Resource
AssetBundle作为Unity官方推崇的资源更新方案,与传统的Resource差异如下:
a、Resource放在Resources目录下,resources.assets文件,单个文件有2GB限制,首次必须全部下载;
b、AssetBundle创建需要通过Editor脚本创建,支持动态下载,是Unity Web Caching License唯一可以缓存的内容;
3、AssetBundle的适用平台与跨平台性
AssetBundle适用于多种平台,包括网页应用、移动应用、桌面应用等,可以动态更新,但不同平台所使用的AssetBundle并不相同,在创建离线AssetBundle的时候需要通过参数来指定目标平台,相容关系如表所示
4、AssetBundle的工作流程(与flash加载swf类似)
a、创建AssetBundle;
b、上传到Server;
c、游戏运行时根据需要下载(或者从本地cache中加载)AssetBundle文件;
d、解析加载Assets;
e、使用完毕后释放;
6.AssetBundle的制作
一。以制作一个Cube预制体为例,将Cube制作成预设profeb文件,
点击Cube的profeb文件,在属性面板上底下添加AssetBundle和右边的名字。
二。在Assets文件夹新建一个Editor文件目录,并创建一个脚本
并且创建一个AssetBundles目录,自后将AssetBundles文件打包到该目录下
-
using System.Collections;
-
using UnityEditor;
-
-
public class CreateAssetBundles
-
{
-
[]
-
static void BuildAllAssetBundles()
-
{
-
BuildPipeline.BuildAssetBundles( "Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
-
}
-
}
点击Assets,选择Build AssetBundles按钮
此时untiy将我们以命名的预制体进行打包到Assets/AssetBundles目录
AssetBundles.manifest文件是记录管理全部的AssetBundles文件
cube.assetbundles.cube文件是保存我们的预制体信息
cube.assetbundles.cube.manifest文件记录管理预制体之间的依赖关系
【加载AssetBundles文件】
加载就更简单了
直接贴代码吧
-
using System.Collections;
-
using System.Collections.Generic;
-
using UnityEngine;
-
using System;
-
-
public class LoadAssetBundles : MonoBehaviour
-
{
-
-
public string url;
-
-
public string playerName;
-
-
// Use this for initialization
-
void Start ()
-
{
-
StartCoroutine( "Load");
-
}
-
-
IEnumerator Load()
-
{
-
using (WWW www = new WWW(url))
-
{
-
-
yield return www;
-
if (www.error == null)
-
{
-
AssetBundle assetbundle = www.assetBundle;
-
UnityEngine.Object obj = assetbundle.LoadAsset(playerName);
-
-
Instantiate(obj);
-
assetbundle.Unload( false); //把用完后的资源删除,不会把全部的AssetBundle都删除了
-
}
-
else
-
{
-
Debug.Log( "资源加载失败");
-
}
-
-
}
-
-
}
-
}
将脚本挂载到游戏对象上
将基本信息填写完整后,便能实例化出游戏对象了
在这里路径后的文件名是cube.assetbundles.cube
player Name是预制体名字
url=file:///E:/Users/Unity Projects/Learning20180708/Assetsfile:///E:\Users\Unity Projects\Learning20180708\Assets\AssetBundles\capsule.assetbundle.capsule
Player Name = capsule