unity Editor 下Assetbundle 打包

来源:互联网 发布:淘宝服务器 编辑:程序博客网 时间:2024/06/13 23:34

using UnityEngine;
using System.Collections;

using UnityEditor;
using System.IO;

public class CreateAssetBundle : MonoBehaviour
{
[MenuItem(“Custom/Create Object to It’s Single Bundle”)]
static void ExecuteSingleObjectCreateAssetBundle()
{
//清除缓存
Caching.CleanCache();
//获取Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
//生成AssetBundle保存目录
if (!Directory.Exists(Application.dataPath + “/StreamingAssets”))
{
Directory.CreateDirectory(Application.dataPath + “/StreamingAssets”);
}
//遍历所有游戏对象
foreach(Object _object in SelectedAsset)
{
string sourcePath = Application.dataPath + “/StreamingAssets/” + _object.name + “.assetbundle”;

        #if UNITY_ANDROID        if(BuildPipeline.BuildAssetBundle(_object, null, sourcePath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))        {            Debug.Log(_object.name + "Android端资源打包成功。");        }        else        {            Debug.Log(_object.name + "Android端资源打包失败。");        }        #elif UNITY_IPHONE        if(BuildPipeline.BuildAssetBundle(_object, null, sourcePath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS))        {            Debug.Log(_object.name + "iPhone端资源打包成功。");        }        else        {            Debug.Log(_object.name + "iPhone端资源打包失败。");        }        #elif UNITY_STANDALONE_WIN || UNITY_EDITOR        //原先存在则删除掉旧有的文件        if (File.Exists(sourcePath))        {            File.Delete(sourcePath);        }        if (BuildPipeline.BuildAssetBundle(_object, null, sourcePath, BuildAssetBundleOptions.CollectDependencies))        {            Debug.Log("<color=green>" + _object.name + "</color>" + "在PC端资源打包成功。");            //刷新Project视图            AssetDatabase.Refresh();        }        else        {            Debug.Log(_object.name + "在PC端资源打包失败。");        }

else

endif

    }}[MenuItem("Custom/Create All Objects In A Bundle")]static void CreateAllObjectsAssetBundle(){    //清除缓存    Caching.CleanCache();    //生成AssetBundle保存目录    if (!Directory.Exists(Application.dataPath + "/StreamingAssets"))    {        Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");    }    string sourcePath = Application.dataPath + "/StreamingAssets/AllObjects.assetbundle";    Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);    foreach (Object _object in SelectedAsset)    {        Debug.Log("Create AssetBundes name:" + _object.name);

if UNITY_ANDROID

        if (BuildPipeline.BuildAssetBundle(_object, null, sourcePath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.Android))        {            Debug.Log("Android端资源打包成功。");        }        else        {            Debug.Log("Android端资源打包失败。");        }

elif UNITY_IPHONE

    if(BuildPipeline.BuildAssetBundle(_object, null, sourcePath, BuildAssetBundleOptions.CollectDependencies, BuildTarget.iOS))    {        Debug.Log("iPhone端资源打包成功。");    }    else    {        Debug.Log("iPhone端资源打包失败。");    }

elif UNITY_STANDALONE_WIN || UNITY_EDITOR

    //原先存在则删除掉旧有的文件    if(File.Exists(sourcePath))    {        File.Delete(sourcePath);    }    if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, sourcePath, BuildAssetBundleOptions.CollectDependencies))    {        Debug.Log("PC端资源打包成功。");        //刷新Project视图        AssetDatabase.Refresh();    }    else    {        Debug.Log("PC端资源打包失败。");    }

else

endif

    }}[MenuItem("Custom/Create Scene AssetBundle")]static void CreateSceneAssetBundle(){    //清空缓存    Caching.CleanCache();    //生成AssetBundle保存目录    if (!Directory.Exists(Application.dataPath + "/StreamingAssets"))    {        Directory.CreateDirectory(Application.dataPath + "/StreamingAssets");    }    string sourcePath = Application.dataPath + "/StreamingAssets/AssetBundleScene.unity3d";    if (File.Exists(sourcePath))    {        File.Delete(sourcePath);    }    string[] levels = { "Assets/Scenes/Design.unity", "Assets/Scenes/Design_2.unity" };    //打包场景    BuildPipeline.BuildPlayer(levels, sourcePath, BuildTarget.StandaloneWindows64, BuildOptions.BuildAdditionalStreamedScenes);    //刷新视图    AssetDatabase.Refresh();}

}

原创粉丝点击