123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546 |
- using UnityEngine;
- using UnityEngine.UI;
- using UnityEditor;
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- using System.Xml;
- using System.Runtime.InteropServices;
- /// <summary>
- /// 打包Asset Bundle(包括场景和普通资源)
- /// By WJ 2017-12-1 10:35:48
- /// </summary>
- public class AssetBundlesExport
- {
- /// <summary>
- /// 资源包后缀
- /// </summary>
- private static string AssetBundleURE = ".unity3d";
- /// <summary>
- /// 创建安卓资源(自动路径)
- /// </summary>
- [MenuItem("Build/Android平台 CreateAssetBundles", false, 101)]
- private static void CreateAssetBunlde_Android()
- {
- CreateAssetBundle(BuildTarget.Android);
- }
- /// <summary>
- /// 创建安卓场景资源(自动路径)
- /// </summary>
- [MenuItem("Build/Android平台 CreateScenesAssetBunldes", false, 102)]
- private static void CreateScenesAssetBunldes_Android()
- {
- CreateScenesAssetBunldes(BuildTarget.Android);
- }
- private static void CreateAssetBundle(BuildTarget buildTarget)
- {
- // 获取在Project视图中选择的所有游戏对象
- Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- string path = Application.dataPath + "/StreamingAssets/NewRes";
- if (0 != path.Length)
- {
- // 遍历所有的游戏对象
- foreach (Object obj in SelectedAssets)
- {
- // 文件夹不打包
- if (obj.GetType().Name == "DefaultAsset")
- {
- continue;
- }
- string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
- #pragma warning disable
- if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
- BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, buildTarget))
- {
- UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
- }
- else
- {
- UnityEngine.Debug.Log("资源打包失败:" + obj.name);
- }
- #pragma warning restore
- }
- }
- // 刷新编辑器
- AssetDatabase.Refresh();
- }
- // /// <summary>
- // /// 创建Windows资源(自动路径)
- // /// </summary>
- // [MenuItem("Build/Create AssetBunldes_Windows_ChoosePath")]
- // private static void CreateAssetBunlde_Windows_ChoosePath()
- // {
- // CreateAssetBunldes_Choose(BuildTarget.StandaloneWindows);
- // }
- // /// <summary>
- // /// 创建安卓资源(自动路径)
- // /// </summary>
- // [MenuItem("Build/Create AssetBunldes_Android_ChoosePath")]
- // private static void CreateAssetBunlde_Android_ChoosePath()
- // {
- // CreateAssetBunldes_Choose(BuildTarget.Android);
- // }
- // /// <summary>
- // /// 打包安卓资源 (自己选择路径)
- // /// </summary>
- // private static void CreateAssetBunldes_Choose(BuildTarget buildTarget)
- // {
- // string path = EditorUtility.SaveFolderPanel("save bundle", string.Empty, string.Empty);
- // if (0 != path.Length)
- // {
- // // 获取在Project视图中选择的所有游戏对象
- // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- // // 遍历所有的游戏对象
- // foreach (Object obj in SelectedAsset)
- // {
- // // 文件夹不打包
- // if (obj.GetType().Name == "DefaultAsset")
- // {
- // continue;
- // }
- // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
- //#pragma warning disable
- // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
- // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
- // buildTarget))
- // {
- // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
- // }
- // else
- // {
- // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
- // }
- //#pragma warning restore
- // }
- // }
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- // }
- /// <summary>
- /// 创建Windows资源(自动路径)
- /// </summary>
- [MenuItem("Build/Windows平台 CreateAssetBundles", false, 201)]
- private static void CreateAssetBunlde_Windows()
- {
- CreateAssetBundle(BuildTarget.StandaloneWindows);
- }
- /// <summary>
- /// 创建Windows场景资源(自动路径)
- /// </summary>
- [MenuItem("Build/Windows平台 CreateScenesAssetBunldes", false, 202)]
- private static void CreateScenesAssetBunldes_Windows()
- {
- CreateScenesAssetBunldes(BuildTarget.StandaloneWindows);
- }
- /// <summary>
- /// 创建安卓场景资源(自动路径)
- /// </summary>
- private static void CreateScenesAssetBunldes(BuildTarget buildTarget)
- {
- string[] paths = GetScenePathFormSelectionFile();
- string[] names = new string[1];
- for (int i = 0; i < paths.Length; ++i)
- {
- string s = paths[i];
- // 分解出场景名字
- string SceneName = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1);
- //将要生成AssetBundle资源的路径及名称
- string targetPathName = Application.dataPath + "/StreamingAssets/NewRes/" + SceneName.ToLower().Substring(0, SceneName.LastIndexOf('.')) + AssetBundleURE;
- //场景文件路径及名称
- names[0] = paths[i];
- // 新版打包方式
- BuildPipeline.BuildPlayer(names, targetPathName, buildTarget, BuildOptions.BuildAdditionalStreamedScenes);
- UnityEngine.Debug.Log("场景打包成功:" + paths[i] + " 路径:" + targetPathName);
- }
- // 刷新编辑器
- AssetDatabase.Refresh();
- }
- //#if UNITY_IOS
- // /// <summary>
- // /// 打包IOS资源(自动路径 )
- // /// </summary>
- // [MenuItem("Build/Create AssetBunldes IOS")]
- // private static void CreateAssetBunldes_IOS()
- // {
- // // string path = EditorUtility.SaveFolderPanel("save bundle", "", "");
- // string path = Application.dataPath + "/StreamingAssets/NewRes";
- // if (0 != path.Length)
- // {
- // // 获取在Project视图中选择的所有游戏对象
- // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- // // 遍历所有的游戏对象
- // foreach (Object obj in SelectedAsset)
- // {
- // // 文件夹不打包
- // if (obj.GetType().Name == "DefaultAsset")
- // {
- // continue;
- // }
- // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
- //#pragma warning disable
- // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
- // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.iOS))
- // {
- // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
- // }
- // else
- // {
- // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
- // }
- //#pragma warning restore
- // }
- // }
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- // }
- // /// <summary>
- // /// 打包IOS资源 (自己选择路径)
- // /// </summary>
- // [MenuItem("Build/Create AssetBunldes IOS - ChoosePath")]
- // private static void CreateAssetBunldes_IosChoose()
- // {
- // string path = EditorUtility.SaveFolderPanel("save bundle", string.Empty, string.Empty);
- // if (0 != path.Length)
- // {
- // // 获取在Project视图中选择的所有游戏对象
- // Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- // // 遍历所有的游戏对象
- // foreach (Object obj in SelectedAsset)
- // {
- // // 文件夹不打包
- // if (obj.GetType().Name == "DefaultAsset")
- // {
- // continue;
- // }
- // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
- //#pragma warning disable
- // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
- // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression | BuildAssetBundleOptions.DeterministicAssetBundle,
- // BuildTarget.iOS))
- // {
- // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
- // }
- // else
- // {
- // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
- // }
- //#pragma warning restore
- // }
- // }
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- // }
- // /// <summary>
- // /// 创建IOS场景资源(自动路径)
- // /// </summary>
- // [MenuItem("Build/Create ScenesAssetBunldes IOS")]
- // private static void CreateScenesAssetBunldes_IOS()
- // {
- // string[] paths = GetScenePathFormSelectionFile();
- // string[] names = new string[1];
- // for (int i = 0; i < paths.Length; ++i)
- // {
- // string s = paths[i];
- // // 分解出场景名字
- // string SceneName = s.Substring(s.LastIndexOf('/') + 1, s.Length - s.LastIndexOf('/') - 1);
- // //将要生成AssetBundle资源的路径及名称
- // string targetPathName = Application.dataPath + "/StreamingAssets/NewRes/" + SceneName.Substring(0, SceneName.LastIndexOf('.')) + AssetBundleURE;
- // //场景文件路径及名称
- // names[0] = paths[i];
- // ////官方已弃用,但是新版用起来不如旧版方便,此处仍用旧版的场景打包方式
- // //BuildPipeline.BuildStreamedSceneAssetBundle(names, targetPathName, BuildTarget.iOS);
- // // 新版打包方式
- // BuildPipeline.BuildPlayer(names, targetPathName, BuildTarget.iOS, BuildOptions.BuildAdditionalStreamedScenes);
- // UnityEngine.Debug.Log("场景打包成功:" + paths[i] + " 路径:" + targetPathName);
- // }
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- // }
- // /// <summary>
- // /// 分割符,无实际意义
- // /// </summary>
- // [MenuItem("Build/--------------------------")]
- // private static void LineFun()
- // {
- // }
- //#endif
- //#if PLATFORM_STANDALONE_WIN
- // /// <summary>
- // /// 创建Windows资源(自动路径)
- // /// </summary>
- // [MenuItem("Build/Create AssetBunldes Windows")]
- // private static void CreateAssetBunldes_Android()
- // {
- // // 获取在Project视图中选择的所有游戏对象
- // Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- // string path = Application.dataPath + "/StreamingAssets/NewRes";
- // if (0 != path.Length)
- // {
- // // 遍历所有的游戏对象
- // foreach (Object obj in SelectedAssets)
- // {
- // // 文件夹不打包
- // if (obj.GetType().Name == "DefaultAsset")
- // {
- // continue;
- // }
- // string targetPath = path + "/" + obj.name.ToLower() + AssetBundleURE;
- //#pragma warning disable
- // if (BuildPipeline.BuildAssetBundle(obj, null, targetPath,
- // BuildAssetBundleOptions.CollectDependencies | BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.StandaloneWindows))
- // {
- // UnityEngine.Debug.Log("资源打包成功:" + obj.name + " 路径:" + targetPath);
- // }
- // else
- // {
- // UnityEngine.Debug.Log("资源打包失败:" + obj.name);
- // }
- //#pragma warning restore
- // }
- // }
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- // }
- //#endif
- // /// <summary>
- // /// 打开本地沙盒
- // /// </summary>
- // [MenuItem("Build/打开本地沙盒")]
- // static void OpenSandbox()
- // {
- //#if UNITY_EDITOR_WIN
- // // 删除文件夹
- // string resPath = Application.persistentDataPath;
- // resPath = resPath.Replace("/", "\\");
- // //打开文件夹
- // System.Diagnostics.Process.Start("explorer.exe", resPath);
- //#else
- // Debug.Log("IOS 暂未支持此功能!");
- //#endif
- // }
- // /// <summary>
- // /// 清空本地版本信息
- // /// </summary>
- // [MenuItem("Build/清空本地版本信息")]
- // static void ClearLocalVerInfo()
- // {
- // // 删除 本地文件夹
- // string resPath = Application.persistentDataPath + "/NewRes";
- // if (Directory.Exists(resPath))
- // {
- // Directory.Delete(resPath, true);
- // }
- // // 删除 网络文件夹
- // string netPath = Application.persistentDataPath + "/NetRes";
- // if (Directory.Exists(netPath))
- // {
- // Directory.Delete(netPath, true);
- // }
- // PlayerPrefs.DeleteKey("YLSJ_LastUpdateInfo");
- // UnityEngine.Debug.Log("[Version] 本地版本信息已清空!");
- // }
- // /// <summary>
- // /// 创建一个vs用的项目xml
- // /// </summary>
- // [MenuItem("Tools/创建DLL项目用的xml")]
- // private static void CreateDllXml()
- // {
- // string savePath = Application.dataPath + "/../../YLSJShared/TestCreateDllXml/TestCreateDllXml";
- // XmlDocument XmlDoc = new XmlDocument();
- // XmlDoc.Load(savePath + "/Core.csproj");
- // XmlNodeList xnl = XmlDoc.ChildNodes[0].ChildNodes;
- // if (XmlDoc.ChildNodes[0].Name.ToLower() != "project")
- // xnl = XmlDoc.ChildNodes[1].ChildNodes;
- // XmlNode XmlRoot = xnl.Item(4);
- // DirectoryInfo diretoryInfo = new DirectoryInfo(Application.dataPath + "/Scripts/Core");
- // DirectoryInfo[] dirInfo = diretoryInfo.GetDirectories();
- // XmlRoot.RemoveAll();
- // //遍历文件夹
- // FileInfo[] _fileInfo = diretoryInfo.GetFiles("*.cs", SearchOption.AllDirectories);
- // foreach (FileInfo NextFile in _fileInfo) //遍历文件
- // {
- // if (NextFile.DirectoryName.Contains("Editor"))
- // {
- // continue;
- // }
- // XmlElement xmlCompile = XmlDoc.CreateElement("Compile", "http://schemas.microsoft.com/developer/msbuild/2003");
- // XmlRoot.AppendChild(xmlCompile);
- // xmlCompile.SetAttribute("Include", "Core" + NextFile.DirectoryName.Replace(diretoryInfo.FullName, "") + "\\" + NextFile.Name);
- // }
- // XmlDoc.Save(savePath + "/Core.csproj");
- // Debug.Log("已经完成");
- // }
- // /// <summary>
- // /// 创建一个vs用的项目xml
- // /// </summary>
- // [MenuItem("Tools/创建DLL项目用的xml(WJ)")]
- // private static void CreateDllXmlWJ()
- // {
- // string savePath = "E:/YLSJShared/TestCreateDllXml/TestCreateDllXml";
- // XmlDocument XmlDoc = new XmlDocument();
- // XmlDoc.Load(savePath + "/Core.csproj");
- // XmlNodeList xnl = XmlDoc.ChildNodes[0].ChildNodes;
- // if (XmlDoc.ChildNodes[0].Name.ToLower() != "project")
- // xnl = XmlDoc.ChildNodes[1].ChildNodes;
- // XmlNode XmlRoot = xnl.Item(4);
- // DirectoryInfo diretoryInfo = new DirectoryInfo(Application.dataPath + "/Scripts/Core");
- // DirectoryInfo[] dirInfo = diretoryInfo.GetDirectories();
- // XmlRoot.RemoveAll();
- // //遍历文件夹
- // FileInfo[] _fileInfo = diretoryInfo.GetFiles("*.cs", SearchOption.AllDirectories);
- // foreach (FileInfo NextFile in _fileInfo) //遍历文件
- // {
- // if (NextFile.DirectoryName.Contains("Editor"))
- // {
- // continue;
- // }
- // XmlElement xmlCompile = XmlDoc.CreateElement("Compile", "http://schemas.microsoft.com/developer/msbuild/2003");
- // XmlRoot.AppendChild(xmlCompile);
- // xmlCompile.SetAttribute("Include", "Core" + NextFile.DirectoryName.Replace(diretoryInfo.FullName, "") + "\\" + NextFile.Name);
- // }
- // XmlDoc.Save(savePath + "/Core.csproj");
- // Debug.Log("已经完成");
- // }
- ///// <summary>
- ///// 创建安卓资源(自动路径 新版)
- ///// </summary>
- //[MenuItem("Build/Create AssetBunldes AndroidNew")]
- //private static void CreateAssetBunldes_AndroidNew()
- //{
- // //// 获取在Project视图中选择的所有游戏对象
- // //Object[] SelectedAssets = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
- // //foreach (Object selected in SelectedAssets)
- // //{
- // // //返回所有对象相对于工程目录的存储路径如Assets/_Scenes/Main.unity
- // // string path = AssetDatabase.GetAssetPath(selected);
- // // //把一个目录的对象检索为AssetImporter
- // // AssetImporter asset = AssetImporter.GetAtPath(path);
- // // asset.assetBundleName = selected.name; //设置Bundle文件的名称
- // // asset.assetBundleVariant = "unity3d";//设置Bundle文件的扩展名
- // // asset.SaveAndReimport();
- // //}
- // //AssetDatabase.Refresh();
- // //如果目录不存在,就创建一个目录
- // string targetPath = Application.dataPath + "/StreamingAssets/NewRes";
- // if (!Directory.Exists(targetPath))
- // {
- // Directory.CreateDirectory(targetPath);
- // }
- // BuildPipeline.BuildAssetBundles(targetPath, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
- // // 刷新编辑器
- // AssetDatabase.Refresh();
- //}
- /////// <summary>
- /////// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
- /////// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
- /////// </summary>
- ////private static void ClearAssetBundlesName()
- ////{
- //// int length = AssetDatabase.GetAllAssetBundleNames().Length;
- //// Debug.Log(length);
- //// string[] oldAssetBundleNames = new string[length];
- //// for (int i = 0; i < length; i++)
- //// {
- //// oldAssetBundleNames[i] = AssetDatabase.GetAllAssetBundleNames()[i];
- //// }
- //// for (int j = 0; j < oldAssetBundleNames.Length; j++)
- //// {
- //// AssetDatabase.RemoveAssetBundleName(oldAssetBundleNames[j], true);
- //// }
- //// length = AssetDatabase.GetAllAssetBundleNames().Length;
- ////}
- /// <summary>
- /// 得到鼠标选中的场景文件,只返回工程内的文件名及后缀
- /// </summary>
- /// <returns>所有选中的场景文件名</returns>
- private static string[] GetScenePathFormSelectionFile()
- {
- UnityEngine.Object[] ScenePaths = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
- List<string> Scenes = new List<string>();
- for (int i = 0; i < ScenePaths.Length; ++i)
- {
- // 排除文件夹
- if (ScenePaths[i].GetType().Name == "SceneAsset")
- {
- string s = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(ScenePaths[i]);
- //Debug.Log("完整路径:" + s);
- string ss = s.Substring(s.LastIndexOf("Assets/"), s.Length - s.LastIndexOf("Assets/"));
- Scenes.Add(ss);
- //Debug.Log("使用路径:" + ss);
- }
- }
- return Scenes.ToArray();
- }
- }
|