纳金网
标题:
Unity5.0中AssetBundle的简单打包和获取
[打印本页]
作者:
烟雨
时间:
2016-3-29 20:56
标题:
Unity5.0中AssetBundle的简单打包和获取
1.新建一个工程项目,叫AssetBundleDemo。
2.在工程中创建一个prefab,然后在他的inspector面板下有个AssetBundle选项,一定要写上名字才能打包。
3.新建Editor文件夹创建AssetBundle类。
using UnityEngine;
using System.Collections;
using UnityEditor;
public class AssetBundle {
[MenuItem("BuildBudle/Build Asset Bundles")]
static void BuildAssetBundle()
{
BuildPipeline.BuildAssetBundles(Application.dataPath+"/AssetBundles"); //打包资源路径
}
}
4.工程中要创建对应的AssetBundle文件夹,与上一步打包路径一定要一致。
5.这样就可以在菜单栏上选择BuildBundle按钮进行打包了。
6.打包完之后就可以在项目中进行读取,创建LoadAssetBundle类,绑定到项目中。
using UnityEngine;
using System.Collections;
public class LoadAssetBundle : MonoBehaviour {
private GameObject cube;
private string filePath = "file://D:\\Project\\AssetBundleDemo\\Assets\\AssetBundles\\cube"; //资源所在路径
void Start () {
StartCoroutine(GetMainObject(filePath));
}
IEnumerator GetMainObject(string filePath)
{
WWW wwwObject = new WWW(filePath); //利用www类加载
Debug.Log(wwwObject.url);
yield return wwwObject;
AssetBundle mainBundle = wwwObject.assetBundle; //获得AssetBundle
AssetBundleRequest abr = mainBundle.LoadAssetAsync("Cube", typeof(GameObject)); //异步加载GameObject类型
yield return abr;
cube = Instantiate(abr.asset) as GameObject;
yield return null;
mainBundle.Unload(false); //卸载所有包含在bundle中的对象。
wwwObject.Dispose(); //中断www
}
}
复制代码
作者:
毛毛虫
时间:
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