Files
plugin-library/Assets/01.CodeChecker/Editor/CodeChecker.cs
2025-03-04 16:02:44 +08:00

181 lines
6.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
namespace wvdet.CodeChecker
{
public class CodeChecker : EditorWindow
{
private Vector2 _scroll;
private int _warningCount;
private int _errorCount;
private GUIStyle kLabelStyle;
private GUIStyle kHintStyle;
private GUIStyle kSuggestionStyle;
private GUIStyle kWarningStyle;
private GUIStyle kErrorStyle;
private GUIStyle kWaitingStyle;
private GUIStyle kWonderfulStyle;
private static GUIContent _sceneIcon;
public List<Result> Results { get; set; }
private static GUIContent scriptIcon => _sceneIcon ?? (_sceneIcon = EditorGUIUtility.IconContent("d_cs Script Icon"));
[MenuItem("Evo/Utility/优化/代码检查", false, 30)]
public static void TopMenuItem()
{
var wnd = GetWindow<CodeChecker>();
wnd.titleContent = new GUIContent("代码检查");
wnd.minSize = new Vector2(800f, 600f);
EditorApplication.delayCall += () => { wnd.UpdateResults(); };
}
public void UpdateResults()
{
EditorUtility.DisplayProgressBar("", "代码检查中...", 0);
Results = CheckerUtils.CheckCodes();
Results.ForEach(result => { _errorCount += result.errorCount; });
Results.ForEach(result => { _warningCount += result.warningCount; });
EditorUtility.ClearProgressBar();
}
private void OnGUI()
{
if (kLabelStyle == null)
InitGuiStyles();
EditorGUILayout.Separator();
if (GUILayout.Button("检 查", GUILayout.Height(25)))
UpdateResults();
if (Results == null)
DrawWaitingTip();
else if (Results.Count > 0)
DrawAllResults();
else
DrawWonderfulTip();
}
private void DrawAllResults()
{
EditorGUILayout.Separator();
DrawLabel($"错误:{_errorCount}\t\t警告: {_warningCount}");
_scroll = GUILayout.BeginScrollView(_scroll, GUILayout.Height(position.height - 40));
{
GUI.skin.button.alignment = TextAnchor.UpperLeft;
{
foreach (var result in Results)
{
EditorGUILayout.BeginVertical(EditorStyles.helpBox);
{
scriptIcon.text = $"{result.filepath} \t\t (Warning: {result.warningCount} Error: {result.errorCount})";
if (GUILayout.Button(scriptIcon, EditorStyles.linkLabel, GUILayout.Height(20)))
EditorGUIUtility.PingObject(AssetDatabase.LoadAssetAtPath(result.filepath, typeof(Object)));
for (int i = 0; i < result.details.Count; i++)
{
if (i >= 1)
DrawGUILine();
DrawResultDetail(result.details[i]);
if (i < result.details.Count - 1)
EditorGUILayout.Separator();
}
}
EditorGUILayout.EndVertical();
EditorGUILayout.Separator();
}
}
GUI.skin.button.alignment = TextAnchor.MiddleCenter;
}
GUILayout.EndScrollView();
}
private void DrawResultDetail(Detail detail)
{
var style = GetLevelStyle(detail.level);
if (!string.IsNullOrWhiteSpace(detail.guideline))
DrawLabel($"规则: {detail.guideline}", style);
if (detail.line > 0)
DrawLabel($"行号: {detail.line}");
if (!string.IsNullOrWhiteSpace(detail.codeSnippet))
EditorGUILayout.LabelField($"当前: {detail.codeSnippet}", kLabelStyle);
// DrawLabel($"当前: {detail.codeSnippet}");
if (!string.IsNullOrWhiteSpace(detail.suggestion))
DrawLabel($"建议: {detail.suggestion}", kSuggestionStyle);
}
private void DrawWaitingTip()
{
EditorGUILayout.LabelField("无检查记录,请重新检查", kWaitingStyle, GUILayout.Height(150f));
}
private void DrawWonderfulTip()
{
EditorGUILayout.LabelField("未检测出不规范的地方\n继续加油", kWonderfulStyle, GUILayout.Height(150f));
}
private GUIStyle GetLevelStyle(Level level)
{
var style = kLabelStyle;
if (level == Level.Hint)
style = kHintStyle;
else if (level == Level.Warning)
style = kWarningStyle;
else if (level == Level.Error)
style = kErrorStyle;
return style;
}
private void DrawLabel(string text, GUIStyle style = null)
{
// EditorGUILayout.SelectableLabel($"{space}{text}", style ?? kNormalStyle, GUILayout.Height(16f));
EditorGUILayout.LabelField(text, style ?? kLabelStyle, GUILayout.Height(16f));
}
private void InitGuiStyles()
{
kLabelStyle = new GUIStyle(EditorStyles.label);
kLabelStyle.fontSize = 12;
// kLabelStyle.wordWrap = false;
kHintStyle = new GUIStyle(kLabelStyle);
kHintStyle.normal.textColor = Color.cyan;
kSuggestionStyle = new GUIStyle(kLabelStyle);
kSuggestionStyle.normal.textColor = Color.green;
kWarningStyle = new GUIStyle(kLabelStyle);
kWarningStyle.normal.textColor = Color.yellow;
kErrorStyle = new GUIStyle(kLabelStyle);
kErrorStyle.normal.textColor = Color.red;
kWaitingStyle = new GUIStyle(EditorStyles.label);
kWaitingStyle.fontSize = 30;
kWaitingStyle.fontStyle = FontStyle.Bold;
kWaitingStyle.alignment = TextAnchor.MiddleCenter;
kWaitingStyle.normal.textColor = Color.grey;
kWonderfulStyle = new GUIStyle(kWaitingStyle);
kWonderfulStyle.normal.textColor = Color.green;
}
public static void DrawGUILine(int height = 1)
{
Rect rect = EditorGUILayout.GetControlRect(false, height);
rect.height = height;
EditorGUI.DrawRect(rect, new Color(0.5f, 0.5f, 0.5f, 1));
}
}
}