From 44ad68fc7f39b46210483f29d8b8a8b37050b99b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Tue, 1 Apr 2025 10:33:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Editor/Constant.meta | 3 - Editor/Constant/DisplayNameAttribute.cs | 16 - Editor/Constant/DisplayNameAttribute.cs.meta | 3 - Editor/Misc/HelperInfo.cs | 90 ------ Editor/Misc/HelperInfo.cs.meta | 11 - Editor/Misc/OpenAssetLogLine.cs | 302 +++++++------------ Editor/Misc/ScriptableSingletonUtil.cs | 40 --- Editor/Misc/ScriptableSingletonUtil.cs.meta | 3 - Runtime/Utility/Utility.Assembly.cs | 20 ++ 9 files changed, 128 insertions(+), 360 deletions(-) delete mode 100644 Editor/Constant.meta delete mode 100644 Editor/Constant/DisplayNameAttribute.cs delete mode 100644 Editor/Constant/DisplayNameAttribute.cs.meta delete mode 100644 Editor/Misc/HelperInfo.cs delete mode 100644 Editor/Misc/HelperInfo.cs.meta delete mode 100644 Editor/Misc/ScriptableSingletonUtil.cs delete mode 100644 Editor/Misc/ScriptableSingletonUtil.cs.meta diff --git a/Editor/Constant.meta b/Editor/Constant.meta deleted file mode 100644 index ce0c9aa..0000000 --- a/Editor/Constant.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 1478a6e22f3d4f5e8ff645e5272c8b34 -timeCreated: 1736931429 \ No newline at end of file diff --git a/Editor/Constant/DisplayNameAttribute.cs b/Editor/Constant/DisplayNameAttribute.cs deleted file mode 100644 index f7f52cd..0000000 --- a/Editor/Constant/DisplayNameAttribute.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace AlicizaX.Editor -{ - [AttributeUsage(AttributeTargets.Class)] - public class DisplayNameAttribute:Attribute - { - public string DisplayName; - - public DisplayNameAttribute(string displayName) - { - DisplayName = displayName; - } - } -} - diff --git a/Editor/Constant/DisplayNameAttribute.cs.meta b/Editor/Constant/DisplayNameAttribute.cs.meta deleted file mode 100644 index 1c079da..0000000 --- a/Editor/Constant/DisplayNameAttribute.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 15a53446355a45549c7060c581d194ac -timeCreated: 1737013039 \ No newline at end of file diff --git a/Editor/Misc/HelperInfo.cs b/Editor/Misc/HelperInfo.cs deleted file mode 100644 index 788d223..0000000 --- a/Editor/Misc/HelperInfo.cs +++ /dev/null @@ -1,90 +0,0 @@ -using AlicizaX; -using System.Collections.Generic; -using System.Text.RegularExpressions; -using UnityEditor; -using UnityEngine; - -namespace AlicizaX.Editor -{ - public sealed class HelperInfo where T : MonoBehaviour - { - private const string CustomOptionName = ""; - - private readonly string m_Name; - - private SerializedProperty m_HelperTypeName; - private SerializedProperty m_CustomHelper; - private string[] m_HelperTypeNames; - private int m_HelperTypeNameIndex; - - public HelperInfo(string name) - { - m_Name = name; - - m_HelperTypeName = null; - m_CustomHelper = null; - m_HelperTypeNames = null; - m_HelperTypeNameIndex = 0; - } - - public void Init(SerializedObject serializedObject) - { - m_HelperTypeName = serializedObject.FindProperty(Utility.Text.Format("m_{0}HelperTypeName", m_Name)); - m_CustomHelper = serializedObject.FindProperty(Utility.Text.Format("m_Custom{0}Helper", m_Name)); - } - - public void Draw() - { - string displayName = FieldNameForDisplay(m_Name); - int selectedIndex = EditorGUILayout.Popup(Utility.Text.Format("{0} Helper", displayName), m_HelperTypeNameIndex, m_HelperTypeNames); - if (selectedIndex != m_HelperTypeNameIndex) - { - m_HelperTypeNameIndex = selectedIndex; - m_HelperTypeName.stringValue = selectedIndex <= 0 ? null : m_HelperTypeNames[selectedIndex]; - } - - if (m_HelperTypeNameIndex <= 0) - { - EditorGUILayout.PropertyField(m_CustomHelper); - if (m_CustomHelper.objectReferenceValue == null) - { - EditorGUILayout.HelpBox(Utility.Text.Format("You must set Custom {0} Helper.", displayName), MessageType.Error); - } - } - } - - public void Refresh() - { - List helperTypeNameList = new List - { - CustomOptionName - }; - - helperTypeNameList.AddRange(Utility.Assembly.GetRuntimeTypeNames(typeof(T))); - m_HelperTypeNames = helperTypeNameList.ToArray(); - - m_HelperTypeNameIndex = 0; - if (!string.IsNullOrEmpty(m_HelperTypeName.stringValue)) - { - m_HelperTypeNameIndex = helperTypeNameList.IndexOf(m_HelperTypeName.stringValue); - if (m_HelperTypeNameIndex <= 0) - { - m_HelperTypeNameIndex = 0; - m_HelperTypeName.stringValue = null; - } - } - } - - private string FieldNameForDisplay(string fieldName) - { - if (string.IsNullOrEmpty(fieldName)) - { - return string.Empty; - } - - string str = Regex.Replace(fieldName, @"^m_", string.Empty); - str = Regex.Replace(str, @"((?<=[a-z])[A-Z]|[A-Z](?=[a-z]))", @" $1").TrimStart(); - return str; - } - } -} \ No newline at end of file diff --git a/Editor/Misc/HelperInfo.cs.meta b/Editor/Misc/HelperInfo.cs.meta deleted file mode 100644 index 0690695..0000000 --- a/Editor/Misc/HelperInfo.cs.meta +++ /dev/null @@ -1,11 +0,0 @@ -fileFormatVersion: 2 -guid: 7ff85bf9d2c44a14d90f78221024499e -MonoImporter: - externalObjects: {} - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Editor/Misc/OpenAssetLogLine.cs b/Editor/Misc/OpenAssetLogLine.cs index 11d90d2..c1f7e62 100644 --- a/Editor/Misc/OpenAssetLogLine.cs +++ b/Editor/Misc/OpenAssetLogLine.cs @@ -1,194 +1,108 @@ -// using System; -// using System.Reflection; -// using AlicizaX.Runtime; -// using UnityEditor; -// using UnityEngine; -// -// namespace AlicizaX.Editor -// { -// public static class LogEditor -// { -// private class LogEditorConfig -// { -// public string logScriptPath = ""; -// public string logTypeName = ""; -// public int instanceID = 0; -// -// public LogEditorConfig(string logScriptPath, System.Type logType) -// { -// this.logScriptPath = logScriptPath; -// this.logTypeName = logType.FullName; -// } -// } -// -// -// private static LogEditorConfig[] _logEditorConfig = new LogEditorConfig[] -// { -// new LogEditorConfig("Packages/com.alicizax.unity/Runtime/Base/Log/Log.cs", typeof(Log)) -// }; -// -// -// [UnityEditor.Callbacks.OnOpenAssetAttribute(-1)] -// private static bool OnOpenAsset(int instanceID, int line) -// { -// for (int i = _logEditorConfig.Length - 1; i >= 0; --i) -// { -// var configTmp = _logEditorConfig[i]; -// UpdateLogInstanceID(configTmp); -// if (instanceID == configTmp.instanceID) -// { -// var statckTrack = GetStackTrace(); -// if (!string.IsNullOrEmpty(statckTrack)) -// { -// var fileNames = statckTrack.Split('\n'); -// var fileName = GetCurrentFullFileName(fileNames); -// var fileLine = LogFileNameToFileLine(fileName); -// fileName = GetRealFileName(fileName); -// -// AssetDatabase.OpenAsset(AssetDatabase.LoadAssetAtPath(fileName), fileLine); -// return true; -// } -// -// break; -// } -// } -// -// return false; -// } -// -// private static string GetStackTrace() -// { -// var consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow"); -// var fieldInfo = consoleWindowType.GetField("ms_ConsoleWindow", BindingFlags.Static | BindingFlags.NonPublic); -// var consoleWindowInstance = fieldInfo.GetValue(null); -// -// if (null != consoleWindowInstance) -// { -// if ((object)EditorWindow.focusedWindow == consoleWindowInstance) -// { -// // Get ListViewState in ConsoleWindow -// // var listViewStateType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ListViewState"); -// // fieldInfo = consoleWindowType.GetField("m_ListView", BindingFlags.Instance | BindingFlags.NonPublic); -// // var listView = fieldInfo.GetValue(consoleWindowInstance); -// -// // Get row in listViewState -// // fieldInfo = listViewStateType.GetField("row", BindingFlags.Instance | BindingFlags.Public); -// // int row = (int)fieldInfo.GetValue(listView); -// -// // Get m_ActiveText in ConsoleWindow -// fieldInfo = consoleWindowType.GetField("m_ActiveText", BindingFlags.Instance | BindingFlags.NonPublic); -// string activeText = fieldInfo.GetValue(consoleWindowInstance).ToString(); -// -// return activeText; -// } -// } -// -// return ""; -// } -// -// private static void UpdateLogInstanceID(LogEditorConfig config) -// { -// if (config.instanceID > 0) -// { -// return; -// } -// -// var assetLoadTmp = AssetDatabase.LoadAssetAtPath(config.logScriptPath); -// if (null == assetLoadTmp) -// { -// throw new System.Exception("not find asset by path=" + config.logScriptPath); -// } -// -// config.instanceID = assetLoadTmp.GetInstanceID(); -// } -// -// private static string GetCurrentFullFileName(string[] fileNames) -// { -// string retValue = ""; -// int findIndex = -1; -// -// for (int i = fileNames.Length - 1; i >= 0; --i) -// { -// bool isCustomLog = false; -// for (int j = _logEditorConfig.Length - 1; j >= 0; --j) -// { -// if (fileNames[i].Contains(_logEditorConfig[j].logTypeName)) -// { -// isCustomLog = true; -// break; -// } -// } -// -// if (isCustomLog) -// { -// findIndex = i; -// break; -// } -// } -// -// if (findIndex >= 0 && findIndex < fileNames.Length - 1) -// { -// retValue = fileNames[findIndex + 1]; -// } -// -// return retValue; -// } -// -// private static string GetRealFileName(string fileName) -// { -// int indexStart = fileName.IndexOf("(at ") + "(at ".Length; -// int indexEnd = ParseFileLineStartIndex(fileName) - 1; -// -// fileName = fileName.Substring(indexStart, indexEnd - indexStart); -// return fileName; -// } -// -// private static int LogFileNameToFileLine(string fileName) -// { -// int findIndex = ParseFileLineStartIndex(fileName); -// string stringParseLine = ""; -// for (int i = findIndex; i < fileName.Length; ++i) -// { -// var charCheck = fileName[i]; -// if (!IsNumber(charCheck)) -// { -// break; -// } -// else -// { -// stringParseLine += charCheck; -// } -// } -// -// return int.Parse(stringParseLine); -// } -// -// private static int ParseFileLineStartIndex(string fileName) -// { -// int retValue = -1; -// for (int i = fileName.Length - 1; i >= 0; --i) -// { -// var charCheck = fileName[i]; -// bool isNumber = IsNumber(charCheck); -// if (isNumber) -// { -// retValue = i; -// } -// else -// { -// if (retValue != -1) -// { -// break; -// } -// } -// } -// -// return retValue; -// } -// -// private static bool IsNumber(char c) -// { -// return c >= '0' && c <= '9'; -// } -// } -// } +using System; +using UnityEditor; +using System.Reflection; +using System.Text.RegularExpressions; +using UnityEditor.Callbacks; +using UnityEditorInternal; +using UnityEngine; + +namespace AlicizaX.Editor +{ + public static class OpenAssetLogLine + { + [OnOpenAsset(0)] + private static bool OnOpenAsset(int instanceID, int line) + { + if (line <= 0) + { + return false; + } + // 获取资源路径 + string assetPath = AssetDatabase.GetAssetPath(instanceID); + + // 判断资源类型 + if (!assetPath.EndsWith(".cs")) + { + return false; + } + + bool autoFirstMatch = assetPath.Contains("Log.cs"); + + var stackTrace = GetStackTrace(); + if (!string.IsNullOrEmpty(stackTrace) && stackTrace.Contains("Log.cs")) + + { + if (!autoFirstMatch) + { + var fullPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal)); + fullPath = $"{fullPath}{assetPath}"; + // 跳转到目标代码的特定行 + InternalEditorUtility.OpenFileAtLineExternal(fullPath.Replace('/', '\\'), line); + return true; + } + + // 使用正则表达式匹配at的哪个脚本的哪一行 + var matches = Regex.Match(stackTrace, @"\(at (.+)\)", + RegexOptions.IgnoreCase); + while (matches.Success) + { + var pathLine = matches.Groups[1].Value; + + if (!pathLine.Contains("Log.cs")) + { + var splitIndex = pathLine.LastIndexOf(":", StringComparison.Ordinal); + // 脚本路径 + var path = pathLine.Substring(0, splitIndex); + // 行号 + line = Convert.ToInt32(pathLine.Substring(splitIndex + 1)); + var fullPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("Assets", StringComparison.Ordinal)); + fullPath = $"{fullPath}{path}"; + // 跳转到目标代码的特定行 + InternalEditorUtility.OpenFileAtLineExternal(fullPath.Replace('/', '\\'), line); + break; + } + + matches = matches.NextMatch(); + } + + return true; + } + + return false; + } + + /// + /// 获取当前日志窗口选中的日志的堆栈信息。 + /// + /// 选中日志的堆栈信息实例。 + private static string GetStackTrace() + { + // 通过反射获取ConsoleWindow类 + var consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow"); + // 获取窗口实例 + var fieldInfo = consoleWindowType.GetField("ms_ConsoleWindow", + BindingFlags.Static | + BindingFlags.NonPublic); + if (fieldInfo != null) + { + var consoleInstance = fieldInfo.GetValue(null); + if (consoleInstance != null) + if (EditorWindow.focusedWindow == (EditorWindow)consoleInstance) + { + // 获取m_ActiveText成员 + fieldInfo = consoleWindowType.GetField("m_ActiveText", + BindingFlags.Instance | + BindingFlags.NonPublic); + // 获取m_ActiveText的值 + if (fieldInfo != null) + { + var activeText = fieldInfo.GetValue(consoleInstance).ToString(); + return activeText; + } + } + } + + return null; + } + } +} diff --git a/Editor/Misc/ScriptableSingletonUtil.cs b/Editor/Misc/ScriptableSingletonUtil.cs deleted file mode 100644 index 39a4a14..0000000 --- a/Editor/Misc/ScriptableSingletonUtil.cs +++ /dev/null @@ -1,40 +0,0 @@ -using System; -using AlicizaX; -using UnityEngine; - -namespace AlicizaX.Editor -{ - public static class ScriptableSingletonUtil - { - public static T Get() where T : ScriptableObject - { - string assetType = typeof(T).Name; - T globalSetting = default; - string[] globalAssetPaths = UnityEditor.AssetDatabase.FindAssets($"t:{assetType}"); - if (globalAssetPaths.Length > 1) - { - foreach (var assetPath in globalAssetPaths) - { - Debug.LogError($"Could not had Multiple {assetType}. Repeated Path: {UnityEditor.AssetDatabase.GUIDToAssetPath(assetPath)}"); - } - - throw new Exception($"Could not had Multiple {assetType}"); - } - - if (globalAssetPaths.Length == 1) - { - string path = UnityEditor.AssetDatabase.GUIDToAssetPath(globalAssetPaths[0]); - globalSetting = UnityEditor.AssetDatabase.LoadAssetAtPath(path); - } - - - if (globalSetting == null) - { - Debug.LogError($"Could not found {assetType} asset"); - return null; - } - - return globalSetting; - } - } -} \ No newline at end of file diff --git a/Editor/Misc/ScriptableSingletonUtil.cs.meta b/Editor/Misc/ScriptableSingletonUtil.cs.meta deleted file mode 100644 index 4072c30..0000000 --- a/Editor/Misc/ScriptableSingletonUtil.cs.meta +++ /dev/null @@ -1,3 +0,0 @@ -fileFormatVersion: 2 -guid: 7544b657bf904330859e64362e5098a8 -timeCreated: 1737536609 \ No newline at end of file diff --git a/Runtime/Utility/Utility.Assembly.cs b/Runtime/Utility/Utility.Assembly.cs index 16da409..3a748a3 100644 --- a/Runtime/Utility/Utility.Assembly.cs +++ b/Runtime/Utility/Utility.Assembly.cs @@ -122,6 +122,26 @@ namespace AlicizaX return results; } + + public static List GetRuntimeTypes(Type typeBase) + { + var types = GetTypes(); + List results = new List(); + foreach (var t in types) + { + if (t.IsAbstract || !t.IsClass) + { + continue; + } + + if (t.IsSubclassOf(typeBase) || t.IsImplWithInterface(typeBase)) + { + results.Add(t); + } + } + + return results; + } } } }