Files
webRtc/Assets/Script/Editor/MessageChannelEditor.cs
2026-05-12 23:04:08 +08:00

134 lines
5.3 KiB
C#
Raw Permalink 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.
// Assets/Script/Editor/MessageChannelEditor.cs
using Unity.RenderStreaming;
using UnityEditor;
using UnityEngine;
namespace RenderStreaming.Editor
{
[CustomEditor(typeof(MessageChannel))]
public class MessageChannelEditor : UnityEditor.Editor
{
private Vector2 _scrollPos;
private bool _showHistory = true;
private bool _showStats = true;
public override void OnInspectorGUI()
{
DrawDefaultInspector();
var channel = (MessageChannel)target;
EditorGUILayout.Space(10);
// --- 连接状态 ---
EditorGUILayout.BeginVertical("HelpBox");
EditorGUILayout.LabelField("连接状态", EditorStyles.boldLabel);
EditorGUI.indentLevel++;
EditorGUILayout.LabelField("Channel Label", channel.Label);
EditorGUILayout.LabelField("Connection ID", channel.ConnectionId ?? "未连接");
var stateContent = channel.IsConnected
? new GUIContent("● 已连接", EditorGUIUtility.FindTexture("greenCheck"))
: new GUIContent("○ 未连接");
if (channel.IsConnected)
GUI.color = Color.green;
else
GUI.color = Color.gray;
EditorGUILayout.LabelField( stateContent);
GUI.color = Color.white;
EditorGUI.indentLevel--;
EditorGUILayout.EndVertical();
EditorGUILayout.Space(5);
// --- 统计信息 ---
if (_showStats)
{
EditorGUILayout.BeginVertical("HelpBox");
_showStats = EditorGUILayout.BeginToggleGroup("统计信息", _showStats);
EditorGUI.indentLevel++;
EditorGUILayout.LabelField("收到消息总数", channel.ReceivedMessageCount.ToString());
EditorGUILayout.LabelField("历史记录数", channel.MessageHistory.Count.ToString());
EditorGUI.indentLevel--;
EditorGUILayout.EndToggleGroup();
EditorGUILayout.EndVertical();
}
EditorGUILayout.Space(5);
// --- 消息历史 ---
EditorGUILayout.BeginVertical("HelpBox");
_showHistory = EditorGUILayout.Foldout(_showHistory, "消息历史", true, EditorStyles.boldLabel);
if (_showHistory)
{
var history = channel.MessageHistory;
if (history == null || history.Count == 0)
{
EditorGUILayout.HelpBox("暂无消息记录", MessageType.Info);
}
else
{
_scrollPos = EditorGUILayout.BeginScrollView(
_scrollPos, GUILayout.MinHeight(120), GUILayout.MaxHeight(300));
for (int i = history.Count - 1; i >= 0; i--)
{
var msg = history[i];
// 交替背景色
if (i % 2 == 0)
{
var bg = new GUIStyle("CN EntryBackEven");
EditorGUILayout.BeginVertical(bg);
}
else
{
EditorGUILayout.BeginVertical();
}
// 方向标签 + 时间
EditorGUILayout.BeginHorizontal();
var dirIcon = msg.isSent ? "▶ 发送" : "◀ 接收";
var dirStyle = msg.isSent
? new GUIStyle(EditorStyles.label) { normal = { textColor = new Color(0.3f, 0.6f, 1f) } }
: new GUIStyle(EditorStyles.label) { normal = { textColor = new Color(1f, 0.7f, 0.2f) } };
EditorGUILayout.LabelField(dirIcon, dirStyle, GUILayout.Width(60));
EditorGUILayout.LabelField(msg.timestamp, GUILayout.Width(70));
//EditorGUILayout.LabelField(msg.connectionId, EditorStyles.miniLabel);
EditorGUILayout.EndHorizontal();
// 消息内容
EditorGUILayout.SelectableLabel(msg.type, EditorStyles.wordWrappedLabel,
GUILayout.Height(EditorStyles.wordWrappedLabel.CalcHeight(
new GUIContent(msg.type), EditorGUIUtility.currentViewWidth - 40)));
EditorGUILayout.EndVertical();
EditorGUILayout.Space(2);
}
EditorGUILayout.EndScrollView();
}
// 清除按钮
EditorGUILayout.Space(5);
GUI.backgroundColor = new Color(1f, 0.5f, 0.5f);
if (GUILayout.Button("清除消息记录", GUILayout.Height(25)))
{
Undo.RecordObject(channel, "Clear Message History");
channel.ClearHistory();
EditorUtility.SetDirty(channel);
}
GUI.backgroundColor = Color.white;
}
EditorGUILayout.EndVertical();
// 持续刷新 Inspector消息实时到达时自动更新
if (Application.isPlaying && channel.IsConnected)
{
Repaint();
}
}
}
}