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 targetPaths = new Dictionary(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 oriPaths = new Dictionary(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 notChangeUIs = new List(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(iter.Current.Value); var oriGo = AssetDatabase.LoadAssetAtPath(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(); var oriScripts = ori.GetComponents(); 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; } } }