资源导入工具修改提交

(cherry picked from commit e587f6d35d7b4b592e1785c1aa9bc27c190a0e0e)
dev_chengFeng
jiaoyangna 2021-07-05 16:27:40 +08:00
parent 632291a19c
commit bc4d6fb744
3 changed files with 206 additions and 6 deletions

View File

@ -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("图片导入完成");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2d9c1dca6427cd8438ae5f2629ebb5ec
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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;
}
}