查看: 725|回复: 0
打印 上一主题 下一主题

[其他] Unity5.0中AssetBundle的简单打包和获取(转载)

[复制链接]

9903

主题

126

听众

7万

积分

首席设计师

Rank: 8Rank: 8

纳金币
53488
精华
316

最佳新人 热心会员 灌水之王 活跃会员 突出贡献 荣誉管理 论坛元老

跳转到指定楼层
楼主
发表于 2016-4-22 05:21:13 |只看该作者 |倒序浏览

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
    }
}


分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

手机版|纳金网 ( 闽ICP备2021016425号-2/3

GMT+8, 2025-8-17 18:09 , Processed in 0.069237 second(s), 29 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部