11using System ;
2- using System . Collections . Generic ;
32using System . IO ;
4- using System . Linq ;
5- using System . Net ;
6- using System . Net . NetworkInformation ;
7- using System . Net . Sockets ;
83using System . Runtime . InteropServices ;
9- using System . Text ;
10- using System . Threading . Tasks ;
114using Newtonsoft . Json ;
125using UnityEditor ;
136using UnityEngine ;
@@ -23,9 +16,6 @@ public class UnityMcpEditorWindow : EditorWindow
2316 private Vector2 scrollPosition ;
2417 private string pythonServerInstallationStatus = "Not Installed" ;
2518 private Color pythonServerInstallationStatusColor = Color . red ;
26- private string pythonServerConnectionStatus = "Not Connected" ;
27- private Color pythonServerConnectionStatusColor = Color . red ;
28- private DateTime lastConnectionCheck ;
2919 private const int unityPort = 6400 ; // Hardcoded Unity port
3020 private const int mcpPort = 6500 ; // Hardcoded MCP port
3121 private readonly McpClients mcpClients = new ( ) ;
@@ -39,7 +29,6 @@ public static void ShowWindow()
3929 private void OnEnable ( )
4030 {
4131 UpdatePythonServerInstallationStatus ( ) ;
42- UpdatePythonServerConnectionStatus ( ) ;
4332
4433 isUnityBridgeRunning = UnityMcpBridge . IsRunning ;
4534 foreach ( McpClient mcpClient in mcpClients . clients )
@@ -48,16 +37,6 @@ private void OnEnable()
4837 }
4938 }
5039
51- private void Update ( )
52- {
53- if ( lastConnectionCheck . AddSeconds ( 2 ) < DateTime . Now )
54- {
55- UpdatePythonServerConnectionStatus ( ) ;
56-
57- lastConnectionCheck = DateTime . Now ;
58- }
59- }
60-
6140 private Color GetStatusColor ( McpStatus status )
6241 {
6342 // Return appropriate color based on the status enum
@@ -84,13 +63,13 @@ private void UpdatePythonServerInstallationStatus()
8463
8564 if ( ServerInstaller . IsNewerVersion ( latestVersion , installedVersion ) )
8665 {
87- pythonServerInstallationStatus = "Up to Date " ;
88- pythonServerInstallationStatusColor = Color . green ;
66+ pythonServerInstallationStatus = "Newer Version Available " ;
67+ pythonServerInstallationStatusColor = Color . yellow ;
8968 }
9069 else
9170 {
92- pythonServerInstallationStatus = "Newer Version Available " ;
93- pythonServerInstallationStatusColor = Color . yellow ;
71+ pythonServerInstallationStatus = "Up to Date " ;
72+ pythonServerInstallationStatusColor = Color . green ;
9473 }
9574 }
9675 else
@@ -100,41 +79,12 @@ private void UpdatePythonServerInstallationStatus()
10079 }
10180 }
10281
103- private void UpdatePythonServerConnectionStatus ( )
104- {
105- IPGlobalProperties ipGlobalProperties = IPGlobalProperties . GetIPGlobalProperties ( ) ;
106- TcpConnectionInformation [ ] tcpConnections =
107- ipGlobalProperties . GetActiveTcpConnections ( ) ;
108- IPEndPoint [ ] tcpListeners = ipGlobalProperties . GetActiveTcpListeners ( ) ;
109-
110- // Check if the port is in use by any active listener
111- bool isListenerActive = tcpListeners . Any ( static endpoint => endpoint . Port == mcpPort ) ;
112-
113- // Optionally, check if the port is in use by any active connection
114- bool isConnectionActive = tcpConnections . Any ( static connection =>
115- connection . LocalEndPoint . Port == mcpPort
116- || connection . RemoteEndPoint . Port == mcpPort
117- ) ;
118-
119- // Return true if either a listener or connection is using the port
120- if ( isListenerActive || isConnectionActive )
121- {
122- pythonServerConnectionStatus = "Connected" ;
123- pythonServerConnectionStatusColor = Color . green ;
124- }
125- else
126- {
127- pythonServerConnectionStatus = "Not Connected" ;
128- pythonServerConnectionStatusColor = Color . red ;
129- }
130- }
131-
13282 private void ConfigurationSection ( McpClient mcpClient )
13383 {
13484 // Calculate if we should use half-width layout
13585 // Minimum width for half-width layout is 400 pixels
13686 bool useHalfWidth = position . width >= 800 ;
137- float sectionWidth = useHalfWidth ? position . width / 2 - 15 : position . width - 20 ;
87+ float sectionWidth = useHalfWidth ? ( position . width / 2 ) - 15 : position . width - 20 ;
13888
13989 // Begin horizontal layout if using half-width
14090 if ( useHalfWidth && mcpClients . clients . IndexOf ( mcpClient ) % 2 == 0 )
@@ -178,9 +128,11 @@ private void ConfigurationSection(McpClient mcpClient)
178128 EditorGUILayout . Space ( 8 ) ;
179129
180130 // Configure button with improved styling
181- GUIStyle buttonStyle = new ( GUI . skin . button ) ;
182- buttonStyle . padding = new RectOffset ( 15 , 15 , 5 , 5 ) ;
183- buttonStyle . margin = new RectOffset ( 10 , 10 , 5 , 5 ) ;
131+ GUIStyle buttonStyle = new ( GUI . skin . button )
132+ {
133+ padding = new RectOffset ( 15 , 15 , 5 , 5 ) ,
134+ margin = new RectOffset ( 10 , 10 , 5 , 5 ) ,
135+ } ;
184136
185137 // Create muted button style for Manual Setup
186138 GUIStyle mutedButtonStyle = new ( buttonStyle ) ;
@@ -228,7 +180,11 @@ private void ConfigurationSection(McpClient mcpClient)
228180 private void DrawStatusDot ( Rect statusRect , Color statusColor )
229181 {
230182 Rect dotRect = new ( statusRect . x + 6 , statusRect . y + 4 , 12 , 12 ) ;
231- Vector3 center = new ( dotRect . x + dotRect . width / 2 , dotRect . y + dotRect . height / 2 , 0 ) ;
183+ Vector3 center = new (
184+ dotRect . x + ( dotRect . width / 2 ) ,
185+ dotRect . y + ( dotRect . height / 2 ) ,
186+ 0
187+ ) ;
232188 float radius = dotRect . width / 2 ;
233189
234190 // Draw the main dot
@@ -263,13 +219,13 @@ private void OnGUI()
263219 ) ;
264220 EditorGUILayout . Space ( 10 ) ;
265221
266- // Python Server Status Section
222+ // Python Server Installation Status Section
267223 EditorGUILayout . BeginVertical ( EditorStyles . helpBox ) ;
268224 EditorGUILayout . LabelField ( "Python Server Status" , EditorStyles . boldLabel ) ;
269225
270226 // Status indicator with colored dot
271- var statusRect = EditorGUILayout . BeginHorizontal ( GUILayout . Height ( 20 ) ) ;
272- DrawStatusDot ( statusRect , pythonServerInstallationStatusColor ) ;
227+ Rect installStatusRect = EditorGUILayout . BeginHorizontal ( GUILayout . Height ( 20 ) ) ;
228+ DrawStatusDot ( installStatusRect , pythonServerInstallationStatusColor ) ;
273229 EditorGUILayout . LabelField ( " " + pythonServerInstallationStatus ) ;
274230 EditorGUILayout . EndHorizontal ( ) ;
275231
@@ -321,13 +277,13 @@ private void ToggleUnityBridge()
321277 private string WriteToConfig ( string pythonDir , string configPath )
322278 {
323279 // Create configuration object for unityMCP
324- var unityMCPConfig = new McpConfigServer
280+ McpConfigServer unityMCPConfig = new ( )
325281 {
326282 command = "uv" ,
327283 args = new [ ] { "--directory" , pythonDir , "run" , "server.py" } ,
328284 } ;
329285
330- var jsonSettings = new JsonSerializerSettings { Formatting = Formatting . Indented } ;
286+ JsonSerializerSettings jsonSettings = new ( ) { Formatting = Formatting . Indented } ;
331287
332288 // Read existing config if it exists
333289 string existingJson = "{}" ;
@@ -339,16 +295,13 @@ private string WriteToConfig(string pythonDir, string configPath)
339295 }
340296 catch ( Exception e )
341297 {
342- UnityEngine . Debug . LogWarning ( $ "Error reading existing config: { e . Message } .") ;
298+ Debug . LogWarning ( $ "Error reading existing config: { e . Message } .") ;
343299 }
344300 }
345301
346302 // Parse the existing JSON while preserving all properties
347303 dynamic existingConfig = JsonConvert . DeserializeObject ( existingJson ) ;
348- if ( existingConfig == null )
349- {
350- existingConfig = new Newtonsoft . Json . Linq . JObject ( ) ;
351- }
304+ existingConfig ??= new Newtonsoft . Json . Linq . JObject ( ) ;
352305
353306 // Ensure mcpServers object exists
354307 if ( existingConfig . mcpServers == null )
@@ -383,7 +336,7 @@ private void ShowManualInstructionsWindow(string configPath, McpClient mcpClient
383336 string pythonDir = FindPackagePythonDirectory ( ) ;
384337
385338 // Create the manual configuration message
386- var jsonConfig = new McpConfig
339+ McpConfig jsonConfig = new ( )
387340 {
388341 mcpServers = new McpConfigServers
389342 {
@@ -395,25 +348,26 @@ private void ShowManualInstructionsWindow(string configPath, McpClient mcpClient
395348 } ,
396349 } ;
397350
398- var jsonSettings = new JsonSerializerSettings { Formatting = Formatting . Indented } ;
351+ JsonSerializerSettings jsonSettings = new ( ) { Formatting = Formatting . Indented } ;
399352 string manualConfigJson = JsonConvert . SerializeObject ( jsonConfig , jsonSettings ) ;
400353
401354 ManualConfigEditorWindow . ShowWindow ( configPath , manualConfigJson , mcpClient ) ;
402355 }
403356
404357 private string FindPackagePythonDirectory ( )
405358 {
406- string pythonDir = "/path/to/your/unity-mcp/Python" ;
359+ string pythonDir = ServerInstaller . GetServerPath ( ) ;
407360
408361 try
409362 {
410363 // Try to find the package using Package Manager API
411- var request = UnityEditor . PackageManager . Client . List ( ) ;
364+ UnityEditor . PackageManager . Requests . ListRequest request =
365+ UnityEditor . PackageManager . Client . List ( ) ;
412366 while ( ! request . IsCompleted ) { } // Wait for the request to complete
413367
414368 if ( request . Status == UnityEditor . PackageManager . StatusCode . Success )
415369 {
416- foreach ( var package in request . Result )
370+ foreach ( UnityEditor . PackageManager . PackageInfo package in request . Result )
417371 {
418372 if ( package . name == "com.justinpbarnett.unity-mcp" )
419373 {
@@ -432,7 +386,7 @@ private string FindPackagePythonDirectory()
432386 }
433387 else if ( request . Error != null )
434388 {
435- UnityEngine . Debug . LogError ( "Failed to list packages: " + request . Error . message ) ;
389+ Debug . LogError ( "Failed to list packages: " + request . Error . message ) ;
436390 }
437391
438392 // If not found via Package Manager, try manual approaches
@@ -442,7 +396,7 @@ private string FindPackagePythonDirectory()
442396 Path . GetFullPath ( Path . Combine ( Application . dataPath , "unity-mcp" , "Python" ) ) ,
443397 } ;
444398
445- foreach ( var dir in possibleDirs )
399+ foreach ( string dir in possibleDirs )
446400 {
447401 if ( Directory . Exists ( dir ) && File . Exists ( Path . Combine ( dir , "server.py" ) ) )
448402 {
@@ -451,13 +405,11 @@ private string FindPackagePythonDirectory()
451405 }
452406
453407 // If still not found, return the placeholder path
454- UnityEngine . Debug . LogWarning (
455- "Could not find Python directory, using placeholder path"
456- ) ;
408+ Debug . LogWarning ( "Could not find Python directory, using placeholder path" ) ;
457409 }
458410 catch ( Exception e )
459411 {
460- UnityEngine . Debug . LogError ( $ "Error finding package path: { e . Message } ") ;
412+ Debug . LogError ( $ "Error finding package path: { e . Message } ") ;
461413 }
462414
463415 return pythonDir ;
@@ -525,7 +477,7 @@ private string ConfigureMcpClient(McpClient mcpClient)
525477 }
526478
527479 ShowManualInstructionsWindow ( configPath , mcpClient ) ;
528- UnityEngine . Debug . LogError (
480+ Debug . LogError (
529481 $ "Failed to configure { mcpClient . name } : { e . Message } \n { e . StackTrace } "
530482 ) ;
531483 return $ "Failed to configure { mcpClient . name } ";
@@ -543,7 +495,7 @@ McpClient mcpClient
543495 string pythonDir = FindPackagePythonDirectory ( ) ;
544496
545497 // Create the manual configuration message
546- var jsonConfig = new McpConfig
498+ McpConfig jsonConfig = new ( )
547499 {
548500 mcpServers = new McpConfigServers
549501 {
@@ -555,7 +507,7 @@ McpClient mcpClient
555507 } ,
556508 } ;
557509
558- var jsonSettings = new JsonSerializerSettings { Formatting = Formatting . Indented } ;
510+ JsonSerializerSettings jsonSettings = new ( ) { Formatting = Formatting . Indented } ;
559511 string manualConfigJson = JsonConvert . SerializeObject ( jsonConfig , jsonSettings ) ;
560512
561513 ManualConfigEditorWindow . ShowWindow ( configPath , manualConfigJson , mcpClient ) ;
@@ -590,7 +542,7 @@ private void CheckMcpConfiguration(McpClient mcpClient)
590542 }
591543
592544 string configJson = File . ReadAllText ( configPath ) ;
593- var config = JsonConvert . DeserializeObject < McpConfig > ( configJson ) ;
545+ McpConfig config = JsonConvert . DeserializeObject < McpConfig > ( configJson ) ;
594546
595547 if ( config ? . mcpServers ? . unityMCP != null )
596548 {
0 commit comments