您的位置:首页 > 移动开发 > Unity3D

Unity热更新专题(五)如何打包AssetBundle

2017-03-07 17:07 537 查看


Unity热更新专题(五)如何打包AssetBundle


如何打包AssetBundle?

我看了许多博客和论坛,写得有点乱,看的也有一点迷糊。而且unity4.6和Unity5.1在打包制作AssetBundle时,有很大区别。所以还是看看原API文档吧。

===================================================================================


BuildPipeline.BuildAssetBundles

[csharp] view
plain copy

 print?

public static AssetBundleManifest BuildAssetBundles(string outputPath, BuildAssetBundleOptions assetBundleOptions = BuildAssetBundleOptions.None, BuildTarget targetPlatform = BuildTarget.WebPlayer);  

该方法就是打包制作AssetBundle的方法。

(好长的方法啊······

大概看一下

参数一:输出路径

参数二:打包的选项

参数三:平台

下面具体分析一下。

参数一:路径而已。没什么可以选的。

参数二:



描述的还是挺好理解的。

参数三:

这就更好理解了,就是Unity支持的平台。




描述:

制作在Unity编辑器里指定的所有物体的AssetBundle。

补充一点:(Unity5的新化~

1、我们创建一个cube,命名为Player。

2、把Player搞成Prefab。

我们可以看到选中Prefab时,下面有这个框。



我们点击New,标记为player.AssetBundle。

好了。下面修改代码。

创建脚本ExportAssetBundles.cs,把官方API文档上的代码贴上去。

[csharp] view
plain copy

 print?

// C# Example  

// Builds an asset bundle from the selected objects in the project view.  

// Once compiled go to "Menu" -> "Assets" and select one of the choices  

// to build the Asset Bundle  

  

using UnityEngine;  

using UnityEditor;  

  

public class ExportAssetBundles  

{  

    [MenuItem("Test/Build Asset Bundles")]  

    static void BuildABs() {  

    // Put the bundles in a folder called "ABs" within the  

    // Assets folder.  

    BuildPipeline.BuildAssetBundles("Assets/ABs");  

}  

}  

我们可以看到Unity编辑器发生了变化。



没错。工具栏上出现了我们代码中定义的东东。点击它吧~

当然,如果你的工程中没有

Assets/ABs的话,是会出错的。



创建该目录,再次点击。我们就可以看到我们想看到的了。



加载AssetBundle。很简单。

贴个示例代码吧:

[csharp] view
plain copy

 print?

using System;  

using UnityEngine;  

using System.Collections;  

  

public class Load: MonoBehaviour  

{  

    private string BundleURL = "file:///C:/cube.assetbundle";  

    private string SceneURL = "file:///C:/scene1.unity3d";  

  

    void Start()  

    {  

        //BundleURL = "file//"+Application.dataPath+"/cube.assetbundle";  

        Debug.Log(BundleURL);  

        StartCoroutine(DownloadAssetAndScene());  

    }  

  

    IEnumerator DownloadAssetAndScene()  

    {  

        //下载assetbundle,加载Cube  

        using (WWW asset = new WWW(BundleURL))  

        {  

            yield return asset;  

            AssetBundle bundle = asset.assetBundle;  

            Instantiate(bundle.Load("Cube"));  

            bundle.Unload(false);  

            yield return new WaitForSeconds(5);  

        }  

        //下载场景,加载场景  

        using (WWW scene = new WWW(SceneURL))  

        {  

            yield return scene;  

            AssetBundle bundle = scene.assetBundle;  

            Application.LoadLevel("scene1");  

        }  

         

    }  

}  

===================================================================================

到这里吧。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: