using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.IO;
public class BundleBuilderUI : BundleBuilder
{
//[MenuItem("====Build Tools====/[7]打包 UI 相关")]
public static void ShowWindow()
{
EditorWindow.GetWindow<BundleBuilderUI>(true);
}
/// <summary>滚动条坐标</summary>
private Vector2 scrollPos;
private Vector2 texScrollPos;
private Vector2 filesScrollPos;
/// <summary>
/// UI资源,需要拆除引用的资源文件.
/// </summary>
private static string[] UIAssetFoldersFiles = new string[]
{
"Assets/Content/UI/UIAtlas",//ATLAS 资源.
"Assets/Content/UI/UITexture",//Texture资源.
"Assets/Content/UI/UIFont",//字体资源.美术字体与 ttf.
Constants.ChangeFacePath,
Constants.SpineDynamicPath,
};
/// <summary>
/// UI预设文件夹,按照功能文件夹进行打包.
/// </summary>
private static string[] UIAssetFolders = new string[]
{
"Assets/Content/Prefab/UI"
};
private static List<AssetEntry> allFolders = new List<AssetEntry>();
private static List<AssetEntry> allFoldersFiles = new List<AssetEntry>();
private static List<AssetEntry> allAvgFoldersFiles = new List<AssetEntry>();
static Dictionary<string, int> allDependency = new Dictionary<string, int>();
private static string[] buildFolders;
private static string[] buildFoldersFiles;
private static string[] buildAvgFoldersFiles;
private static void ListAll()
{
allFolders.Clear();
allFoldersFiles.Clear();
foreach (string assetFolder in UIAssetFolders)
{
foreach (string folder in Directory.GetDirectories(assetFolder, "*", SearchOption.AllDirectories))
{
if (folder.Contains("_WasteAsset")) continue;
allFolders.Add(new AssetEntry() { path = folder.Replace('\\', '/') });
}
}
// CollectUIDepency();
foreach (string assetfile in UIAssetFoldersFiles)
{
if (assetfile.Contains("UIAtlas"))
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains(".meta") || file.Contains(".mat") || file.Contains(".png") )
continue;
string pathStr = file.Replace('\\', '/');
if (GetRefrenceCount(pathStr) < 2)
continue;
//Debug.LogError("UIAtlas" + file);
allFoldersFiles.Add(new AssetEntry() { path = pathStr });
}
}
/* else if (assetfile.Contains("AvatarEquip"))
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains("meta")||file.Contains(".mat"))
continue;
string pathStr = file.Replace('\\', '/');
// if (GetRefrenceCount(pathStr) < 2)
// continue;
allAvgFoldersFiles.Add(new AssetEntry() { path = pathStr });
}
}*/
else if (assetfile.Contains("UITexture"))
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains(".meta"))
continue;
string pathStr = file.Replace('\\', '/');
//if (!pathStr.Contains("BgLoading") && !pathStr.Contains("Atk_") && !pathStr.Contains("lszy")
// && !pathStr.Contains("MKTJ") && !pathStr.Contains("AVG_") && !pathStr.Contains("MS_"))
// continue;
if (GetRefrenceCount(pathStr) < 2)
continue;
allFoldersFiles.Add(new AssetEntry() { path = pathStr });
}
}
else if (assetfile.Contains("UIFont"))
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains(".meta")|| file.Contains(".mat") || file.Contains(".png"))
continue;
string pathStr = file.Replace('\\', '/');
if (GetRefrenceCount(pathStr) < 2)
continue;
allFoldersFiles.Add(new AssetEntry() { path = pathStr });
//Debug.LogError("font file: " + file);
}
}
else if (assetfile.Contains(Constants.SpineDynamicPath))
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains(".meta"))
continue;
if (!file.Contains(".prefab") || file.Contains("WasteAsset"))
continue;
allAvgFoldersFiles.Add(new AssetEntry() { path = file.Replace('\\', '/') });
}
}
else
{
foreach (string file in Directory.GetFiles(assetfile, "*", SearchOption.AllDirectories))
{
if (file.Contains(".meta")||file.Contains("WasteAsset"))
continue;
Debug.LogError("OTHER:" + file);
allFoldersFiles.Add(new AssetEntry() { path = file.Replace('\\', '/') });
}
}
}
}
private static void CollectUIDepency()
{
allDependency.Clear();
List<AssetEntry> folders = allFolders.FindAll((entry) => { return entry.selected; });
buildFolders = new string[folders.Count];
for (int i = 0; i < folders.Count; ++i)
{
buildFolders[i] = folders[i].path;
}
for (int i = 0; i < buildFolders.Length; ++i)
{
string[] files = System.Array.FindAll(Directory.GetFiles(buildFolders[i]), (file) =>
{
string ext = Path.GetExtension(file);
if (ext == ".meta" || ext == ".unity")
return false;
return true;
});
for (int j = 0; j < files.Length; j++)
{
string s = files[j].Replace("\\", "/");
string[] deps = AssetDatabase.GetDependencies(s);
for (int h = 0; h < deps.Length; h++)
{
string ext = Path.GetExtension(deps[h]);
if (ext == ".cs" || ext == ".shader")
{
continue;
}
//Debug.LogError(deps[h]);
if (allDependency.ContainsKey(deps[h]))
{
allDependency[deps[h]] = allDependency[deps[h]] + 1;
}
else
{
allDependency[deps[h]] = 1;
}
}
continue;
}
}
}
static int GetRefrenceCount(string file)
{
return 10;
if (allDependency.ContainsKey(file))
return allDependency[file];
else
return 0;
}
private static void GetBuildFilesAndFolders()
{
// 需要生成的文件夹的文件.
List<AssetEntry> foldersfiles = allFoldersFiles.FindAll((entry) => { return entry.selected; });
buildFoldersFiles = new string[foldersfiles.Count];
for (int i = 0; i < foldersfiles.Count; ++i)
{
buildFoldersFiles[i] = foldersfiles[i].path;
}
// 需要生成的文件夹
List<AssetEntry> folders = allFolders.FindAll((entry) => { return entry.selected; });
buildFolders = new string[folders.Count];
for (int i = 0; i < folders.Count; ++i)
{
buildFolders[i] = folders[i].path;
}
List<AssetEntry> avgfoldersfiles = allAvgFoldersFiles.FindAll((entry) => { return entry.selected; });
buildAvgFoldersFiles = new string[avgfoldersfiles.Count];
for (int i = 0; i < avgfoldersfiles.Count; ++i)
{
buildAvgFoldersFiles[i] = avgfoldersfiles[i].path;
}
}
private static Dictionary<string, bool> shaderList = new Dictionary<string, bool>();
private static void Build()
{
// 没有需要生成的内容,退出
if (buildFolders.Length == 0 && buildFoldersFiles.Length == 0 && buildAvgFoldersFiles.Length == 0)
return;
outputPath = FileSystem.PackagePath();
builds.Clear();
for (int i = 0; i < buildFoldersFiles.Length; ++i)
{
//string pathName = buildFoldersFiles[i].Replace("Assets/Content/", "");
//pathName = pathName.Replace("/", "");
string pathName = Path.GetFileName(buildFoldersFiles[i]);
if (buildFoldersFiles[i].Contains("UITexture"))
{
pathName = buildFoldersFiles[i].Replace("Assets/Content/", "");
pathName = pathName.Replace("/", "");
pathName = Path.GetFileNameWithoutExtension(pathName);
}
builds.Add(new AssetBundleBuild()
{
assetBundleName = pathName + ".unity3d",
assetNames = new string[] { buildFoldersFiles[i] }
});
GetDependencies(buildFoldersFiles[i],".shader", ref shaderList);//获取需要单独提取的资源.
}
for (int i = 0; i < buildFolders.Length; ++i)
{
string[] files = System.Array.FindAll(Directory.GetFiles(buildFolders[i]), (file) =>
{
string ext = Path.GetExtension(file);
if (ext == ".meta" || ext == ".unity")
return false;
return true;
});
GetDependencies(files, ".shader", ref shaderList);//获取需要单独提取的资源.
string pathName = buildFolders[i].Replace("Assets/Content/", "");
pathName = pathName.Replace("/", "");
builds.Add(new AssetBundleBuild()
{
assetBundleName = pathName + ".unity3d",
assetNames = files,
}
);
}
if(shaderList.Count > 0)
{
List<string> shaderAssetNames = new List<string> ();
foreach(var sn in shaderList)
{
shaderAssetNames.Add(sn.Key);
}
builds.Add(new AssetBundleBuild()
{
assetBundleName = "ryzuishader.unity3d",
assetNames = shaderAssetNames.ToArray(),
}
);
}
for (int i = 0; i < buildAvgFoldersFiles.Length; ++i)
{
string[] files = System.Array.FindAll(Directory.GetFiles(buildAvgFoldersFiles[i]), (file) =>
{
string ext = Path.GetExtension(file);
if (ext == ".meta" || ext == ".unity")
return false;
return true;
});
//GetDependencies(files, ".shader", ref shaderList);//获取需要单独提取的资源.
string pathName = buildAvgFoldersFiles[i].Replace("Assets/Content/", "");
pathName = pathName.Replace("/", "");
pathName = Path.GetFileNameWithoutExtension(pathName);
builds.Add(new AssetBundleBuild()
{
assetBundleName = pathName + ".unity3d",
assetNames = files,
}
);
}
//if (target != mBuildTarget)
//{
// EditorUserBuildSettings.SwitchActiveBuildTarget(target);
//}
string tempFolder = Application.dataPath + "/../BuildTemp" + "/ryzui" ;
DebugHelper.Log(tempFolder);
//if (Directory.Exists(tempFolder))
// Directory.Delete(tempFolder, true);
//Directory.CreateDirectory(tempFolder);
if (!Directory.Exists(tempFolder))
{
Directory.CreateDirectory(tempFolder);
}
AssetBundleManifest abm = BuildPipeline.BuildAssetBundles(tempFolder, builds.ToArray(),
BuildAssetBundleOptions.DeterministicAssetBundle |
BuildAssetBundleOptions.ChunkBasedCompression ,
target);
if (abm)
{
DebugHelper.Log("<color=green>================ BuildUI Successsssssssssssssssss ================</color>");
//EditorUtility.DisplayDialog("", "Build UI Completed", "OK");
}
else
{
DebugHelper.LogError("BuildUI Faileddddddddddddd");
//EditorUtility.DisplayDialog("", "Build UI Failed", "OK");
}
FileSystem.CreateDirIfNotExist(outputPath);
foreach (string file in Directory.GetFiles(tempFolder))
{
if (Path.GetExtension(file) != ".manifest")
{
string des = Path.Combine(outputPath, Path.GetFileName(file));
if (File.Exists(des))
File.Delete(des);
File.Copy(file, des);
}
}
}
private void OnEnable()
{
target = EditorUserBuildSettings.activeBuildTarget;// BuildTarget.Android;
titleContent = new GUIContent("UI");
minSize = new Vector2(640f, 700f);
maxSize = new Vector2(640f, 1200f);
ListAll();
}
private void OnGUI()
{
//BuildTargetSetting();
#region UIAtlas Font Texture;
GUILayout.Space(20);
// 生成按钮
if (GUILayout.Button("Build UI Assetbundle"))
{
GetBuildFilesAndFolders();
Build();
}
GUILayout.Space(20);
GUILayout.Label("UI Atlas & Font", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Select All"))
{
SelectAll(allFoldersFiles);
}
if (GUILayout.Button("Unselect All"))
{
UnselectAll(allFoldersFiles);
}
GUILayout.EndHorizontal();
texScrollPos = GUILayout.BeginScrollView(texScrollPos, GUILayout.Height(200));
foreach (var file in allFoldersFiles)
{
file.selected = GUILayout.Toggle(file.selected, file.path);
}
GUILayout.EndScrollView();
#endregion UIAtlas Font Texture;
#region AVG;
GUILayout.Space(20);
GUILayout.Label("AVG", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Select All"))
{
SelectAll(allAvgFoldersFiles);
}
if (GUILayout.Button("Unselect All"))
{
UnselectAll(allAvgFoldersFiles);
}
GUILayout.EndHorizontal();
filesScrollPos = GUILayout.BeginScrollView(filesScrollPos, GUILayout.Height(100));
foreach (var file in allAvgFoldersFiles)
{
file.selected = GUILayout.Toggle(file.selected, file.path);
}
GUILayout.EndScrollView();
#endregion AVG;
#region UIPrefabs;
GUILayout.Label("UI Prefabs", EditorStyles.boldLabel);
GUILayout.BeginHorizontal();
if (GUILayout.Button("Select All"))
{
SelectAll(allFolders);
}
if (GUILayout.Button("Unselect All"))
{
UnselectAll(allFolders);
}
GUILayout.EndHorizontal();
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Height(300));
foreach (var folder in allFolders)
{
folder.selected = GUILayout.Toggle(folder.selected, folder.path);
}
GUILayout.EndScrollView();
#endregion UIPrefabs;
}
public static void CommandBuild()
{
ListAll();
GetBuildFilesAndFolders();
Build();
}
}