一、创建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文件夹没有的话需自己创建)

[csharp] view plaincopy
print?
  1. /// <summary>  
  2. /// 将选中的预制分别打包  
  3. /// </summary>  
  4. [MenuItem("AssetBundleDemo/Create AssetBundles By themselves")]  
  5. static void CreateAssetBundleThemelves(){  
  6.     //获取要打包的对象(在Project视图中)  
  7.     Object[] selects = Selection.GetFiltered (typeof(Object),SelectionMode.DeepAssets);  
  8.     //遍历选中的对象  
  9.     foreach(Object obj in selects){  
  10.         //这里建立一个本地测试  
  11.         //注意本地测试中可以是任意的文件,但是到了移动平台只能读取路径StreamingAssets里面的  
  12.         //StreamingAssets是只读路径,不能写入  
  13.         string targetPath = Application.dataPath + "/AssetBundleLearn/StreamingAssets/" + obj.name + ".assetbundle";//文件的后缀名是assetbundle和unity都可以  
  14.         if(BuildPipeline.BuildAssetBundle(obj,null,targetPath,BuildAssetBundleOptions.CollectDependencies)){  
  15.   
  16.             Debug.Log(obj.name + "is packed successfully!");  
  17.         }else{  
  18.             Debug.Log(obj.name + "is packed failly!");  
  19.         }  
  20.     }  
  21.     //刷新编辑器(不写的话要手动刷新,否则打包的资源不能及时在Project视图内显示)  
  22.     AssetDatabase.Refresh ();  
  23. }  

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.

UnfilteredReturn the whole selection.
TopLevelOnly return the topmost selected transform. A selected child of another selected transform will be filtered out.
DeepReturn the selection and all child transforms of the selection.
ExcludePrefabExcludes any prefabs from the selection.
EditableExcludes any objects which shall not be modified.
AssetsOnly return objects that are assets in the Asset directory.
DeepAssetsIf 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不能混用,不同平台只能用自己的。

三、一起打包

[csharp] view plaincopy
print?
  1. /// <summary>  
  2. /// 将选中的预制打包到一起  
  3. /// </summary>  
  4. [MenuItem("AssetBundleDemo/Create AssetBundles Together")]  
  5. static void CreateAssetBundleTogether(){  
  6.     //要打包的对象  
  7.     Object[] selects = Selection.GetFiltered (typeof(Object),SelectionMode.DeepAssets);  
  8.     //要打包到的路径  
  9.     string targetPath = Application.dataPath + "/AssetBundleLearn/StreamingAssets/Together.assetbundle";  
  10.     if(BuildPipeline.BuildAssetBundle(null,selects,targetPath,BuildAssetBundleOptions.CollectDependencies)){  
  11.         Debug.Log("Packed successfully!");  
  12.   
  13.     }else{  
  14.         Debug.Log("Packed failly!");  
  15.     }  
  16.     //刷新编辑器(不写的话要手动刷新)  
  17.     AssetDatabase.Refresh ();  
  18. }  


四、读取

