123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575 |
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Reflection;
- using System.Linq;
- using System.IO;
- using System.Text;
- using UnityEngine;
- using UnityEditor;
- using UnityEditor.SceneManagement;
- namespace YooAsset.Editor
- {
-
-
-
- public static class EditorTools
- {
- static EditorTools()
- {
- InitAssembly();
- }
- #region Assembly
- #if UNITY_2019_4_OR_NEWER
- private static void InitAssembly()
- {
- }
-
-
-
- public static List<Type> GetAssignableTypes(System.Type parentType)
- {
- TypeCache.TypeCollection collection = TypeCache.GetTypesDerivedFrom(parentType);
- return collection.ToList();
- }
- #else
- private static readonly List<Type> _cacheTypes = new List<Type>(10000);
- private static void InitAssembly()
- {
- Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
- foreach (Assembly assembly in assemblies)
- {
- List<Type> types = assembly.GetTypes().ToList();
- _cacheTypes.AddRange(types);
- }
- }
-
-
-
- public static List<Type> GetAssignableTypes(System.Type parentType)
- {
- List<Type> result = new List<Type>();
- for (int i = 0; i < _cacheTypes.Count; i++)
- {
- Type type = _cacheTypes[i];
- if (parentType.IsAssignableFrom(type))
- {
- if (type.Name == parentType.Name)
- continue;
- result.Add(type);
- }
- }
- return result;
- }
- #endif
-
-
-
-
-
-
- public static object InvokeNonPublicStaticMethod(System.Type type, string method, params object[] parameters)
- {
- var methodInfo = type.GetMethod(method, BindingFlags.NonPublic | BindingFlags.Static);
- if (methodInfo == null)
- {
- UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}");
- return null;
- }
- return methodInfo.Invoke(null, parameters);
- }
-
-
-
-
-
-
- public static object InvokePublicStaticMethod(System.Type type, string method, params object[] parameters)
- {
- var methodInfo = type.GetMethod(method, BindingFlags.Public | BindingFlags.Static);
- if (methodInfo == null)
- {
- UnityEngine.Debug.LogError($"{type.FullName} not found method : {method}");
- return null;
- }
- return methodInfo.Invoke(null, parameters);
- }
- #endregion
- #region EditorUtility
-
-
-
-
-
-
- public static string[] FindAssets(EAssetSearchType searchType, string[] searchInFolders)
- {
-
- for (int i = 0; i < searchInFolders.Length; i++)
- {
- string folderPath = searchInFolders[i];
- searchInFolders[i] = folderPath.TrimEnd('/');
- }
-
- string[] guids;
- if (searchType == EAssetSearchType.All)
- guids = AssetDatabase.FindAssets(string.Empty, searchInFolders);
- else
- guids = AssetDatabase.FindAssets($"t:{searchType}", searchInFolders);
-
- List<string> result = new List<string>();
- for (int i = 0; i < guids.Length; i++)
- {
- string guid = guids[i];
- string assetPath = AssetDatabase.GUIDToAssetPath(guid);
- if (result.Contains(assetPath) == false)
- {
- result.Add(assetPath);
- }
- }
-
- return result.ToArray();
- }
-
-
-
-
-
-
- public static string[] FindAssets(EAssetSearchType searchType, string searchInFolder)
- {
- return FindAssets(searchType, new string[] { searchInFolder });
- }
-
-
-
-
-
-
- public static string OpenFolderPanel(string title, string defaultPath, string defaultName = "")
- {
- string openPath = EditorUtility.OpenFolderPanel(title, defaultPath, defaultName);
- if (string.IsNullOrEmpty(openPath))
- return null;
- if (openPath.Contains("/Assets") == false)
- {
- Debug.LogWarning("Please select unity assets folder.");
- return null;
- }
- return openPath;
- }
-
-
-
-
-
-
- public static string OpenFilePath(string title, string defaultPath, string extension = "")
- {
- string openPath = EditorUtility.OpenFilePanel(title, defaultPath, extension);
- if (string.IsNullOrEmpty(openPath))
- return null;
- if (openPath.Contains("/Assets") == false)
- {
- Debug.LogWarning("Please select unity assets file.");
- return null;
- }
- return openPath;
- }
-
-
-
- public static void DisplayProgressBar(string tips, int progressValue, int totalValue)
- {
- EditorUtility.DisplayProgressBar("进度", $"{tips} : {progressValue}/{totalValue}", (float)progressValue / totalValue);
- }
-
-
-
- public static void ClearProgressBar()
- {
- EditorUtility.ClearProgressBar();
- }
- #endregion
- #region EditorWindow
- public static void FocusUnitySceneWindow()
- {
- EditorWindow.FocusWindowIfItsOpen<SceneView>();
- }
- public static void CloseUnityGameWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView");
- EditorWindow.GetWindow(T, false, "GameView", true).Close();
- }
- public static void FocusUnityGameWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.GameView");
- EditorWindow.GetWindow(T, false, "GameView", true);
- }
- public static void FocueUnityProjectWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ProjectBrowser");
- EditorWindow.GetWindow(T, false, "Project", true);
- }
- public static void FocusUnityHierarchyWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.SceneHierarchyWindow");
- EditorWindow.GetWindow(T, false, "Hierarchy", true);
- }
- public static void FocusUnityInspectorWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.InspectorWindow");
- EditorWindow.GetWindow(T, false, "Inspector", true);
- }
- public static void FocusUnityConsoleWindow()
- {
- System.Type T = Assembly.Load("UnityEditor").GetType("UnityEditor.ConsoleWindow");
- EditorWindow.GetWindow(T, false, "Console", true);
- }
- #endregion
- #region EditorConsole
- private static MethodInfo _clearConsoleMethod;
- private static MethodInfo ClearConsoleMethod
- {
- get
- {
- if (_clearConsoleMethod == null)
- {
- Assembly assembly = Assembly.GetAssembly(typeof(SceneView));
- System.Type logEntries = assembly.GetType("UnityEditor.LogEntries");
- _clearConsoleMethod = logEntries.GetMethod("Clear");
- }
- return _clearConsoleMethod;
- }
- }
-
-
-
- public static void ClearUnityConsole()
- {
- ClearConsoleMethod.Invoke(new object(), null);
- }
- #endregion
- #region SceneUtility
- public static bool HasDirtyScenes()
- {
- var sceneCount = EditorSceneManager.sceneCount;
- for (var i = 0; i < sceneCount; ++i)
- {
- var scene = EditorSceneManager.GetSceneAt(i);
- if (scene.isDirty)
- return true;
- }
- return false;
- }
- #endregion
- #region 文件
-
-
-
-
- public static void CreateFileDirectory(string filePath)
- {
- string destDirectory = Path.GetDirectoryName(filePath);
- CreateDirectory(destDirectory);
- }
-
-
-
- public static bool CreateDirectory(string directory)
- {
- if (Directory.Exists(directory) == false)
- {
- Directory.CreateDirectory(directory);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
- public static bool DeleteDirectory(string directory)
- {
- if (Directory.Exists(directory))
- {
- Directory.Delete(directory, true);
- return true;
- }
- else
- {
- return false;
- }
- }
-
-
-
- public static void FileRename(string filePath, string newName)
- {
- string dirPath = Path.GetDirectoryName(filePath);
- string destPath;
- if (Path.HasExtension(filePath))
- {
- string extentsion = Path.GetExtension(filePath);
- destPath = $"{dirPath}/{newName}{extentsion}";
- }
- else
- {
- destPath = $"{dirPath}/{newName}";
- }
- FileInfo fileInfo = new FileInfo(filePath);
- fileInfo.MoveTo(destPath);
- }
-
-
-
- public static void FileMoveTo(string filePath, string destPath)
- {
- FileInfo fileInfo = new FileInfo(filePath);
- fileInfo.MoveTo(destPath);
- }
-
-
-
-
- public static void CopyDirectory(string sourcePath, string destPath)
- {
- sourcePath = EditorTools.GetRegularPath(sourcePath);
-
- if (Directory.Exists(destPath) == false)
- Directory.CreateDirectory(destPath);
- string[] fileList = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);
- foreach (string file in fileList)
- {
- string temp = EditorTools.GetRegularPath(file);
- string savePath = temp.Replace(sourcePath, destPath);
- CopyFile(file, savePath, true);
- }
- }
-
-
-
- public static void CopyFile(string sourcePath, string destPath, bool overwrite)
- {
- if (File.Exists(sourcePath) == false)
- throw new FileNotFoundException(sourcePath);
-
- CreateFileDirectory(destPath);
-
- File.Copy(sourcePath, destPath, overwrite);
- }
-
-
-
-
- public static void ClearFolder(string directoryPath)
- {
- if (Directory.Exists(directoryPath) == false)
- return;
-
- string[] allFiles = Directory.GetFiles(directoryPath);
- for (int i = 0; i < allFiles.Length; i++)
- {
- File.Delete(allFiles[i]);
- }
-
- string[] allFolders = Directory.GetDirectories(directoryPath);
- for (int i = 0; i < allFolders.Length; i++)
- {
- Directory.Delete(allFolders[i], true);
- }
- }
-
-
-
- public static long GetFileSize(string filePath)
- {
- FileInfo fileInfo = new FileInfo(filePath);
- return fileInfo.Length;
- }
-
-
-
- public static string ReadFileAllText(string filePath)
- {
- if (File.Exists(filePath) == false)
- return string.Empty;
- return File.ReadAllText(filePath, Encoding.UTF8);
- }
-
-
-
- public static string[] ReadFileAllLine(string filePath)
- {
- if (File.Exists(filePath) == false)
- return null;
- return File.ReadAllLines(filePath, Encoding.UTF8);
- }
-
-
-
- public static bool CheckBundleFileValid(byte[] fileData)
- {
- string signature = ReadStringToNull(fileData, 20);
- if (signature == "UnityFS" || signature == "UnityRaw" || signature == "UnityWeb" || signature == "\xFA\xFA\xFA\xFA\xFA\xFA\xFA\xFA")
- return true;
- else
- return false;
- }
- private static string ReadStringToNull(byte[] data, int maxLength)
- {
- List<byte> bytes = new List<byte>();
- for (int i = 0; i < data.Length; i++)
- {
- if (i >= maxLength)
- break;
- byte bt = data[i];
- if (bt == 0)
- break;
- bytes.Add(bt);
- }
- if (bytes.Count == 0)
- return string.Empty;
- else
- return Encoding.UTF8.GetString(bytes.ToArray());
- }
- #endregion
- #region 路径
-
-
-
- public static string GetRegularPath(string path)
- {
- return path.Replace('\\', '/').Replace("\\", "/");
- }
-
-
-
- public static string GetProjectPath()
- {
- string projectPath = Path.GetDirectoryName(Application.dataPath);
- return GetRegularPath(projectPath);
- }
-
-
-
-
- public static string AbsolutePathToAssetPath(string absolutePath)
- {
- string content = GetRegularPath(absolutePath);
- return Substring(content, "Assets/", true);
- }
-
-
-
-
- public static string AssetPathToAbsolutePath(string assetPath)
- {
- string projectPath = GetProjectPath();
- return $"{projectPath}/{assetPath}";
- }
-
-
-
-
-
-
- public static string FindFolder(string root, string folderName)
- {
- DirectoryInfo rootInfo = new DirectoryInfo(root);
- DirectoryInfo[] infoList = rootInfo.GetDirectories();
- for (int i = 0; i < infoList.Length; i++)
- {
- string fullPath = infoList[i].FullName;
- if (infoList[i].Name == folderName)
- return fullPath;
- string result = FindFolder(fullPath, folderName);
- if (string.IsNullOrEmpty(result) == false)
- return result;
- }
- return string.Empty;
- }
-
-
-
-
-
-
-
-
- private static string Substring(string content, string key, bool includeKey, bool firstMatch = true)
- {
- if (string.IsNullOrEmpty(key))
- return content;
- int startIndex = -1;
- if (firstMatch)
- startIndex = content.IndexOf(key);
- else
- startIndex = content.LastIndexOf(key);
-
- if (startIndex == -1)
- return content;
- if (includeKey)
- return content.Substring(startIndex);
- else
- return content.Substring(startIndex + key.Length);
- }
- #endregion
- }
- }
|