Unity AssetBundle打包

来源:互联网 发布:mihoyo米哈游淘宝店铺 编辑:程序博客网 时间:2024/05/22 13:19

Unity AssetBundle打包



using UnityEngine;using UnityEditor;using System.IO;using System.Collections;public class ExportAssetBundle{    [MenuItem("AssetBundle/ExportAllAssetBundlePC")]    static void DoExportAssetBundle()    {        //在测试情况下可能会频繁的打包生成Assetbundle,如果忘记改版本号的话可能会读取之前的缓存,可能就会看不到新的效果,所以先清一下缓存。        Caching.CleanCache();        string pathAssetURL = Application.dataPath + "/StreamingAssets/PC";        if(!Directory.Exists(pathAssetURL))        {           Directory.CreateDirectory(pathAssetURL);        }                Object[] obj=Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);        foreach(Object _obj in obj)        {            Debug.Log("Create AssetBundle Name:"+_obj.name);        }        if (BuildPipeline.BuildAssetBundle(null, obj, pathAssetURL + "/all.assetbundle",BuildAssetBundleOptions.CollectDependencies| BuildAssetBundleOptions.CompleteAssets,BuildTarget.StandaloneWindows))        {            AssetDatabase.Refresh();            Debug.Log("OK!");        }        else        {            Debug.Log("Something Error!");        }    }    [MenuItem("AssetBundle/ExportSeparatedAssetBundlePC")]    static void DoExportSeparatedAssetBundle()     {        Caching.CleanCache();        string pathAssetURL = Application.dataPath + "/StreamingAssets/PC";        if (!Directory.Exists(pathAssetURL))        {            Directory.CreateDirectory(pathAssetURL);        }        Object[] Obj = Selection.GetFiltered(typeof(Object),SelectionMode.DeepAssets);        foreach(Object _obj in Obj)        {            if (BuildPipeline.BuildAssetBundle(_obj, null, pathAssetURL +"/"+ _obj.name + ".assetbundle", BuildAssetBundleOptions.CollectDependencies))            {                Debug.Log("OK!");            }            else            {                Debug.Log(_obj.name+"Error!");            }        }        AssetDatabase.Refresh();    }    [MenuItem("AssetBundle/ExportAllAssetBundleAndroid")]    static void DoExportAssetBundleAndroid()    {        Caching.CleanCache();                string pathAssetURL = Application.dataPath + "/StreamingAssets/Android";        if (!Directory.Exists(pathAssetURL))        {            Directory.CreateDirectory(pathAssetURL);        }        Object[] obj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);        foreach (Object _obj in obj)        {            Debug.Log("Create AssetBundle Name:" + _obj.name);        }        if (BuildPipeline.BuildAssetBundle(null, obj, pathAssetURL + "/all.assetbundle", BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android))        {            AssetDatabase.Refresh();            Debug.Log("OK!");        }        else        {            Debug.Log("Something Error!");        }    }    [MenuItem("AssetBundle/ExportSeparatedAssetBundleAndroid")]    static void DoExportSeparatedAssetBundleAndroid()    {        Caching.CleanCache();        string pathAssetURL = Application.dataPath + "/StreamingAssets/Android";        if (!Directory.Exists(pathAssetURL))        {            Directory.CreateDirectory(pathAssetURL);        }        Object[] Obj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);        foreach (Object _obj in Obj)        {            if (BuildPipeline.BuildAssetBundle(_obj, null, pathAssetURL +"/"+ _obj.name + ".assetbundle", BuildAssetBundleOptions.CollectDependencies,BuildTarget.Android))            {                Debug.Log("OK!");            }            else            {                Debug.Log(_obj.name + "Error!");            }        }        AssetDatabase.Refresh();    }    [MenuItem("AssetBundle/ExportAllAssetBundleIOS")]    static void DoExportAssetBundleIOS()    {        Caching.CleanCache();        string pathAssetURL = Application.dataPath + "/StreamingAssets/IOS";        if (!Directory.Exists(pathAssetURL))        {            Directory.CreateDirectory(pathAssetURL);        }        Object[] obj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);        foreach (Object _obj in obj)        {            Debug.Log("Create AssetBundle Name:" + _obj.name);        }        if (BuildPipeline.BuildAssetBundle(null, obj, pathAssetURL + "/all.assetbundle", BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone))        {            AssetDatabase.Refresh();            Debug.Log("OK!");        }        else        {            Debug.Log("Something Error!");        }    }    [MenuItem("AssetBundle/ExportSeparatedAssetBundleIOS")]    static void DoExportSeparatedAssetBundleIOS()    {        Caching.CleanCache();        string pathAssetURL = Application.dataPath + "/StreamingAssets/IOS";        if (!Directory.Exists(pathAssetURL))        {            Directory.CreateDirectory(pathAssetURL);        }        Object[] Obj = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);        foreach (Object _obj in Obj)        {            if (BuildPipeline.BuildAssetBundle(_obj, null, pathAssetURL + "/" + _obj.name + ".assetbundle", BuildAssetBundleOptions.CollectDependencies, BuildTarget.iPhone))            {                Debug.Log("OK!");            }            else            {                Debug.Log(_obj.name + "Error!");            }        }        //BuildAssetBundleOptions.CollectDependencies(收集依赖关系):包含所有的依赖关系。        //BuildAssetBundleOptions.CompleteAssets(完整资源):强制包含整个资源。        //BuildAssetBundleOptions.DisableWriteTypeTree(禁用写入类型树):在资源包中不包含类型信息。        //BuildAssetBundleOptions.DeterministicAssetBundle(确定资源包):编译资源包使用一个哈希表储存对象ID在资源包中。        //BuildAssetBundleOptions.UncompressedAssetBundle(不压缩)        AssetDatabase.Refresh();    }}


原创粉丝点击