Main/Assets/Editor/DIY/Custom/CopyUILabelEditor.cs
2025-01-25 04:38:09 +08:00

199 lines
7.4 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Thousandto.Editor.Excel;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Thousandto.DIY
{
class CopyUILabelEditor : EditorWindow
{
[MenuItem("Ares/CopyUILabelEditor")]
static void Open()
{
if (Application.isPlaying)
{
return;
}
var win = (CopyUILabelEditor)EditorWindow.GetWindow(typeof(CopyUILabelEditor));
win.Show();
}
private string _targetPath = string.Empty;
private string _originPath = string.Empty;
private string _writePath = string.Empty;
void OnGUI()
{
EditorGUILayout.BeginVertical();
EditorGUILayout.LabelField("选择路径");
if (GUILayout.Button("浏览", GUILayout.Width(50)))
{
_targetPath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _targetPath, "");
}
if (string.IsNullOrEmpty(_targetPath))
{
EditorGUILayout.LabelField("null...");
}
else
{
EditorGUILayout.LabelField(_targetPath);
}
if (GUILayout.Button("浏览", GUILayout.Width(50)))
{
_originPath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _originPath, "");
}
if (string.IsNullOrEmpty(_originPath))
{
EditorGUILayout.LabelField("null...");
}
else
{
EditorGUILayout.LabelField(_originPath);
}
if (GUILayout.Button("浏览", GUILayout.Width(50)))
{
_writePath = EditorUtility.OpenFolderPanel("Change Layer Prefab Path", _writePath, "");
}
if (string.IsNullOrEmpty(_writePath))
{
EditorGUILayout.LabelField("null...");
}
else
{
EditorGUILayout.LabelField(_writePath);
}
if (GUILayout.Button("转换", GUILayout.Width(50)))
{
if (string.IsNullOrEmpty(_targetPath) || !Directory.Exists(_targetPath))
{
UnityEngine.Debug.LogError("请输入正确的目标路径!");
return;
}
if (string.IsNullOrEmpty(_originPath) || !Directory.Exists(_originPath))
{
UnityEngine.Debug.LogError("请输入正确的源路径!");
return;
}
if (string.IsNullOrEmpty(_writePath) || !Directory.Exists(_writePath))
{
UnityEngine.Debug.LogError("请输入正确的写出路径!");
return;
}
DirectoryInfo direction = new DirectoryInfo(_targetPath);
FileInfo[] files = direction.GetFiles("*.prefab", SearchOption.AllDirectories);
Dictionary<string, string> targetPaths = new Dictionary<string, string>(files.Length);
for (int i = 0; i < files.Length; ++i)
{
var name = files[i].Name;
var path = files[i].FullName.Replace('\\', '/');
path = "Assets" + path.Replace(Application.dataPath, "");
targetPaths.Add(name, path);
}
DirectoryInfo directionOri = new DirectoryInfo(_originPath);
FileInfo[] filesOri = directionOri.GetFiles("*.prefab", SearchOption.AllDirectories);
Dictionary<string, string> oriPaths = new Dictionary<string, string>(filesOri.Length);
for (int i = 0; i < filesOri.Length; ++i)
{
var name = filesOri[i].Name;
var path = filesOri[i].FullName.Replace('\\', '/');
path = "Assets" + path.Replace(Application.dataPath, "");
oriPaths.Add(name, path);
}
List<string> notChangeUIs = new List<string>(200);
var iter = targetPaths.GetEnumerator();
try
{
int index = 0;
while (iter.MoveNext())
{
EditorUtility.DisplayProgressBar("当前进度", string.Format("{0}/{1}", index, targetPaths.Count), (float)index / targetPaths.Count);
++index;
string oriFile = string.Empty;
if (!oriPaths.TryGetValue(iter.Current.Key, out oriFile))
{
notChangeUIs.Add(iter.Current.Key);
continue;
}
var targetGo = AssetDatabase.LoadAssetAtPath<GameObject>(iter.Current.Value);
var oriGo = AssetDatabase.LoadAssetAtPath<GameObject>(oriFile);
if (CompareUIGameObject(targetGo, oriGo))
{
var targetPath = Application.dataPath.Replace("Assets", "") + iter.Current.Value;
var oriPath = Application.dataPath.Replace("Assets", "") + oriFile;
File.Copy(oriPath, targetPath, true);
}
else
{
notChangeUIs.Add(iter.Current.Key);
}
}
EditorUtility.ClearProgressBar();
AssetDatabase.Refresh(ImportAssetOptions.ForceUpdate);
//没有修改的UI写出到文件
NormalExcelInfo excelInfo = new NormalExcelInfo();
excelInfo.Name = "UIList";
excelInfo.Values = new string[notChangeUIs.Count, 1];
for (int i = 0; i < notChangeUIs.Count; ++i)
{
excelInfo.Values[i, 0] = notChangeUIs[i];
}
ExcelWriter.Write(_writePath + "/NotCopyedUIList.xlsx", excelInfo);
}
finally
{
iter.Dispose();
}
}
EditorGUILayout.EndVertical();
}
//true表示UI已经修改需要保持false表示没有做修改
bool CompareUIGameObject(GameObject target, GameObject ori)
{
return CompareTree(target, ori);
}
//对比树状结构
bool CompareTree(GameObject target, GameObject ori)
{
if (target.name != ori.name)
{
return false;
}
//对比脚本
var targetScripts = target.GetComponents<MonoBehaviour>();
var oriScripts = ori.GetComponents<MonoBehaviour>();
if (targetScripts.Length != oriScripts.Length)
{
return false;
}
//对比子节点
if (target.transform.childCount != ori.transform.childCount)
{
return false;
}
for (int i = 0; i < target.transform.childCount; ++i)
{
if (!CompareTree(target.transform.GetChild(i).gameObject, ori.transform.GetChild(i).gameObject))
{
return false;
}
}
return true;
}
}
}