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;
///
/// 打包Asset Bundle(包括场景和普通资源)
/// By WJ 2017-12-1 10:35:48
///
public class AssetBundlesExport
{
///
/// 资源包后缀
///
private static string AssetBundleURE = ".unity3d";
///
/// 创建安卓资源(自动路径)
///
[MenuItem("Build/Android平台 CreateAssetBundles", false, 101)]
private static void CreateAssetBunlde_Android()
{
CreateAssetBundle(BuildTarget.Android);
}
///
/// 创建安卓场景资源(自动路径)
///
[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();
}
// ///
// /// 创建Windows资源(自动路径)
// ///
// [MenuItem("Build/Create AssetBunldes_Windows_ChoosePath")]
// private static void CreateAssetBunlde_Windows_ChoosePath()
// {
// CreateAssetBunldes_Choose(BuildTarget.StandaloneWindows);
// }
// ///
// /// 创建安卓资源(自动路径)
// ///
// [MenuItem("Build/Create AssetBunldes_Android_ChoosePath")]
// private static void CreateAssetBunlde_Android_ChoosePath()
// {
// CreateAssetBunldes_Choose(BuildTarget.Android);
// }
// ///
// /// 打包安卓资源 (自己选择路径)
// ///
// 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();
// }
///
/// 创建Windows资源(自动路径)
///
[MenuItem("Build/Windows平台 CreateAssetBundles", false, 201)]
private static void CreateAssetBunlde_Windows()
{
CreateAssetBundle(BuildTarget.StandaloneWindows);
}
///
/// 创建Windows场景资源(自动路径)
///
[MenuItem("Build/Windows平台 CreateScenesAssetBunldes", false, 202)]
private static void CreateScenesAssetBunldes_Windows()
{
CreateScenesAssetBunldes(BuildTarget.StandaloneWindows);
}
///
/// 创建安卓场景资源(自动路径)
///
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
// ///
// /// 打包IOS资源(自动路径 )
// ///
// [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();
// }
// ///
// /// 打包IOS资源 (自己选择路径)
// ///
// [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();
// }
// ///
// /// 创建IOS场景资源(自动路径)
// ///
// [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();
// }
// ///
// /// 分割符,无实际意义
// ///
// [MenuItem("Build/--------------------------")]
// private static void LineFun()
// {
// }
//#endif
//#if PLATFORM_STANDALONE_WIN
// ///
// /// 创建Windows资源(自动路径)
// ///
// [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
// ///
// /// 打开本地沙盒
// ///
// [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
// }
// ///
// /// 清空本地版本信息
// ///
// [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] 本地版本信息已清空!");
// }
// ///
// /// 创建一个vs用的项目xml
// ///
// [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("已经完成");
// }
// ///
// /// 创建一个vs用的项目xml
// ///
// [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("已经完成");
// }
/////
///// 创建安卓资源(自动路径 新版)
/////
//[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();
//}
///////
/////// 清除之前设置过的AssetBundleName,避免产生不必要的资源也打包
/////// 之前说过,只要设置了AssetBundleName的,都会进行打包,不论在什么目录下
///////
////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;
////}
///
/// 得到鼠标选中的场景文件,只返回工程内的文件名及后缀
///
/// 所有选中的场景文件名
private static string[] GetScenePathFormSelectionFile()
{
UnityEngine.Object[] ScenePaths = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.DeepAssets);
List Scenes = new List();
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();
}
}