纳金网

标题: Unity5.0中AssetBundle的简单打包和获取 [打印本页]

作者: 烟雨    时间: 2016-3-29 20:56
标题: Unity5.0中AssetBundle的简单打包和获取

1.新建一个工程项目,叫AssetBundleDemo。
2.在工程中创建一个prefab,然后在他的inspector面板下有个AssetBundle选项,一定要写上名字才能打包。
3.新建Editor文件夹创建AssetBundle类。
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEditor;

  4. public class AssetBundle {

  5.     [MenuItem("BuildBudle/Build Asset Bundles")]
  6.     static void BuildAssetBundle()
  7.     {
  8.         BuildPipeline.BuildAssetBundles(Application.dataPath+"/AssetBundles");   //打包资源路径
  9.     }
  10. }
  11. 4.工程中要创建对应的AssetBundle文件夹,与上一步打包路径一定要一致。
  12. 5.这样就可以在菜单栏上选择BuildBundle按钮进行打包了。
  13. 6.打包完之后就可以在项目中进行读取,创建LoadAssetBundle类,绑定到项目中。
  14. using UnityEngine;
  15. using System.Collections;

  16. public class LoadAssetBundle : MonoBehaviour {

  17.     private GameObject cube;
  18.     private string filePath = "file://D:\\Project\\AssetBundleDemo\\Assets\\AssetBundles\\cube";   //资源所在路径

  19.         void Start () {
  20.         StartCoroutine(GetMainObject(filePath));
  21.         }

  22.     IEnumerator GetMainObject(string filePath)
  23.     {
  24.         WWW wwwObject = new WWW(filePath);         //利用www类加载
  25.         Debug.Log(wwwObject.url);
  26.         yield return wwwObject;
  27.         AssetBundle mainBundle = wwwObject.assetBundle;    //获得AssetBundle
  28.         AssetBundleRequest abr = mainBundle.LoadAssetAsync("Cube", typeof(GameObject));    //异步加载GameObject类型
  29.         yield return abr;
  30.         cube = Instantiate(abr.asset) as GameObject;
  31.         yield return null;
  32.         mainBundle.Unload(false);     //卸载所有包含在bundle中的对象。
  33.         wwwObject.Dispose();     //中断www
  34.     }
  35. }
复制代码

作者: 毛毛虫    时间: 2016-3-31 14:30
真心不错啊,谢谢分享!!!!!!
作者: ldqwanldq    时间: 2016-4-18 10:36
Cannot implicitly convert type `UnityEngine.AssetBundle' to `AssetBundle'
报错




欢迎光临 纳金网 (http://go.narkii.com/club/) Powered by Discuz! X2.5