From 5f05e7aa834f78e3e9ce0994d59b5e4f7e1d7575 Mon Sep 17 00:00:00 2001 From: hch <305670599@qq.com> Date: 星期一, 29 四月 2024 17:13:20 +0800 Subject: [PATCH] 0312 导出界面图片工具 --- Assets/Editor/Tool/ExportPrefabsIMG.cs | 282 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Assets/Editor/Tool/ExportPrefabsIMG.cs.meta | 11 ++ 2 files changed, 293 insertions(+), 0 deletions(-) diff --git a/Assets/Editor/Tool/ExportPrefabsIMG.cs b/Assets/Editor/Tool/ExportPrefabsIMG.cs new file mode 100644 index 0000000..c310bf5 --- /dev/null +++ b/Assets/Editor/Tool/ExportPrefabsIMG.cs @@ -0,0 +1,282 @@ +锘縰sing System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEditor; +using System.IO; +using UnityEngine.UI; +using Snxxz.UI; +using System.Windows.Forms; +using dnlib.DotNet; + + +//瀵煎嚭宸叉墦寮�鐨�(鎴栬�呮寚瀹氬悕)鐣岄潰鐢ㄥ埌鐨勫浘鐗� +public class ExportPrefabsIMG : EditorWindow { + + private static string topFilePath= "Assets/ResourcesOut/UI/Window/"; + private static string builtInFilePath = "Assets/ResourcesOut/BuiltIn/Prefabs/"; + private static string priorityWindowFilePath = "Assets/ResourcesOut/UI/PriorityWindow/"; + + private static ExportPrefabsIMG window = null; + private Vector2 scrollPosition; + private static int exportType = 0; //0 杩愯鏃剁晫闈� 1 builtin鐩綍鐣岄潰 2 window鐩綍鐣岄潰 3 priorityWindow鐩綍鐣岄潰 + + + public class FileToggleInfo + { + public FileToggleInfo(string fileName, bool sel) + { + fileInfo = fileName; + isSelect = sel; + } + + public string fileInfo = null; + public bool isSelect = false; + } + + private static List<FileToggleInfo> _tableNameLst = new List<FileToggleInfo>(); //鎵�鏈夎〃鍚嶅瓧鍒楄〃 + + + [UnityEditor.MenuItem("绛栧垝宸ュ叿/瀵煎嚭鎸囧畾鐣岄潰鐨勫浘鐗�", false)] + static void Init() + { + exportType = 0; + window = GetWindow(typeof(ExportPrefabsIMG), true, "瀵煎嚭鐣岄潰鍥剧墖锛岃繍琛屾椂瀵煎嚭浼氭洿鍑嗙‘") as ExportPrefabsIMG; + window.position = new Rect(UnityEngine.Screen.width / 2, UnityEngine.Screen.height / 2, 300, 700); + window.Show(); + } + + private void OnGUI() + { + GUILayout.BeginVertical(); + + scrollPosition = GUILayout.BeginScrollView(scrollPosition); + ShowTableNames(); + GUILayout.EndScrollView(); + + GUILayout.FlexibleSpace(); + + GUILayout.BeginHorizontal(); + if (GUILayout.Button("鍒锋柊杩愯鏃剁晫闈�")) + { + exportType = 0; + GetDicFiles(); + } + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + if (GUILayout.Button("鍒锋柊builtIn鐣岄潰")) + { + exportType = 1; + GetDicFiles(); + } + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + if (GUILayout.Button("鍒锋柊Window鐣岄潰")) + { + exportType = 2; + GetDicFiles(); + } + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + if (GUILayout.Button("鍒锋柊PriorityWindow鐣岄潰")) + { + exportType = 3; + GetDicFiles(); + } + GUILayout.EndHorizontal(); + GUILayout.BeginHorizontal(); + if (GUILayout.Button("鍏ㄩ��")) + { + SelectAll(); + } + GUILayout.EndHorizontal(); + + GUILayout.BeginHorizontal(); + if (GUILayout.Button("瀵煎嚭鍥剧墖")) + { + Export(); + MessageBox.Show("瀵煎嚭鍥剧墖鎴愬姛锛�(鏌ョ湅鏍圭洰褰曠殑OpenWindowImages)"); + AssetDatabase.Refresh(); + } + GUILayout.EndHorizontal(); + + + GUIUtility.ExitGUI(); + } + + private void ShowTableNames() + { + if (_tableNameLst == null) + { + return; + } + for (int i = 0; i < _tableNameLst.Count; i++) + { + string tableName = _tableNameLst[i].fileInfo; + + _tableNameLst[i].isSelect = GUILayout.Toggle(_tableNameLst[i].isSelect, tableName); + + } + } + + // 0 杩愯鏃剁晫闈� 1 builtin鐩綍鐣岄潰 2 window鐩綍鐣岄潰 + private void GetDicFiles() + { + _tableNameLst.Clear(); + if (exportType == 0) + { + if (!EditorApplication.isPlaying || WindowCenter.Instance.openWinNames.IsNullOrEmpty()) + { + MessageBox.Show("璇峰湪娓告垙杩愯鏃舵墦寮�鐣岄潰"); + return; + } + + foreach (var item in WindowCenter.Instance.openWinNames) + { + _tableNameLst.Add(new FileToggleInfo(item.Key, false)); + } + } + else if (exportType == 1) + { + DirectoryInfo folder = new DirectoryInfo(@builtInFilePath); + if (folder != null) + { + FileInfo[] fileInfoArr = folder.GetFiles("*.prefab"); + if (fileInfoArr == null || fileInfoArr.Length <= 0) + { + return; + } + for (int i = 0, length = fileInfoArr.Length; i < length; i++) + { + _tableNameLst.Add(new FileToggleInfo(fileInfoArr[i].Name.Split('.')[0], false)); + } + } + } + else if (exportType == 2) + { + DirectoryInfo folder = new DirectoryInfo(@topFilePath); + if (folder != null) + { + FileInfo[] fileInfoArr = folder.GetFiles("*.prefab"); + if (fileInfoArr == null || fileInfoArr.Length <= 0) + { + return; + } + for (int i = 0, length = fileInfoArr.Length; i < length; i++) + { + _tableNameLst.Add(new FileToggleInfo(fileInfoArr[i].Name.Split('.')[0], false)); + } + } + } + else if (exportType == 3) + { + DirectoryInfo folder = new DirectoryInfo(@priorityWindowFilePath); + if (folder != null) + { + FileInfo[] fileInfoArr = folder.GetFiles("*.prefab"); + if (fileInfoArr == null || fileInfoArr.Length <= 0) + { + return; + } + for (int i = 0, length = fileInfoArr.Length; i < length; i++) + { + _tableNameLst.Add(new FileToggleInfo(fileInfoArr[i].Name.Split('.')[0], false)); + } + } + } + + } + + private void SelectAll() + { + if (_tableNameLst == null) + { + MessageBox.Show("璇峰厛鍒锋柊鐣岄潰"); + return; + } + for (int i = 0; i < _tableNameLst.Count; i++) + { + if (_tableNameLst[i] == null) + { + continue; + } + if (_tableNameLst[i].fileInfo == null) + { + continue; + } + _tableNameLst[i].isSelect = true; + } + } + + private void Export() + { + //瀵煎嚭鎸囧畾鐣岄潰 鎴栬�� 閫変腑宸叉墦寮�鐨勭晫闈� + if (!Directory.Exists("OpenWindowImages")) + Directory.CreateDirectory("OpenWindowImages"); + + if (_tableNameLst == null) + { + MessageBox.Show("鍏堝埛鏂扮晫闈�"); + return; + } + for (int i = 0; i < _tableNameLst.Count; i++) + { + if (!_tableNameLst[i].isSelect) + { + continue; + } + + string fileName = _tableNameLst[i].fileInfo; + string filePath; + if (exportType == 0) + { + if (WindowCenter.Instance.openWinNames[fileName]) + { + filePath = builtInFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + else + { + filePath = topFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + if (!File.Exists(filePath)) + { + filePath = priorityWindowFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + } + else if (exportType == 1) + { + filePath = builtInFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + else if (exportType == 2) + { + filePath = topFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + else + { + filePath = priorityWindowFilePath + _tableNameLst[i].fileInfo + ".prefab"; + } + + GameObject _prefab = AssetDatabase.LoadAssetAtPath<GameObject>(filePath); + if (_prefab == null) + { + Debug.LogFormat("鏃犳硶鍙栧緱鏂囦欢 {0}", filePath); + continue; + } + Image[] transArr14 = _prefab.GetComponentsInChildren<Image>(true); + foreach (Image item in transArr14) + { + var path = AssetDatabase.GetAssetPath(item.sprite.GetInstanceID()); + if (string.IsNullOrEmpty(path)) continue; + if (!path.Contains("ResourcesOut")) continue; + if (!Directory.Exists("OpenWindowImages/" + fileName)) + Directory.CreateDirectory("OpenWindowImages/" + fileName); + + if (!File.Exists("OpenWindowImages/" + fileName + "/" + item.sprite.name + ".png")) + File.Copy(path, "OpenWindowImages/" + fileName + "/" + item.sprite.name + ".png"); + } + } + + + } + + +} diff --git a/Assets/Editor/Tool/ExportPrefabsIMG.cs.meta b/Assets/Editor/Tool/ExportPrefabsIMG.cs.meta new file mode 100644 index 0000000..54eca40 --- /dev/null +++ b/Assets/Editor/Tool/ExportPrefabsIMG.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c8cbde03156fbed459df0b237626f844 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- Gitblit v1.8.0