[csharp] view plaincopy
print?
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. public class ReanAssetbundle : MonoBehaviour {  
  5.   
  6.     //不同平台下StreamingAssets的路径是不同的,这里需要注意一下。  
  7.     public static readonly string m_PathURL =  
  8.         #if UNITY_ANDROID  
  9.         "jar:file://" + Application.dataPath + "!/assets/";  
  10.         #elif UNITY_IPHONE  
  11.         Application.dataPath + "/Raw/";  
  12.         #elif UNITY_STANDALONE_WIN || UNITY_EDITOR  
  13.         "file://" + Application.dataPath + "/AssetBundleLearn/StreamingAssets/";  
  14.         #else  
  15.         string.Empty;  
  16.         #endif  
  17.   
  18.     void OnGUI(){  
  19.         if(GUILayout.Button("加载分开打包的Assetbundle")){  
  20.             StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "One.assetbundle"));  
  21.             StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL +  "Two.assetbundle"));  
  22.             StartCoroutine(LoadGameObjectPackedByThemselves(m_PathURL + "Three.assetbundle"));  
  23.   
  24.         }  
  25.           
  26.         if(GUILayout.Button("加载打包在一起的Assetbundle")){  
  27.             StartCoroutine(LoadGameObjectPackedTogether(m_PathURL + "Together.assetbundle"));  
  28.         }  
  29.           
  30.     }  
  31.     //单独读取资源  
  32.     private IEnumerator LoadGameObjectPackedByThemselves(string path){  
  33.         WWW bundle = new WWW (path);  
  34.         yield return bundle;  
  35.   
  36.         //加载  
  37.         yield return Instantiate (bundle.assetBundle.mainAsset);  
  38.         bundle.assetBundle.Unload (false);  
  39.     }  
  40.   
  41.     IEnumerator  LoadGameObjectPackedTogether (string path)  
  42.     {  
  43.         WWW bundle = new WWW (path);  
  44.         yield return bundle;  
  45.   
  46.         Object one = bundle.assetBundle.Load ("One");  
  47.         Object two = bundle.assetBundle.Load ("Two");  
  48.         Object three = bundle.assetBundle.Load ("Three");  
  49.   
  50.         //加载  
  51.         yield return Instantiate (one);  
  52.         yield return Instantiate (two);  
  53.         yield return Instantiate (three);  
  54.         bundle.assetBundle.Unload (false);  
  55.     }  
  56. }  

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/387480.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【JS】我的JavaScript学习之路(2)

3.从JavaScript页面解析过程看执行顺序 代码(test.html)&#xff1a; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/x…

Codeforces 862D. Mahmoud and Ehab and the binary string 【二分】(交互)

<题目链接> 题目大意&#xff1a; 有一个长度为n(n<1000)的01串&#xff0c;该串中至少有一个0和一个1&#xff0c;现在由你构造出一些01串&#xff0c;进行询问&#xff0c;然后系统会给出你构造的串与原串的 Hamming distance &#xff0c;现在要求你按照步骤进行…

王者荣耀提取攻略

1. 王者荣耀安装后&#xff0c;就将模型等资源解压到SD卡目录里&#xff0c;我们需要找到这个目录。模型资源存储在SD卡中&#xff0c;路径为&#xff1a;【/SDCard/Android/data/com.tencent.tmgp.sgame/files/Resources/AssetBundle/】 2. 2 所有英雄的资源包都在这个目…

2.4 multiset

#include<set> multiset与set的唯一不同&#xff1a;允许插入重复的元素。 在插入元素、删除元素、查找元素上与set 有区别。 multiset元素的插入&#xff1a; multiset<int> ms; ms.insert(11); ms.insert(11); //插入两个11&#xff0c;遍历时同样有两个11。…

Exchange ActiveSyn身份验证类型

http://www.exchangecn.com/html/exchange2010/20110125_316.html 配置 Exchange ActiveSync 身份验证 时间:2011-01-25 11:01来源:Exchange中文站 作者:Exchange中文站 点击:3045次ActiveSync 身份验证是客户端和服务器验证其身份以进行数据传输的过程&#xff0c;本文以示例的…

HotSpot 虚拟机垃圾回收算法实现

作为使用范围最广的虚拟机之一HotSpot&#xff0c;必须对垃圾回收算法的执行效率有严格的考量&#xff0c;只有这样才能保证虚拟机高效运行 枚举根节点 从可达性分析中从 GC Roots 节点找引用链这个操作为例&#xff0c;可以作为 GC Roots 的节点主要在全局性的引用&#xff08…

Java NIO编写Socket服务器的一个例子

最近一直在忙着JAVA NIO的知识&#xff0c;花了一下午的时间&#xff0c;总算写出了一个可以运行的程序&#xff0c;废话少说&#xff0c;上代码&#xff01; Java代码&#xff1a; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerS…

2.5 map

#include<map> key/value对 采用红黑树实现&#xff0c;键值不允许重复 用法与set类似 创建map&#xff1a; map<string, float> m; m["haha"] 11.1; m["hehe"] 22.2; for (map<string, float>::iterator it m.begin(); it ! m.en…

在js传递参数中含加号(+)的处理方式

一般情况下&#xff0c;url中的参数应使用 url 编码规则&#xff0c;即把参数字符串中除了 “ - "、" _ " 、" . "之外的所有非字母数字字符都将被替换成百分号&#xff08;%&#xff09;后跟两位十六进制数&#xff0c;空格则编码为加号&#xff08;…

