@@ -14,8 +14,6 @@ namespace McpUnity.Unity
14
14
public class McpUnityEditorWindow : EditorWindow
15
15
{
16
16
private string _statusMessage = "" ;
17
- private MessageType _statusMessageType = MessageType . Info ;
18
- private double _statusMessageExpireTime = 0 ;
19
17
private GUIStyle _headerStyle ;
20
18
private GUIStyle _subHeaderStyle ;
21
19
private GUIStyle _boxStyle ;
@@ -35,52 +33,53 @@ public static void ShowWindow()
35
33
36
34
private void OnEnable ( )
37
35
{
36
+ titleContent = new GUIContent ( "MCP Unity" ) ;
37
+ _statusMessage = "Ready" ;
38
+
38
39
// Subscribe to McpUnityBridge events
39
40
McpUnityBridge . OnConnected += HandleConnected ;
40
41
McpUnityBridge . OnDisconnected += HandleDisconnected ;
42
+ McpUnityBridge . OnConnecting += HandleConnecting ;
41
43
McpUnityBridge . OnError += HandleError ;
42
44
43
- // Set up regular UI updates
44
- EditorApplication . update += OnEditorUpdate ;
45
+ InitializeStyles ( ) ;
45
46
}
46
47
47
48
private void OnDisable ( )
48
49
{
49
50
// Unsubscribe from McpUnityBridge events
50
51
McpUnityBridge . OnConnected -= HandleConnected ;
51
52
McpUnityBridge . OnDisconnected -= HandleDisconnected ;
53
+ McpUnityBridge . OnConnecting -= HandleConnecting ;
52
54
McpUnityBridge . OnError -= HandleError ;
53
-
54
- // Remove editor update callback
55
- EditorApplication . update -= OnEditorUpdate ;
56
55
}
57
56
58
57
private void HandleConnected ( )
59
58
{
60
- SetStatusMessage ( "Connected to server" , MessageType . Info ) ;
59
+ _statusMessage = "Connected" ;
60
+
61
61
Repaint ( ) ;
62
62
}
63
63
64
64
private void HandleDisconnected ( )
65
65
{
66
- SetStatusMessage ( "Disconnected from server" , MessageType . Info ) ;
66
+ _statusMessage = "Disconnected" ;
67
+
67
68
Repaint ( ) ;
68
69
}
69
70
70
- private void HandleError ( string errorMessage )
71
+ private void HandleConnecting ( )
71
72
{
72
- SetStatusMessage ( $ "Error: { errorMessage } ", MessageType . Error ) ;
73
+ _statusMessage = "Connecting..." ;
74
+
73
75
Repaint ( ) ;
74
76
}
75
77
76
- private void OnEditorUpdate ( )
78
+ private void HandleError ( string errorMessage )
77
79
{
78
- // Check if status message has expired
79
- if ( ! string . IsNullOrEmpty ( _statusMessage ) && EditorApplication . timeSinceStartup > _statusMessageExpireTime )
80
- {
81
- _statusMessage = "" ;
82
- Repaint ( ) ;
83
- }
80
+ _statusMessage = $ "Error: { errorMessage } ";
81
+
82
+ Repaint ( ) ;
84
83
}
85
84
86
85
private void OnGUI ( )
@@ -111,7 +110,7 @@ private void OnGUI()
111
110
// Bottom Status bar
112
111
if ( ! string . IsNullOrEmpty ( _statusMessage ) )
113
112
{
114
- EditorGUILayout . HelpBox ( _statusMessage , _statusMessageType ) ;
113
+ EditorGUILayout . HelpBox ( _statusMessage , MessageType . Info ) ;
115
114
}
116
115
117
116
// Version info at the bottom
@@ -131,8 +130,10 @@ private void DrawServerTab()
131
130
EditorGUILayout . BeginHorizontal ( ) ;
132
131
EditorGUILayout . LabelField ( "Server Status:" , GUILayout . Width ( 120 ) ) ;
133
132
134
- string statusText = McpUnityBridge . Instance . IsConnected ? "Connected" : "Disconnected" ;
135
- Color statusColor = McpUnityBridge . Instance . IsConnected ? Color . green : Color . red ;
133
+ var connectionState = McpUnityBridge . Instance . ConnectionState ;
134
+ var statusText = connectionState == ConnectionState . Connected ? "Connected" : "Disconnected" ;
135
+ var statusColor = connectionState == ConnectionState . Connected ? Color . green : Color . red ;
136
+ statusColor = connectionState == ConnectionState . Connecting ? Color . yellow : statusColor ;
136
137
137
138
GUIStyle statusStyle = new GUIStyle ( EditorStyles . boldLabel ) ;
138
139
statusStyle . normal . textColor = statusColor ;
@@ -155,16 +156,21 @@ private void DrawServerTab()
155
156
// Server control buttons
156
157
EditorGUILayout . BeginHorizontal ( ) ;
157
158
158
- GUI . enabled = ! McpUnityBridge . Instance . IsConnected ;
159
+ // Determine button states based on connection state
160
+ ConnectionState currentState = McpUnityBridge . Instance . ConnectionState ;
161
+
162
+ // Connect button - enabled only when disconnected
163
+ GUI . enabled = currentState == ConnectionState . Disconnected ;
159
164
if ( GUILayout . Button ( "Start Server" , GUILayout . Height ( 30 ) ) )
160
165
{
161
- StartServer ( ) ;
166
+ _ = McpUnityBridge . Instance . Connect ( McpUnitySettings . Instance . WebSocketUrl ) ;
162
167
}
163
168
164
- GUI . enabled = McpUnityBridge . Instance . IsConnected ;
169
+ // Disconnect button - enabled only when connected
170
+ GUI . enabled = currentState == ConnectionState . Connected ;
165
171
if ( GUILayout . Button ( "Stop Server" , GUILayout . Height ( 30 ) ) )
166
172
{
167
- StopServer ( ) ;
173
+ _ = McpUnityBridge . Instance . Disconnect ( ) ;
168
174
}
169
175
170
176
GUI . enabled = true ;
@@ -248,32 +254,6 @@ private void DrawHelpTab()
248
254
EditorGUILayout . EndVertical ( ) ;
249
255
}
250
256
251
- private async void StartServer ( )
252
- {
253
- try
254
- {
255
- await McpUnityBridge . Instance . Connect ( McpUnitySettings . Instance . WebSocketUrl ) ;
256
- }
257
- catch ( Exception ex )
258
- {
259
- Debug . LogError ( $ "[MCP Unity] Failed to start server: { ex . Message } ") ;
260
- EditorUtility . DisplayDialog ( "Error" , $ "Failed to start server: { ex . Message } ", "OK" ) ;
261
- }
262
- }
263
-
264
- private async void StopServer ( )
265
- {
266
- try
267
- {
268
- await McpUnityBridge . Instance . Disconnect ( ) ;
269
- }
270
- catch ( Exception ex )
271
- {
272
- Debug . LogError ( $ "[MCP Unity] Failed to stop server: { ex . Message } ") ;
273
- EditorUtility . DisplayDialog ( "Error" , $ "Failed to stop server: { ex . Message } ", "OK" ) ;
274
- }
275
- }
276
-
277
257
private void GenerateMcpConfigJson ( )
278
258
{
279
259
var config = new Dictionary < string , object >
@@ -400,13 +380,6 @@ private void InitializeStyles()
400
380
401
381
_isInitialized = true ;
402
382
}
403
-
404
- private void SetStatusMessage ( string message , MessageType type )
405
- {
406
- _statusMessage = message ;
407
- _statusMessageType = type ;
408
- _statusMessageExpireTime = EditorApplication . timeSinceStartup + 5 ;
409
- }
410
383
411
384
/// <summary>
412
385
/// Creates a label with text that properly wraps based on available width
0 commit comments