AssetBundl 大全(二)
大家好,我是北京菜鸟在线的unity3d 讲师 ,范老师。今天接着给大家分享 AssetBundl 关于4.x的。

一:打包: 新创建一个场景,拉入三个模型,放入Hierarchy 里,然后在Assets下创建一个 prefabs文件夹,把 这三个物体拉入prefabs文件下,成为预制体
1-1 : 书写代码,查找 这三个需要打包的预制体。代码如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class assetbundelssss {
[ MenuItem("MyAssetBundle/SimplePackage" )]
public static void SimplePackag()
{ // 鼠标选择获取过滤器,过滤条件是Object的都被覆盖,toplevel只能找到最上级的那个transfrom.
Object[] objs = Selection .GetFiltered(typeof( Object),SelectionMode .TopLevel);
for (int i = 0; i < objs.Length; i++) //打印已经-找到的,需要打包的预制体的名字。
{
Debug.Log(objs[i].name);
}
}
}

1-2:当找到需要打包的物品后,我们需要一 一的对它进行打包,代码如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class assetbundelssss {
[ MenuItem("MyAssetBundle/SanDaBao/SimplePackage" )]
public static void SimplePackag()
{ // 鼠标选择获取过滤器,过滤条件是Object的都被覆盖,toplevel只能找到最上级的那个transfrom.
Object[] objs = Selection .GetFiltered(typeof( Object),SelectionMode .TopLevel);
//for (int i = 0; i < objs.Length; i++)
//{ //打印已经-找到的,需要打包的预制体的名字。
// Debug.Log(objs[i].name);
//}
for (int i = 0; i < objs.Length; i++)
{ //下面参数的含义是:第一个是对每个物品对象进行打包,第二个是数组进行打包,这里我们写NULL,第三个是打包的路径。
BuildPipeline.BuildAssetBundle(objs[i],null ,Application.streamingAssetsPath + "/" + objs[i].name + ".unity3d" ); // 关于这个后缀名,也可以这样:
}
Debug.Log(Application .streamingAssetsPath); // 打印路径
}
}
1-3:我们运行代码,发现在是会报错的,因为在我们的工程里面,根本就没有要打包成功后,放置的文件夹 StreamingAssets, 所以,我们需要在工程里Assets 下创建一个这样名字的文件夹

创建StreamingAssets后,运行程序,按照下列图示进行,然后打开StreamingAssets文件夹,就会发现已经有打包好的东东。如下图所示:

然后,观察打包前后的大小,以及后缀名。至此,打包完成。

2-1 : 在摄像机下创建一个脚本,取名LoadAsset. 运行程序,就可以看到所要下载的物体已经被解包。位置还是原来制作成预制体的位置。 代码如下:

using UnityEngine;
using System.Collections;
public class LodAssetbundle : MonoBehaviour {
void Start () {
StartCoroutine( "LoadAsset");
}
IEnumerator LoadAsset()
{
WWW www = new WWW( "File://" + Application .streamingAssetsPath + "/Car2A.unity3d");
yield return www;
if (www.isDone)
{
Instantiate(www.assetBundle.mainAsset);
}
}
}
----------------------------------------------------------------------------下面是选中好几个打包到本地,然后全部解包,加载 --------------------------------------------------------------------------------------
创建一个新的场景,在像机上写一个解包代码:代码如下;
using UnityEngine;
using System.Collections;
public class LodAssetbundle : MonoBehaviour {
private string [] packageName = { "Car1A", "Car2A", "Car3A" };
void Start () {
StartCoroutine( "LoadAsset");
}
IEnumerator LoadAsset()
{
WWW www;
for (int i = 0; i < packageName.Length; i++)
{
www = new WWW ("File://" + Application.streamingAssetsPath + "/" + packageName[i] + ".unity3d" );
yield return www;
if (www.isDone)
{
Instantiate(www.assetBundle.mainAsset);
}
}
}
}

然后,在服务器上可以找到刚刚丢上去的那个东东。

