From bc4d6fb744d665870d97dfa8ce7ad2dac19899a7 Mon Sep 17 00:00:00 2001 From: jiaoyangna <3046463818@qq.com> Date: Mon, 5 Jul 2021 16:27:40 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B5=84=E6=BA=90=E5=AF=BC=E5=85=A5=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E4=BF=AE=E6=94=B9=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit e587f6d35d7b4b592e1785c1aa9bc27c190a0e0e) --- .../GameEditor/FrameTool/ImportResWindow.cs | 182 ++++++++++++++++++ .../FrameTool/ImportResWindow.cs.meta | 11 ++ Assets/Scripts/Editor/Util/ProcessUtil.cs | 19 +- 3 files changed, 206 insertions(+), 6 deletions(-) create mode 100644 Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs create mode 100644 Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs.meta diff --git a/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs b/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs new file mode 100644 index 0000000000..f37bfabff3 --- /dev/null +++ b/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs @@ -0,0 +1,182 @@ +using LitJson; +using System; +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using UnityEditor; +using UnityEngine; +using UnityEngine.Networking; + +namespace GameEditor.Util +{ + public class ImportResWindow : EditorWindow + { + string tag = Environment.CurrentDirectory; + string tag1 = "Assets/ManagedResources"; + string importTexturePath; + string targetPath; + string[] usedPath; + bool[] usedPathSelect; + string[] filePath; + [MenuItem("资源导入/资源导入")] + static void InitWindow() + { + // Get existing open window or if none, make a new one: + ImportResWindow window = (ImportResWindow)EditorWindow.GetWindow(typeof(ImportResWindow)); + window.InitData(); + window.Show(); + } + void InitData() + { + importTexturePath = PlayerPrefs.GetString("ImportResWindow_importTexturePath"); + targetPath = PlayerPrefs.GetString("ImportResWindow_targetPath"); + usedPath = new string[] { + "ArtFont_en","ArtFont_vi","Icon/FaBao","Icon/Hero","Icon/Item","Icon/SkillIcon","Icon/Pokemon","Icon/ZuoQi","Icons","Icons/SkillIcon","Icons/SoulPrint" + }; + usedPathSelect = new bool[usedPath.Length]; + for (int i = 0; i < usedPath.Length; i++) + { + usedPathSelect[i] = false; + } + } + private void OnGUI() + { + EditorGUILayout.BeginVertical(); + + EditorGUILayout.BeginHorizontal(); + GUILayout.Label("导入图片"); + importTexturePath = GUILayout.TextArea(importTexturePath); + if (GUILayout.Button("Select", GUILayout.ExpandWidth(false))) + { + filePath = ProcessUtil.OpenFileWin(importTexturePath,"*.*"); + if (filePath != null && filePath.Length > 0) + { + importTexturePath = filePath[0]; + PlayerPrefs.SetString("ImportResWindow_importTexturePath", importTexturePath); + } + } + EditorGUILayout.EndHorizontal(); + + for (int i = 0; i < usedPath.Length; i++) + { + usedPathSelect[i] = EditorGUILayout.Toggle(usedPath[i], usedPathSelect[i]); + } + + EditorGUILayout.BeginHorizontal(); + GUILayout.Label("导入路径"); + GUILayout.Label(tag1); + targetPath = GUILayout.TextArea(targetPath); + if (GUILayout.Button("Brown", GUILayout.ExpandWidth(false))) + { + string path = Path.Combine(tag,tag1) +"/"+ targetPath; + path = EditorUtility.OpenFolderPanel("Resource path", path, "*.*"); + targetPath = path.Replace((Path.Combine(tag, tag1) + "/").Replace("\\","/"), ""); + PlayerPrefs.SetString("ImportResWindow_targetPath", targetPath); + } + if (GUILayout.Button("导入", GUILayout.ExpandWidth(false))) + { + string path = ""; + for (int i = 0; i < usedPathSelect.Length; i++) + { + if (usedPathSelect[i]) + { + path = usedPath[i]; + } + } + if (string.IsNullOrEmpty(path) && !string.IsNullOrEmpty(targetPath)) + { + path = targetPath; + } + ImportTexture(path); + } + EditorGUILayout.EndHorizontal(); + + EditorGUILayout.EndVertical(); + } + void ImportTexture(string path) + { + if (filePath == null || filePath.Length < 2) + { + Debug.LogError("导入文件为空"); + return; + } + string fullPath = Path.Combine(tag, tag1) + "/" + path; + if (!Directory.Exists(fullPath)) + { + Directory.CreateDirectory(fullPath); + AssetDatabase.Refresh(); + } + string desPath = Environment.CurrentDirectory + "/Assets/ImportFloder"; + if (!Directory.Exists(desPath)) + { + Directory.CreateDirectory(desPath); + } + for (int i = 1; i < filePath.Length; i++) + { + string fileName = Path.GetFileName(filePath[i]); + File.Copy(filePath[0] + "/" + filePath[i], desPath + "/" + fileName); + } + AssetDatabase.Refresh(); + + AssetDatabase.StartAssetEditing(); + string[] files = Directory.GetFiles(desPath); + for (int i = 0; i < files.Length; i++) + { + string fileName = Path.GetFileName(files[i]); + string sourceFileName = Path.GetFileNameWithoutExtension(files[i]); + if (sourceFileName.EndsWith("_zh")) + { + sourceFileName = sourceFileName.Substring(0, sourceFileName.Length - 3); + } + string sourceExt = Path.GetExtension(files[i]); + + string[] guids = AssetDatabase.FindAssets(sourceFileName + " t:Texture",new string[] { tag1 }); + if (guids == null || guids.Length < 1) + { + continue; + } + bool isReplace = false; + for (int j = 0; j < guids.Length; j++) + { + string desFilePath = AssetDatabase.GUIDToAssetPath(guids[j]); + string rootPath = Path.GetDirectoryName(desFilePath); + string desfileName = Path.GetFileName(desFilePath); + string desFileNameWithoutExt = Path.GetFileNameWithoutExtension(desFilePath); + if (desFileNameWithoutExt.EndsWith("_zh")) + { + desFileNameWithoutExt = desFileNameWithoutExt.Substring(0, desFileNameWithoutExt.Length - 3); + } + string desExt = Path.GetExtension(desFilePath); + if (sourceFileName == desFileNameWithoutExt) + { + isReplace = true; + if (sourceExt == desExt) + { + AssetDatabase.CopyAsset("Assets/ImportFloder/" + fileName, desFilePath); + } + else + { + AssetDatabase.MoveAssetToTrash(desFilePath); + AssetDatabase.MoveAsset("Assets/ImportFloder/" + fileName, rootPath + "/" + fileName); + } + } + } + if (!isReplace) + { + AssetDatabase.MoveAsset("Assets/ImportFloder/" + fileName, Path.Combine(tag1, path) + "/" + fileName); + } + } + AssetDatabase.StopAssetEditing(); + AssetDatabase.SaveAssets(); + AssetDatabase.Refresh(); + if (Directory.Exists(desPath)) + { + Directory.Delete(desPath,true); + AssetDatabase.Refresh(); + } + Debug.Log("图片导入完成"); + } + } +} diff --git a/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs.meta b/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs.meta new file mode 100644 index 0000000000..f61ae3bb36 --- /dev/null +++ b/Assets/Scripts/Editor/GameEditor/FrameTool/ImportResWindow.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 2d9c1dca6427cd8438ae5f2629ebb5ec +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/Editor/Util/ProcessUtil.cs b/Assets/Scripts/Editor/Util/ProcessUtil.cs index a1367c781e..43da873b71 100644 --- a/Assets/Scripts/Editor/Util/ProcessUtil.cs +++ b/Assets/Scripts/Editor/Util/ProcessUtil.cs @@ -101,8 +101,8 @@ namespace GameEditor.Util } System.Diagnostics.Process.Start("notepad.exe", tPath); //用记事本 } - - public static string[] OpenFileWin() + //返回 数组第一个是文件夹路径 ,以后的是文件名字 + public static string[] OpenFileWin(string path,string ext = "*.*") { //初始化 OpenFileName ofn = new OpenFileName(); @@ -112,11 +112,10 @@ namespace GameEditor.Util ofn.maxFile = ofn.file.Length; ofn.fileTitle = new string(new char[64]); ofn.maxFileTitle = ofn.fileTitle.Length; - string path = Application.streamingAssetsPath; path = path.Replace('/', '\\'); ofn.initialDir = path; //默认路径 ofn.title = "Open Project"; - ofn.defExt = "JPG";//显示文件的类型 + ofn.defExt = ext;//显示文件的类型 //注意 一下项目不一定要全选 但是0x00000008项不要缺少 ofn.flags = 0x00080000 | 0x00001000 | 0x00000800 | 0x00000200 | 0x00000008;//OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST| OFN_ALLOWMULTISELECT|OFN_NOCHANGEDIR string[] strs = null; @@ -125,13 +124,21 @@ namespace GameEditor.Util { //多选文件 string[] Splitstr = { "\0" }; - strs = ofn.file.Split(Splitstr, StringSplitOptions.RemoveEmptyEntries); + string[] strs1 = ofn.file.Split(Splitstr, StringSplitOptions.RemoveEmptyEntries); + if (strs1.Length == 1) + { + strs = new string[] { Path.GetDirectoryName(strs1[0]), Path.GetFileName(strs1[0]) }; + } + else + { + strs = strs1; + } } else { Debug.LogFormat("路径为空"); - } + return strs; } }