UnityLogSetting.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. 
  2. using XGame.Framework.Logger;
  3. using XGame.Framework.Utils;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace XGame.Editor.Logger
  7. {
  8. public class UnityLogSettingWindow : EditorWindow
  9. {
  10. [MenuItem("Tools/Log Setting/Color Setting")]
  11. public static void ShowUnityLogSetting()
  12. {
  13. UnityLogSettingWindow window = EditorWindow.GetWindowWithRect<UnityLogSettingWindow>(new Rect(0, 0, 600, 200) ,false, "LogSetting");
  14. window.Show();
  15. }
  16. //[MenuItem("Tools/Log Setting/Test")]
  17. //public static void Test()
  18. //{
  19. // Debug.Log($"{typeof(LogLevel)}.{LogLevel.Event}");
  20. //}
  21. private Color32 _debug;
  22. private Color32 _info;
  23. private Color32 _warn;
  24. private Color32 _error;
  25. private Color32 _exception;
  26. private Color32 _evnet;
  27. private GUIStyle titleStyle = new GUIStyle();
  28. private Color32 ReadColor(LogLevel level, Color32 defaultColor)
  29. {
  30. var key = $"{typeof(LogLevel)}.{level}";
  31. if (UnityEditor.EditorPrefs.HasKey(key))
  32. {
  33. var hexColor = (uint)EditorPrefs.GetInt(key);
  34. return ColorUtils.HexToColor(hexColor);
  35. }
  36. return defaultColor;
  37. }
  38. private void WriteColor(LogLevel level, Color32 color)
  39. {
  40. var key = $"{typeof(LogLevel)}.{level}";
  41. EditorPrefs.SetInt(key, (int)ColorUtils.ColorToHex(color));
  42. }
  43. private void Save()
  44. {
  45. WriteColor(LogLevel.Debug, _debug);
  46. WriteColor(LogLevel.Info, _info);
  47. WriteColor(LogLevel.Warn, _warn);
  48. WriteColor(LogLevel.Error, _error);
  49. WriteColor(LogLevel.Exception, _exception);
  50. WriteColor(LogLevel.Event, _evnet);
  51. }
  52. private void OnEnable()
  53. {
  54. _debug = ReadColor(LogLevel.Debug, ColorUtils.HexToColor(0xFF8428D9));
  55. _info = ReadColor(LogLevel.Info, Color.white);
  56. _warn = ReadColor(LogLevel.Warn, Color.yellow);
  57. _error = ReadColor(LogLevel.Error, Color.red);
  58. _exception = ReadColor(LogLevel.Exception, ColorUtils.HexToColor(0xFFF000FF));
  59. _evnet = ReadColor(LogLevel.Event, Color.white);
  60. }
  61. private void DrawLine(LogLevel level, ref Color32 color)
  62. {
  63. EditorGUILayout.BeginHorizontal();
  64. titleStyle.normal.textColor = color;
  65. EditorGUILayout.LabelField($"日志等级[{level}]文本颜色", titleStyle, GUILayout.Width(300));
  66. color = EditorGUILayout.ColorField(color);
  67. EditorGUILayout.EndHorizontal();
  68. GUILayout.Space(2);
  69. }
  70. private void OnGUI()
  71. {
  72. GUILayout.Space(5);
  73. DrawLine(LogLevel.Debug, ref _debug);
  74. DrawLine(LogLevel.Info, ref _info);
  75. DrawLine(LogLevel.Warn, ref _warn);
  76. DrawLine(LogLevel.Error, ref _error);
  77. DrawLine(LogLevel.Exception, ref _exception);
  78. DrawLine(LogLevel.Event, ref _evnet);
  79. EditorGUILayout.Space();
  80. EditorGUILayout.Space();
  81. if (GUILayout.Button("Save"))
  82. Save();
  83. }
  84. }
  85. }