频道栏目
首页 > 程序开发 > 移动开发 > 其他 > 正文
编程开发教程_生成AssetBundle
2017-09-11 09:32:53         来源:SendSI的专栏  
收藏   我要投稿

选中prefabs,生成AssetBundle

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using UObject = UnityEngine.Object;

public class CreateAseetBundle {

    [MenuItem("CreateAssetBundle/AssetBundle_Android")]
    static void ExportRoom_Android()
    {
        CreatAssetBundle(BuildTarget.Android);
    }
    [MenuItem("CreateAssetBundle/AssetBundle_IOS")]
    static void ExportRoom_IOS()
    {
        CreatAssetBundle(BuildTarget.iOS);
    }
    [MenuItem("CreateAssetBundle/AssetBundle_Win")]
    static void ExportRoom_Win()
    {
        CreatAssetBundle(BuildTarget.StandaloneWindows);
    }
    /// 
    /// 创建AssetBundle 包括材质等 -->prefabs
    /// 
    static void CreatAssetBundle(BuildTarget target)
    {
        UObject[] selection = Selection.GetFiltered(typeof(UObject), SelectionMode.DeepAssets);
        foreach (UObject item in selection)
        {
            string prefabPath = AssetDatabase.GetAssetPath(item);
            string[] resourcesName = new string[1];
            resourcesName[0] = prefabPath;

            AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
            buildMap[0].assetBundleName = item.name + "_assetbundle";
            Debug.Log(resourcesName[0]);
            buildMap[0].assetNames = resourcesName;
            string exportPath = GetBundleUrl(target);
            BuildPipeline.BuildAssetBundles(exportPath, buildMap, BuildAssetBundleOptions.None, target);
       //  BuildPipeline.BuildAssetBundles(dirFile, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        }
        AssetDatabase.Refresh();
    }

    static string GetBundleUrl(BuildTarget target)
    {
        if (target == BuildTarget.Android)
            return Application.streamingAssetsPath + "/Android/";
        else if (target == BuildTarget.iOS)
            return Application.streamingAssetsPath + "/IOS/";
        else if (target == BuildTarget.StandaloneWindows)
            return Application.streamingAssetsPath + "/Win/";
        return Application.streamingAssetsPath + "/WinEditor/";
    }
}

加载

    /// 
    /// 初始化加载
    /// 
    IEnumerator GetStartLoadAsset(string assetName)
    {
        WWW www = new WWW("file://" + Application.streamingAssetsPath + @"/Win/" + assetName + "_assetbundle");
        yield return www;
        AssetBundle ab = www.assetBundle;
        GameObject obj = ab.LoadAsset(assetName) as GameObject;
        GameObject genObj = Instantiate(obj);
        genObj.name = assetName + "_child";
        genObj.transform.parent = GameObject.Find(assetName).transform;
        genObj.transform.localPosition = new Vector3(0, 0, 0);
        genObj.transform.localScale = new Vector3(1f, 1f, 1f);
        genObj.transform.localEulerAngles = new Vector3(0, 0, 0);
        www.assetBundle.Unload(false);
    }
点击复制链接 与好友分享!回本站首页
上一篇:Handler机制浅析
下一篇:FactoryKit去除一些功能测试项
相关文章
图文推荐
文章
推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站