分享

AssetBundles用法基础

 kiki的号 2017-08-26


第一次研究AssetBundles。本次讲如何用AssetBundles打包一个资源。又如何加载到场景里。

从最基础的入手:

首先将下列脚本放到Asset里,什么都不用管,放进去就OK

注意: 代码中 BuildPipeline.BuildAssetBundle这个方法在高版本中被废弃了,我用的是5.3.2f1版本还可以用,5.4就不能了。

[csharp] view plain copy
  1. using UnityEngine;  
  2. using System.Collections;  
  3.   
  4. using UnityEditor;  
  5.   
  6. public class ExportAssetBundles  
  7. {  
  8.   
  9.     [MenuItem("Export / Build AssetBundle From Selection - Track dependencies")]  
  10.   
  11.     static void ExportResource()  
  12.     {  
  13.   
  14.         // Bring up save panel  
  15.   
  16.         string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");  
  17.   
  18.         if (path.Length != 0)  
  19.         {  
  20.   
  21.             // Build the resource file from the active selection.  
  22.   
  23.             Object[] selection = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);  
  24.   
  25.             BuildPipeline.BuildAssetBundle(Selection.activeObject, selection, path, BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.CompleteAssets);  
  26.   
  27.             Selection.objects = selection;  
  28.   
  29.         }  
  30.   
  31.     }  
  32.   
  33.     [MenuItem("Export / Build AssetBundle From Selection - No dependency tracking")]  
  34.   
  35.     static void ExportResourceNoTrack()  
  36.     {  
  37.   
  38.         // Bring up save panel  
  39.   
  40.         string path = EditorUtility.SaveFilePanel("Save Resource", "", "New Resource", "unity3d");  
  41.   
  42.         if (path.Length != 0)  
  43.         {  
  44.   
  45.             // Build the resource file from the active selection.  
  46.   
  47.             BuildPipeline.BuildAssetBundle(Selection.activeObject, Selection.objects, path);  
  48.   
  49.         }  
  50.   
  51.     }  
  52.   
  53. }  


当上面代码放入项目里时会看到在菜单里有一个Export的东西出现,里面有两项:第一个是选择打包文件,第二个是打包所有文件。

此文用来讲解选择一个东西打包(一般来说要被打包的东西都是预设prefab)。然后我们新建一个cube在场景里,把他做成预设物体命名为89。

因为window的本地加载都是在StreamingAssets文件里加载的(其他平台不一样),所以我们在Asset下手动新建一个StreamingAssets文件夹。

如下图:



来看一下我们的cube现在的位置:(目的是用来对比一会加载进来的位置,其实位置是不会改变的)



之后就在project里选择89然后进行打包,选择路径(路径很重要,加载时要用,看下图)会弹出这个窗口,然后为89命名为666,之后会被保存在StreaningAssets文件夹里。(下图中由于搜狗输入法截图时输入中文有问题所以只能写英文了)



打包之后刷新一下unity会在打包的文件里看到刚打包的东西:


至此,我们的打包功能已经完成。


..............................................接下来讲解如何加载。..........................

以下方法是windows电脑加载的方法(安卓iOS与PC互相都不一样):

把下列脚本随便绑定到一个物体上就OK(该代码是用来加载物体了)

[csharp] view plain copy
  1. using UnityEngine;  
  2.   
  3.   
  4. using System.Collections;  
  5.   
  6.   
  7. using System.IO;  
  8.   
  9.   
  10. public class LoadUnity3d : MonoBehaviour  
  11.   
  12.   
  13. {  
  14.   
  15.   
  16.     // Use this for initialization  
  17.   
  18.   
  19.     void Start()  
  20.   
  21.   
  22.     {  
  23.   
  24.   
  25.         StartCoroutine(LoadScene());  
  26.   
  27.   
  28.     }  
  29.   
  30.   
  31.     // Update is called once per frame  
  32.   
  33.   
  34.     void Update()  
  35.   
  36.   
  37.     {  
  38.   
  39.   
  40.     }  
  41.   
  42.   
  43.     IEnumerator LoadScene()  
  44.   
  45.   
  46.     {  
  47.         //文件路径,也就是我们打包的那个  
  48.          
  49.         WWW www = new WWW("file:///"+ "C:/Users/Desktop/new/New Unity Project/Assets/" + "/StreamingAssets/666.unity3d");  
  50.   
  51.   
  52.         yield return www;  
  53.   
  54.   
  55.         Instantiate(www.assetBundle.mainAsset);  
  56.   
  57.   
  58.     }  
  59.   
  60.   
  61. }  

注:要更换文件路径只能更换"C:/Users/Desktop/new/New Unity Project/Assets/",在window里本地加载必须在StreamingAssets文件里。


之后我们运行unity看到在场景里加载进了一个cube而且位置和之前是一样的。

本文许多代码并无优化也不严谨,目的是为了让刚接触AssetBundles的小伙伴能够更清晰的看到打包实现过程和加载过程。也就是说老司机请绕行!呵呵哒!

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多