一、创建Assetbundle 在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。
一、创建Assetbundle
在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。
一个Assetbundle可以打包一个模型(这里的模型不单单指的是预制模型,可以是Project视图下的任何东西),也可以是多个模型,但两种打包方式占用的空间不一样。
比如我打包三个一样的模型(只不过他们的脚本不一样创建三个空的GameObject(One,Two,Three),分别挂载脚本One,Two,Three)。如果我为每个模型单独打包生成One,Two,Three三个Assetbundle,其所占的空间是A,B,C,但是A+B+C != D.由此可知想通的资源尽可能的打包到一起,他们共用一套资源。不相同的模型尽量分开打包。
二、分开打包(注意这个脚本必须放在Editor文件夹内,Editor文件夹没有的话需自己创建)
- /// <summary>
- /// 将选中的预制分别打包
- /// </summary>
- [MenuItem("AssetBundleDemo/Create AssetBundles By themselves")]
- static void CreateAssetBundleThemelves(){
- //获取要打包的对象(在Project视图中)
- Object[] selects = Selection.GetFiltered (typeof(Object),SelectionMode.DeepAssets);
- //遍历选中的对象
- foreach(Object obj in selects){
- //这里建立一个本地测试
- //注意本地测试中可以是任意的文件,但是到了移动平台只能读取路径StreamingAssets里面的
- //StreamingAssets是只读路径,不能写入
- string targetPath = Application.dataPath + "/AssetBundleLearn/StreamingAssets/" + obj.name + ".assetbundle";//文件的后缀名是assetbundle和unity都可以
- if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies)){
- Debug.Log(obj.name + "is packed successfully!");
- }else{
- Debug.Log(obj.name + "is packed failly!");
- }
- }
- //刷新编辑器(不写的话要手动刷新,否则打包的资源不能及时在Project视图内显示)
- AssetDatabase.Refresh ();
- }
SelectionMode.DeepAssets
这个选择模式意味着如果选择中包含多个文件,那么他将包含这个文件视图中的所有资源。
他还有其他的各种选项(以下是官方文档)
SelectionMode
Description
SelectionMode can be used to tweak the selection returned by Selection.GetTransforms.
The default transform selection mode is: SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable.
Unfiltered | Return the whole selection. |
TopLevel | Only return the topmost selected transform. A selected child of another selected transform will be filtered out. |
Deep | Return the selection and all child transforms of the selection. |
ExcludePrefab | Excludes any prefabs from the selection. |
Editable | Excludes any objects which shall not be modified. |
Assets | Only return objects that are assets in the Asset directory. |
DeepAssets | If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy. |
最核心的方法:BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies)
参数1:它只能放一个对象,因为我们这里是分别打包,所以通过循环将每个对象分别放在了这里。
参数2:可以放入一个数组对象。
参数3:要打包到的路径
参数4:默认情况下打的包只能在电脑上用,如果要在手机上用就要添加一个参数。
Android上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android)
IOS上:
BuildPipeline.BuildAssetBundle (obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies,BuildTarget.iPhone)
另外,电脑上和手机上打出来的Assetbundle不能混用,不同平台只能用自己的。
三、一起打包
- /// <summary>
- /// 将选中的预制打包到一起
- /// </summary>
- [MenuItem("AssetBundleDemo/Create AssetBundles Together")]
- static void CreateAssetBundleTogether(){
- //要打包的对象
- Object[] selects = Selection.GetFiltered (typeof(Object),SelectionMode.DeepAssets);
- //要打包到的路径
- string targetPath = Application.dataPath + "/AssetBundleLearn/StreamingAssets/Together.assetbundle";
- if(BuildPipeline.BuildAssetBundle(null,selects,targetPath,BuildAssetBundleOptions.CollectDependencies)){
- Debug.Log("Packed successfully!");
- }else{
- Debug.Log("Packed failly!");
- }
- //刷新编辑器(不写的话要手动刷新)
- AssetDatabase.Refresh ();
- }
四、读取
- using UnityEngine;
- using System.Collections;
- public class ReanAssetbundle : MonoBehaviour {
- //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。
- public static readonly string m_PathURL =
- #if UNITY_ANDROID
- "jar:file://" + Application.dataPath + "!/assets/";
- #elif UNITY_IPHONE
- Application.dataPath + "/Raw/";
- #elif UNITY_STANDALONE_WIN || UNITY_EDITOR
- "file://" + Application.dataPath + "/AssetBundleLearn/StreamingAssets/";
- #else
- string.Empty;
- #endif
- void OnGUI(){
- if(GUILayout.Button("加载分开打包的Assetbundle")){
- StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "One.assetbundle"));
- StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "Two.assetbundle"));
- StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "Three.assetbundle"));
- }
- if(GUILayout.Button("加载打包在一起的Assetbundle")){
- StartCoroutine(LoadGameObjectPackedTogether(m_PathURL + "Together.assetbundle"));
- }
- }
- //单独读取资源
- private IEnumerator LoadGameObjectPackedByThemselves(string path){
- WWW bundle = new WWW (path);
- yield return bundle;
- //加载
- yield return Instantiate (bundle.assetBundle.mainAsset);
- bundle.assetBundle.Unload (false);
- }
- IEnumerator LoadGameObjectPackedTogether (string path)
- {
- WWW bundle = new WWW (path);
- yield return bundle;
- Object one = bundle.assetBundle.Load ("One");
- Object two = bundle.assetBundle.Load ("Two");
- Object three = bundle.assetBundle.Load ("Three");
- //加载
- yield return Instantiate (one);
- yield return Instantiate (two);
- yield return Instantiate (three);
- bundle.assetBundle.Unload (false);
- }
- }
一、创建Assetbundle 在unity3d开发的游戏中,无论模型,音频,还是图片等,我们都做成Prefab,然后打包成Assetbundle,方便我们后面的使用,来达到资源的更新。相关推荐
- Unity2016 Unity3D开发VR游戏的经验
http://z.youxiputao.com/articles/8313 在4月12日的Unite 2016大会上,暴风魔镜高级产品经理吴涛分享他用Unity3D开发VR游戏的经验,以下为分享实录: ...
- 【Unity3D开发小游戏】《愤怒的小鸟》开发教程
[Unity3D开发小游戏]<愤怒的小鸟>开发教程 2019年09月11日 10:34:36 恬静的小魔龙 阅读数 1698更多 分类专栏: Unity3D日常 Unity3d手游开发 U ...
- 【Unity3D开发小游戏】《战棋小游戏》Unity开发教程
一.前言 这次想要做的一个小游戏,或者说一个小Demo,其实是一个简单且传统的战棋战斗场景.初步的设计是:在2D世界里创建一张由六边形地块组成的战斗地图,敌我双方依据体力在地图上轮流行动并向对方发动攻 ...
- Unity3D游戏开发之使用Unity3D开发2D游戏 (一)
今天要和大家分享的是基于Unity3D开发2D游戏,博主一直钟爱于国产武侠RPG,这个我在开始写Unity3D游戏开发系列文章的时候就已经说过了,所以我们今天要做的就是利用Unity3D来实现在2D游 ...
- Unity3d开发IOS游戏 基础
Unity3d开发IOS游戏 基础 @阿龙 - 649998群 1.先说明两个问题,我在WIN7下面的U3D里面,用了雅黑字体,但是导出为ios后,字体就看不见了,这是为什么呢?这是需要在MAC下找 ...
- Unity3d开发MOBA游戏类《王者荣耀》记录(起)
最近在公司搬砖事情不多,因为最近在玩鹅厂游戏<王者荣耀>,是一款目前市场上最火爆的moba类手游,所以抽空想做一个类似的demo~本文纯灌水记录一下开发计划,客户端打算采用Unity3d, ...
- 【Unity3D开发小游戏】专栏文章导读清单
推荐阅读 CSDN主页 GitHub开源地址 Unity3D插件分享 简书地址 我的个人博客 QQ群:1040082875 一.前言 本专栏,以小游戏的开发为手段,以学习为目的,在学习的道路中,总是要 ...
- Unity3D动作类游戏中两种相机抖动
Unity3D动作类游戏中两种相机抖动 在动作类RPG游戏中,为了体现角色的技能和增加动作打击感,我们常常会给相机添加抖动效果来配合特效做一些技能效果. 我在网上搜了一下,然后结合自己的项目总结一下, ...
- Unity骚操作:Spine动画打包成AssetBundle资源到安卓平台时,材质丢失的问题解决方案
目录 Spine动画打包成AssetBundle资源到安卓平台时,材质丢失. 如果真的特别需要在u3d编辑器中显示为正常,可以手动在编辑器中的右边Inspector面板重新选择一下材质球的shader ...
最新文章
- java基础知识总结,绝对经典
- sql server取某个时间段内所有日期或者所有月份
- JUKI贴片机RX-7R_JUKI贴片机_贴片机
- winform中textbox属性Multiline=true时全选
- 添加中文菜单项出现乱码的解决办法
- nginx限速方法以及报错处理
- STP重新收敛过程和补充内容
- C#- XPath教程
- Gsonformat
- 如何区分1位和4位数码管引脚图
- 3d max 快捷键
- HTML实现手机端适配
- C Minimum Ties
- 博朗x乐高全球限量型趣宝盒首发
- 装机春天时刻,盘点4月高性价比国产SSD
- CSP-J1 CSP-S2第1轮 初赛资料集(2022.09.09)
- Vue小demo—美团注册页面
- hp vse虚拟服务器,HP9000用户升级硬件平台的捷径
- 学习JVM有什么实际意义呢?
- ai自动配音_AI全自动剪辑视频软件4.3版,新增双语字幕条人工配音功能!
热门文章
- C语言代码规范(十)花里胡哨代码鉴赏
- python 示例_带有示例的Python字典popitem()方法
- python的pass语句_适用于pass语句的Python程序
- Java——网络编程三要素
- 编写一程序,输入一个字符串,查找该字符串中是否包含“abc”。
- java canvas画圆圈_java – 在视图上绘制一个圆圈(android)
- UVA 11991——Easy Problem from Rujia Liu?
- 详解list容器(应用+模拟实现)
- dup/dup2函数的用法
- UVa272-TeX中的引号