Unity中的AssetBundle笔记

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文件打包到该目录下

  1. using System.Collections;
  2. using UnityEditor;
  3. public class CreateAssetBundles
  4. {
  5. [ MenuItem("Assets/Build AssetBundles")]
  6. static void BuildAllAssetBundles()
  7. {
  8. BuildPipeline.BuildAssetBundles( "Assets/AssetBundles", BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
  9. }
  10. }

点击Assets,选择Build AssetBundles按钮

此时untiy将我们以命名的预制体进行打包到Assets/AssetBundles目录


AssetBundles.manifest文件是记录管理全部的AssetBundles文件

cube.assetbundles.cube文件是保存我们的预制体信息

cube.assetbundles.cube.manifest文件记录管理预制体之间的依赖关系


【加载AssetBundles文件】

加载就更简单了

直接贴代码吧

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. public class LoadAssetBundles : MonoBehaviour
  6. {
  7. public string url;
  8. public string playerName;
  9. // Use this for initialization
  10. void Start ()
  11. {
  12. StartCoroutine( "Load");
  13. }
  14. IEnumerator Load()
  15. {
  16. using (WWW www = new WWW(url))
  17. {
  18. yield return www;
  19. if (www.error == null)
  20. {
  21. AssetBundle assetbundle = www.assetBundle;
  22. UnityEngine.Object obj = assetbundle.LoadAsset(playerName);
  23. Instantiate(obj);
  24. assetbundle.Unload( false); //把用完后的资源删除,不会把全部的AssetBundle都删除了
  25. }
  26. else
  27. {
  28. Debug.Log( "资源加载失败");
  29. }
  30. }
  31. }
  32. }

将脚本挂载到游戏对象上


将基本信息填写完整后,便能实例化出游戏对象了

在这里路径后的文件名是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


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值