修改调整

This commit is contained in:
陈思海 2025-04-01 10:33:33 +08:00
parent ee3e511eee
commit 44ad68fc7f
9 changed files with 128 additions and 360 deletions

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 1478a6e22f3d4f5e8ff645e5272c8b34
timeCreated: 1736931429

View File

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

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 15a53446355a45549c7060c581d194ac
timeCreated: 1737013039

View File

@ -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<T> where T : MonoBehaviour
{
private const string CustomOptionName = "<Custom>";
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<string> helperTypeNameList = new List<string>
{
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;
}
}
}

View File

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

View File

@ -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<UnityEngine.Object>(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<UnityEngine.Object>(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;
}
/// <summary>
/// 获取当前日志窗口选中的日志的堆栈信息。
/// </summary>
/// <returns>选中日志的堆栈信息实例。</returns>
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;
}
}
}

View File

@ -1,40 +0,0 @@
using System;
using AlicizaX;
using UnityEngine;
namespace AlicizaX.Editor
{
public static class ScriptableSingletonUtil
{
public static T Get<T>() 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<T>(path);
}
if (globalSetting == null)
{
Debug.LogError($"Could not found {assetType} asset");
return null;
}
return globalSetting;
}
}
}

View File

@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 7544b657bf904330859e64362e5098a8
timeCreated: 1737536609

View File

@ -122,6 +122,26 @@ namespace AlicizaX
return results;
}
public static List<Type> GetRuntimeTypes(Type typeBase)
{
var types = GetTypes();
List<Type> results = new List<Type>();
foreach (var t in types)
{
if (t.IsAbstract || !t.IsClass)
{
continue;
}
if (t.IsSubclassOf(typeBase) || t.IsImplWithInterface(typeBase))
{
results.Add(t);
}
}
return results;
}
}
}
}