2023-04-03 11:04:31 +08:00

346 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UEObject = UnityEngine.Object;
using System.Text.RegularExpressions;
namespace BFEditor.Resource
{
public class ResourceCheckWindow : EditorWindow
{
List<BFMainChecker> mainCheckers;
List<GUIContent> mainCheckerGUIContents;
List<BadRes> badList;
GUIStyle titleStyle;
GUIStyle checkerBtnStyle;
GUIStyle operateBtnStyle;
int selectMainCheckerIndex = 0;
Vector2 resultScrollPos;
Vector2 checkerScrollPos;
bool clickCheck;
bool clickCheckAll;
bool clickClear;
bool clickCorrect;
bool clickWhiteList;
bool clickExplain;
public void Init()
{
mainCheckers = ResourceProcessConfig.GetAllCheckers();
mainCheckerGUIContents = new List<GUIContent>();
badList = new List<BadRes>();
foreach (var main in mainCheckers)
{
mainCheckerGUIContents.Add(main.GuiContent);
foreach (var sub in main.SubCheckers)
{
sub.active = false;
}
}
}
void OnGUI()
{
InitGUIStyles();
DrawMainCheckers();
DrawSubCheckers();
DrawOperatePart();
ExecuteClick();
DrawResultPart();
}
void InitGUIStyles()
{
titleStyle = new GUIStyle(GUI.skin.label);
titleStyle.normal.textColor = Color.yellow;
titleStyle.fontSize = 16;
titleStyle.alignment = TextAnchor.UpperLeft;
titleStyle.fontStyle = FontStyle.Italic;
checkerBtnStyle = new GUIStyle(GUI.skin.button);
checkerBtnStyle.fontSize = 12;
checkerBtnStyle.alignment = TextAnchor.MiddleCenter;
operateBtnStyle = new GUIStyle(GUI.skin.button);
operateBtnStyle.fontSize = 15;
operateBtnStyle.alignment = TextAnchor.MiddleCenter;
}
void DrawMainCheckers()
{
GUILayout.Space(15);
GUILayout.Box("", GUI.skin.box, GUILayout.Width(794), GUILayout.Height(3));
GUILayout.BeginHorizontal();
GUILayout.Space(20);
GUILayout.Label("选择", titleStyle, GUILayout.Width(120), GUILayout.Height(20));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(17);
checkerScrollPos = GUILayout.BeginScrollView(checkerScrollPos, false, false, GUILayout.Width(800), GUILayout.Height(35));
selectMainCheckerIndex = GUILayout.Toolbar(selectMainCheckerIndex, mainCheckerGUIContents.ToArray(),
GUILayout.Width(mainCheckerGUIContents.Count * 90), GUILayout.Height(25));
GUILayout.EndScrollView();
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(20);
if (GUILayout.Button("全部选中", checkerBtnStyle, GUILayout.Width(80), GUILayout.Height(18)))
{
var checker = mainCheckers[selectMainCheckerIndex];
foreach (var sub in checker.SubCheckers)
{
sub.active = true;
}
}
GUILayout.Space(5);
if (GUILayout.Button("全部取消", checkerBtnStyle, GUILayout.Width(80), GUILayout.Height(18)))
{
var checker = mainCheckers[selectMainCheckerIndex];
foreach (var sub in checker.SubCheckers)
{
sub.active = false;
}
}
GUILayout.EndHorizontal();
}
void DrawSubCheckers()
{
var toggleStyle = new GUIStyle(GUI.skin.toggle);
toggleStyle.fontSize = 12;
toggleStyle.alignment = TextAnchor.MiddleLeft;
toggleStyle.normal.textColor = Color.white;
toggleStyle.contentOffset = new Vector2(5, -1);
GUILayout.BeginVertical();
GUILayout.Space(5);
var index = selectMainCheckerIndex;
var checker = mainCheckers[index];
var count = checker.SubCheckers.Count;
var column = (count - 1) / 3 + 1; // 总显示列数
for (var i = 0; i < 3; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Space(20);
for (var j = 0; j < column; j++)
{
var subIndex = j * 3 + i;
if (subIndex < count)
{
var subChecker = checker.SubCheckers[subIndex];
subChecker.active = GUILayout.Toggle(subChecker.active, subChecker.Name, toggleStyle, GUILayout.Width(140));
}
}
GUILayout.EndHorizontal();
}
GUILayout.EndVertical();
}
void DrawOperatePart()
{
GUILayout.BeginVertical();
GUILayout.Box("", GUI.skin.box, GUILayout.Width(794), GUILayout.Height(3));
GUILayout.BeginHorizontal();
GUILayout.Space(20);
GUILayout.Label("执行", titleStyle, GUILayout.Width(70), GUILayout.Height(20));
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(20);
clickCheck = GUILayout.Button("检查", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
operateBtnStyle.fontSize = 10;
clickCheckAll = GUILayout.Button("检查所有", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
operateBtnStyle.fontSize = 15;
clickClear = GUILayout.Button("清除", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
operateBtnStyle.fontSize = 10;
clickCorrect = GUILayout.Button("一键修复", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
operateBtnStyle.fontSize = 12;
clickWhiteList = GUILayout.Button("白名单", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
operateBtnStyle.fontSize = 15;
clickExplain = GUILayout.Button("说明", operateBtnStyle, GUILayout.Width(55), GUILayout.Height(26));
GUILayout.Space(5);
if (badList.Count > 0)
{
var style = new GUIStyle(GUI.skin.label);
style.normal.textColor = Color.red;
style.fontSize = 15;
style.alignment = TextAnchor.LowerLeft;
GUILayout.Label("异常资源数量:" + badList.Count, style);
}
GUILayout.EndHorizontal();
GUILayout.Box("", GUI.skin.box, GUILayout.Width(794), GUILayout.Height(3));
GUILayout.EndVertical();
}
void ExecuteClick()
{
if (clickCheck)
{
DoCheck();
}
if (clickCheckAll)
{
CheckAll();
}
if (clickClear)
{
ClearBad();
}
if (clickCorrect)
{
CorrectAll();
}
if (clickWhiteList)
{
ShowWhiteList();
}
if (clickExplain)
{
ShowExplain();
}
}
void DrawResultPart()
{
resultScrollPos = GUILayout.BeginScrollView(resultScrollPos, GUILayout.Width(800), GUILayout.Height(660));
for (int i = badList.Count - 1; i >= 0; i--)
{
var bad = badList[i];
GUILayout.BeginHorizontal();
GUILayout.Space(20);
if (GUILayout.Button("修复", GUILayout.Width(40)))
{
ResourceCheckControll.FixBadRes(bad);
badList.RemoveAt(i);
}
GUILayout.Space(10);
EditorGUILayout.ObjectField("", AssetDatabase.LoadAssetAtPath<UEObject>(bad.assetPath),
typeof(UEObject), true, GUILayout.Width(200));
GUILayout.Space(10);
var style = GUI.skin.textField;
style.wordWrap = true;
style.alignment = TextAnchor.UpperLeft;
GUILayout.TextField(bad.badLog, GUILayout.Height(GetLogHeight(bad.badLog)));
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
GUILayout.Box("", GUI.skin.box, GUILayout.Width(794), GUILayout.Height(3));
}
float GetLogHeight(string log)
{
var regex = new Regex("\n");
return (regex.Matches(log).Count + 1) * 15;
}
void DoCheck()
{
badList = ResourceCheckControll.DoCheck(mainCheckers);
if (badList.Count == 0)
{
EditorUtility.DisplayDialog("提示", "没有异常资源", "ok");
}
}
void CheckAll()
{
foreach (var main in mainCheckers)
{
foreach (var sub in main.SubCheckers)
{
sub.active = true;
}
}
badList = ResourceCheckControll.DoCheck(mainCheckers);
if (badList.Count == 0)
{
EditorUtility.DisplayDialog("提示", "没有异常资源", "ok");
}
}
void ClearBad()
{
if (badList.Count > 0)
{
badList.Clear();
}
else
{
EditorUtility.DisplayDialog("提示", "没有异常资源", "ok");
}
}
void CorrectAll()
{
if (badList.Count > 0)
{
var i = 0;
var totalCount = badList.Count;
foreach (var bad in badList)
{
i++;
EditorUtility.DisplayProgressBar("正在修复资源", bad.assetPath, (float)i / totalCount);
ResourceCheckControll.FixBadRes(bad);
}
badList.Clear();
EditorUtility.ClearProgressBar();
EditorUtility.DisplayDialog("提示", "修复完成", "ok");
}
else
{
EditorUtility.DisplayDialog("提示", "没有异常资源", "ok");
}
}
void ShowWhiteList()
{
if (ResourceProcessConfig.whiteResList.Count > 0)
{
ResourceWhiteWindow.OpenWindow();
}
else
{
EditorUtility.DisplayDialog("提示", "没有白名单,可在ResourceProcessConfig.cs中添加", "ok");
}
}
void ShowExplain()
{
ResourceExplainWindow.OpenWindow();
}
public static void OpenWindow()
{
var window = GetWindowWithRect<ResourceCheckWindow>(new Rect(Screen.width / 2, Screen.height / 2, 830, 1000));
window.ShowUtility();
window.Init();
}
}
}