----------------------------------------------------------------------------下面是上传多个物体到服务器,解包,加载 (其实就是把多个物品拖到服务器文件夹内)----------------------------------------------------------
从服务器上解包多个物体
创建一个新的场景,在像机上添加如下代码:
using UnityEngine;
using System.Collections;
public class LodAssetbundle : MonoBehaviour {
private string [] packageName = { "Car1A", "Car2A", "Car3A" };
// Use this for initialization
void Start () {
StartCoroutine( "LoadAsset");
}
IEnumerator LoadAsset()
{
WWW www;
for (int i = 0; i < packageName.Length; i++)
{
www = new WWW ("http://101.200.76.117/qcdz/" + packageName[i] + ".unity3d" );
yield return www;
if (www.isDone)
{
Instantiate(www.assetBundle.mainAsset);
}
}
}
}
然后运行程序,就可以看到,随时时间的增长,这几个物体就都出现了。
如果需要更新模型,我们只需要把那人服务器上的模型进行更改就好了。不需要更改与编译整个项目。很方便。
** 把下面代码挂在新建场景中的像机上就好。**

*** 给下载的过程中添加一个进度条:进度条如下图设置***

进度条代码如下:

---------------------------------------------------------------上面虽然也能打包,下载,加载,但是这种方法还不是最优化的,还可以把文件做的更小,下面是更高性能的打包代码。---------------------------------------------------------------------
一:书写高性能的打包代码:同样放在Editor文件下,思想是把贴图和模型分别打包。代码如下:
using UnityEngine;
using System.Collections;
using UnityEditor;
public class assetbundelssss {
[ MenuItem("MyAssetBundle/SanDaBao/AdvancePackage" )] //高级打包方式
public static void AdvancePackage()
{ // 手动查找Assets下需要打包的贴图
Object tex = AssetDatabase .LoadMainAssetAtPath("Assets/Textures/cars.tif");
Debug.Log(tex.name); /// 打印找到贴图的名字
BuildPipeline.PushAssetDependencies();
/// 手动对需要打包模型的贴图进行打包。
BuildPipeline.BuildAssetBundle(tex,null ,Application.streamingAssetsPath + "/Tex.unity3d");
// 鼠标选择获取过滤器,过滤条件是Object的都被覆盖,toplevel只能找到最上级的那个transfrom.
Object[] objs = Selection .GetFiltered(typeof( Object), SelectionMode .TopLevel);
for (int i = 0; i < objs.Length; i++)
{
BuildPipeline.PushAssetDependencies();
//下面参数的含义是:第一个是对每个物品对象进行打包,第二个是数组进行打包,这里我们写NULL,第三个是打包的路径。
BuildPipeline.BuildAssetBundle(objs[i], null , Application.streamingAssetsPath + "/" + objs[i].name + ".unity3d" );
BuildPipeline.PopAssetDependencies();
}
BuildPipeline.PopAssetDependencies(); //贴图出去
Debug.Log(Application .streamingAssetsPath); // 打印路径
}
}

打包后显示:

二: 书写需要下载和加载的代码,代码如下:
using UnityEngine;
using System.Collections;
public class LodAssetbundle : MonoBehaviour {
private string [] packageName = { "Car1A", "Car2A", "Car3A" };
// Use this for initialization
void Start () {
StartCoroutine( "LoaAdvancedAsset");
}
IEnumerator LoaAdvancedAsset()
{ //先下载贴图
WWW ww = new WWW( "File://" + Application .streamingAssetsPath + "/Tex.unity3d");
yield return ww;
if (ww.isDone)
{
print(ww.assetBundle);
}
WWW www; // 再下载模型
for (int i = 0; i < packageName.Length; i++)
{
www = new WWW ("File://" + Application.streamingAssetsPath + "/" +packageName[i] + ".unity3d" );
yield return www;
if (www.isDone)
{
Instantiate(www.assetBundle.mainAsset);
}
}
}
}

网友评论