二 SVN代码冲突的解决

问题&#xff1a; A和B都是最新的代码&#xff0c;A修改了代码提交了&#xff0c;B也修改了代码&#xff0c;但是B提交的时候出现冲突的问题。 解决方案&#xff1a;编辑冲突 解决冲突&#xff1a; 方法一&#xff1a;将文件里面冲突的描述去掉&#xff0c;重新提交 方法二&…

Android7.0反射类找不到的问题

Java中使用反射的地方较多&#xff0c;尤其是各种框架中。最近在Android7.0的项目中遇到个问题很奇怪&#xff0c;反射使用的类找不到了&#xff0c;但是编译的时候没问题啊。然后在代码中使用非反射的方式调用代码也是没有问题的&#xff0c;这时奇怪的现象出现了&#xff0c;…

2.6 multimap

#include<map> multimap的元素插入、删除、查找与map不同 multimap元素的插入&#xff1a;&#xff08;未提供mm[key]value插入方式&#xff09; multimap<string, double> mm; mm.insert(pair<string, double>("haha", 11.1)); mm.insert(pai…

Mybatis学习笔记18 - 缓存

两级缓存&#xff1a; 一级缓存&#xff1a;&#xff08;本地缓存&#xff09;&#xff1a;sqlSession级别的缓存。一级缓存是一直开启的&#xff1b;SqlSession级别的一个Map 数据库同一次会话期间查询到的数据会放在本地缓存中。以后如果需要获取相同的数据&#xff0c;直接从…

2.7 deque

#include<deque> 双端队列容器 注意&#xff1a;头入队时伴随的是尾出队&#xff1b;提供中间元素的更新和删除操作。 与vector一样&#xff0c;采用线性表顺序存储结构 deque采用分块的线性存储结构来存储数据&#xff0c;每块大小一般为512字节 所有deque块由一个…

APK 加壳方法

下载工具http://download.csdn.net/download/sys025/8958363一款免费的为apk加固的工具。 特别说明&#xff1a;加固后需要重新签名apk才能安装。加固的apk包会比未加固的大一些。 jarsigner -verbose -keystore dms.keystore -storepass pactera -keypass pactera -sigfile CE…

Java DSL简介(收集整理)

一、领域特定语言&#xff08;DSL&#xff09; 领域特定语言&#xff08;DSL&#xff09;通常被定义为一种特别针对某类特殊问题的计算机语言&#xff0c;它不打算解决其领域外的问题。对于DSL的正式研究已经持续很多年&#xff0c;直 到最近&#xff0c;在程序员试图采用最易读…

[转]JSon数据解析的四种方式

转至http://blog.csdn.net/enuola/article/details/7903632 作为一种轻量级的数据交换格式&#xff0c;json正在逐步取代xml&#xff0c;成为网络数据的通用格式。 有的json代码格式比较混乱&#xff0c;可以使用此“http://www.bejson.com/”网站来进行JSON格式化校验&#xf…

2.8 list

#include<list> 双向循环链表 list结点的三个域&#xff1a;数据域、前驱元素指针域、后继元素指针域 对于list的迭代器&#xff0c;只有或--的操作&#xff0c;无n或-n的操作 创建list对象&#xff1a; list<int> l; list<int> l(10); 插入和遍历&…

Spring AOP两种实现机制是什么?

Spring AOP两种实现机制是什么&#xff1f; 1.如果是有接口声明的类进行AOP 时&#xff0c;spring调用的是java.lang.reflection.Proxy 类来做处理 2.如果是没有接口声明的类时&#xff0c; spring通过cglib包和内部类来实现 在AOP&#xff0c;权限控制&#xff0c;事务管理等…

iOS开发UI篇—Quartz2D使用(绘图路径)

1 //1.获取图形上下文 2 CGContextRef ctxUIGraphicsGetCurrentContext(); 3 //2.绘图&#xff08;画线&#xff09; 4 //设置起点 5 CGContextMoveToPoint(ctx, 20, 20); 6 //设置终点 7 CGContextAddLineToPoint(ctx, 200, 300); 8 //渲染 9…