想要使用UnityEditorWindow作为调试窗口吗?
这样做可以很方便的针对游戏中的重要对象做调试。
但是有一个很不方便的地方,OnGUI 的刷新频率不高,或者说需要鼠标点击之后才会重绘,如何解决这一问题?
可以如下操作:
在 Update 函数中调用 Repaint();
using System;
using System.Collections.Generic;
using System.Text;
using cfg.Config;
using InPlayBuff;
using UnityEditor;
using UnityEngine;public class BuffDebugWindow : EditorWindow
{[MenuItem("SoyBean/Debug/Buff")]private static void ShowBuffDebugInspector(){BuffDebugWindow window = EditorWindow.GetWindow<BuffDebugWindow>();window.Show();}private void OnGUI(){if (!Application.isPlaying)return;if (_manager == null){_manager = FindObjectOfType<BuffManager>();}if (_manager == null){return;}List<BuffBase> allBuff = _manager.GetAllBuff();for (int i = 0; i < allBuff.Count; i++){BuffBase buff = allBuff[i];DrawBuffInfo(buff);}}private void DrawBuffInfo(BuffBase buff){StringBuilder builder = new StringBuilder();builder.Append(buff.Owner.name);builder.Append(" | ");builder.Append(buff.Name);builder.Append(" | ");builder.Append(buff.RemainingDuration);builder.Append(" | ");builder.Append(buff.CurrentLevel);builder.Append(" | ");builder.Append(buff.Description);builder.Append(" | ");if (buff is SpecialDataBuff){SpecialDataBuff specialDataBuff = buff as SpecialDataBuff;for (int i = 0; i < specialDataBuff.Data.AttributeNumber1.Count; i++){builder.Append($"效果{i}:");IDToFloat idToFloat = specialDataBuff.Data.AttributeNumber1[i];builder.Append(LubanConfigManager.Instance.Tables.AttributesDataConfig.Get(idToFloat.ID).Name);builder.Append($"{idToFloat.Value} ");}}EditorGUILayout.LabelField(builder.ToString());}private void Update(){if (_manager != null){Repaint();}}private BuffManager _manager;
}
这里是我写的一个 buff 实时查看调试器。
在游戏运行起来的时候此调试器才会工作,这个调试器会实时展示当前游戏中存在的 buff 信息。