diff --git a/UnityMcpBridge/Editor/Windows/UnityMcpEditorWindow.cs b/UnityMcpBridge/Editor/Windows/UnityMcpEditorWindow.cs index d7128eb..c794fb7 100644 --- a/UnityMcpBridge/Editor/Windows/UnityMcpEditorWindow.cs +++ b/UnityMcpBridge/Editor/Windows/UnityMcpEditorWindow.cs @@ -1,13 +1,6 @@ using System; -using System.Collections.Generic; using System.IO; -using System.Linq; -using System.Net; -using System.Net.NetworkInformation; -using System.Net.Sockets; using System.Runtime.InteropServices; -using System.Text; -using System.Threading.Tasks; using Newtonsoft.Json; using UnityEditor; using UnityEngine; @@ -23,9 +16,6 @@ namespace UnityMcpBridge.Editor.Windows private Vector2 scrollPosition; private string pythonServerInstallationStatus = "Not Installed"; private Color pythonServerInstallationStatusColor = Color.red; - private string pythonServerConnectionStatus = "Not Connected"; - private Color pythonServerConnectionStatusColor = Color.red; - private DateTime lastConnectionCheck; private const int unityPort = 6400; // Hardcoded Unity port private const int mcpPort = 6500; // Hardcoded MCP port private readonly McpClients mcpClients = new(); @@ -39,7 +29,6 @@ namespace UnityMcpBridge.Editor.Windows private void OnEnable() { UpdatePythonServerInstallationStatus(); - UpdatePythonServerConnectionStatus(); isUnityBridgeRunning = UnityMcpBridge.IsRunning; foreach (McpClient mcpClient in mcpClients.clients) @@ -48,16 +37,6 @@ namespace UnityMcpBridge.Editor.Windows } } - private void Update() - { - if (lastConnectionCheck.AddSeconds(2) < DateTime.Now) - { - UpdatePythonServerConnectionStatus(); - - lastConnectionCheck = DateTime.Now; - } - } - private Color GetStatusColor(McpStatus status) { // Return appropriate color based on the status enum @@ -84,13 +63,13 @@ namespace UnityMcpBridge.Editor.Windows if (ServerInstaller.IsNewerVersion(latestVersion, installedVersion)) { - pythonServerInstallationStatus = "Up to Date"; - pythonServerInstallationStatusColor = Color.green; + pythonServerInstallationStatus = "Newer Version Available"; + pythonServerInstallationStatusColor = Color.yellow; } else { - pythonServerInstallationStatus = "Newer Version Available"; - pythonServerInstallationStatusColor = Color.yellow; + pythonServerInstallationStatus = "Up to Date"; + pythonServerInstallationStatusColor = Color.green; } } else @@ -100,41 +79,12 @@ namespace UnityMcpBridge.Editor.Windows } } - private void UpdatePythonServerConnectionStatus() - { - IPGlobalProperties ipGlobalProperties = IPGlobalProperties.GetIPGlobalProperties(); - TcpConnectionInformation[] tcpConnections = - ipGlobalProperties.GetActiveTcpConnections(); - IPEndPoint[] tcpListeners = ipGlobalProperties.GetActiveTcpListeners(); - - // Check if the port is in use by any active listener - bool isListenerActive = tcpListeners.Any(static endpoint => endpoint.Port == mcpPort); - - // Optionally, check if the port is in use by any active connection - bool isConnectionActive = tcpConnections.Any(static connection => - connection.LocalEndPoint.Port == mcpPort - || connection.RemoteEndPoint.Port == mcpPort - ); - - // Return true if either a listener or connection is using the port - if (isListenerActive || isConnectionActive) - { - pythonServerConnectionStatus = "Connected"; - pythonServerConnectionStatusColor = Color.green; - } - else - { - pythonServerConnectionStatus = "Not Connected"; - pythonServerConnectionStatusColor = Color.red; - } - } - private void ConfigurationSection(McpClient mcpClient) { // Calculate if we should use half-width layout // Minimum width for half-width layout is 400 pixels bool useHalfWidth = position.width >= 800; - float sectionWidth = useHalfWidth ? position.width / 2 - 15 : position.width - 20; + float sectionWidth = useHalfWidth ? (position.width / 2) - 15 : position.width - 20; // Begin horizontal layout if using half-width if (useHalfWidth && mcpClients.clients.IndexOf(mcpClient) % 2 == 0) @@ -178,9 +128,11 @@ namespace UnityMcpBridge.Editor.Windows EditorGUILayout.Space(8); // Configure button with improved styling - GUIStyle buttonStyle = new(GUI.skin.button); - buttonStyle.padding = new RectOffset(15, 15, 5, 5); - buttonStyle.margin = new RectOffset(10, 10, 5, 5); + GUIStyle buttonStyle = new(GUI.skin.button) + { + padding = new RectOffset(15, 15, 5, 5), + margin = new RectOffset(10, 10, 5, 5), + }; // Create muted button style for Manual Setup GUIStyle mutedButtonStyle = new(buttonStyle); @@ -228,7 +180,11 @@ namespace UnityMcpBridge.Editor.Windows private void DrawStatusDot(Rect statusRect, Color statusColor) { Rect dotRect = new(statusRect.x + 6, statusRect.y + 4, 12, 12); - Vector3 center = new(dotRect.x + dotRect.width / 2, dotRect.y + dotRect.height / 2, 0); + Vector3 center = new( + dotRect.x + (dotRect.width / 2), + dotRect.y + (dotRect.height / 2), + 0 + ); float radius = dotRect.width / 2; // Draw the main dot @@ -263,13 +219,13 @@ namespace UnityMcpBridge.Editor.Windows ); EditorGUILayout.Space(10); - // Python Server Status Section + // Python Server Installation Status Section EditorGUILayout.BeginVertical(EditorStyles.helpBox); EditorGUILayout.LabelField("Python Server Status", EditorStyles.boldLabel); // Status indicator with colored dot - var statusRect = EditorGUILayout.BeginHorizontal(GUILayout.Height(20)); - DrawStatusDot(statusRect, pythonServerInstallationStatusColor); + Rect installStatusRect = EditorGUILayout.BeginHorizontal(GUILayout.Height(20)); + DrawStatusDot(installStatusRect, pythonServerInstallationStatusColor); EditorGUILayout.LabelField(" " + pythonServerInstallationStatus); EditorGUILayout.EndHorizontal(); @@ -321,13 +277,13 @@ namespace UnityMcpBridge.Editor.Windows private string WriteToConfig(string pythonDir, string configPath) { // Create configuration object for unityMCP - var unityMCPConfig = new McpConfigServer + McpConfigServer unityMCPConfig = new() { command = "uv", args = new[] { "--directory", pythonDir, "run", "server.py" }, }; - var jsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented }; + JsonSerializerSettings jsonSettings = new() { Formatting = Formatting.Indented }; // Read existing config if it exists string existingJson = "{}"; @@ -339,16 +295,13 @@ namespace UnityMcpBridge.Editor.Windows } catch (Exception e) { - UnityEngine.Debug.LogWarning($"Error reading existing config: {e.Message}."); + Debug.LogWarning($"Error reading existing config: {e.Message}."); } } // Parse the existing JSON while preserving all properties dynamic existingConfig = JsonConvert.DeserializeObject(existingJson); - if (existingConfig == null) - { - existingConfig = new Newtonsoft.Json.Linq.JObject(); - } + existingConfig ??= new Newtonsoft.Json.Linq.JObject(); // Ensure mcpServers object exists if (existingConfig.mcpServers == null) @@ -383,7 +336,7 @@ namespace UnityMcpBridge.Editor.Windows string pythonDir = FindPackagePythonDirectory(); // Create the manual configuration message - var jsonConfig = new McpConfig + McpConfig jsonConfig = new() { mcpServers = new McpConfigServers { @@ -395,7 +348,7 @@ namespace UnityMcpBridge.Editor.Windows }, }; - var jsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented }; + JsonSerializerSettings jsonSettings = new() { Formatting = Formatting.Indented }; string manualConfigJson = JsonConvert.SerializeObject(jsonConfig, jsonSettings); ManualConfigEditorWindow.ShowWindow(configPath, manualConfigJson, mcpClient); @@ -403,17 +356,18 @@ namespace UnityMcpBridge.Editor.Windows private string FindPackagePythonDirectory() { - string pythonDir = "/path/to/your/unity-mcp/Python"; + string pythonDir = ServerInstaller.GetServerPath(); try { // Try to find the package using Package Manager API - var request = UnityEditor.PackageManager.Client.List(); + UnityEditor.PackageManager.Requests.ListRequest request = + UnityEditor.PackageManager.Client.List(); while (!request.IsCompleted) { } // Wait for the request to complete if (request.Status == UnityEditor.PackageManager.StatusCode.Success) { - foreach (var package in request.Result) + foreach (UnityEditor.PackageManager.PackageInfo package in request.Result) { if (package.name == "com.justinpbarnett.unity-mcp") { @@ -432,7 +386,7 @@ namespace UnityMcpBridge.Editor.Windows } else if (request.Error != null) { - UnityEngine.Debug.LogError("Failed to list packages: " + request.Error.message); + Debug.LogError("Failed to list packages: " + request.Error.message); } // If not found via Package Manager, try manual approaches @@ -442,7 +396,7 @@ namespace UnityMcpBridge.Editor.Windows Path.GetFullPath(Path.Combine(Application.dataPath, "unity-mcp", "Python")), }; - foreach (var dir in possibleDirs) + foreach (string dir in possibleDirs) { if (Directory.Exists(dir) && File.Exists(Path.Combine(dir, "server.py"))) { @@ -451,13 +405,11 @@ namespace UnityMcpBridge.Editor.Windows } // If still not found, return the placeholder path - UnityEngine.Debug.LogWarning( - "Could not find Python directory, using placeholder path" - ); + Debug.LogWarning("Could not find Python directory, using placeholder path"); } catch (Exception e) { - UnityEngine.Debug.LogError($"Error finding package path: {e.Message}"); + Debug.LogError($"Error finding package path: {e.Message}"); } return pythonDir; @@ -525,7 +477,7 @@ namespace UnityMcpBridge.Editor.Windows } ShowManualInstructionsWindow(configPath, mcpClient); - UnityEngine.Debug.LogError( + Debug.LogError( $"Failed to configure {mcpClient.name}: {e.Message}\n{e.StackTrace}" ); return $"Failed to configure {mcpClient.name}"; @@ -543,7 +495,7 @@ namespace UnityMcpBridge.Editor.Windows string pythonDir = FindPackagePythonDirectory(); // Create the manual configuration message - var jsonConfig = new McpConfig + McpConfig jsonConfig = new() { mcpServers = new McpConfigServers { @@ -555,7 +507,7 @@ namespace UnityMcpBridge.Editor.Windows }, }; - var jsonSettings = new JsonSerializerSettings { Formatting = Formatting.Indented }; + JsonSerializerSettings jsonSettings = new() { Formatting = Formatting.Indented }; string manualConfigJson = JsonConvert.SerializeObject(jsonConfig, jsonSettings); ManualConfigEditorWindow.ShowWindow(configPath, manualConfigJson, mcpClient); @@ -590,7 +542,7 @@ namespace UnityMcpBridge.Editor.Windows } string configJson = File.ReadAllText(configPath); - var config = JsonConvert.DeserializeObject(configJson); + McpConfig config = JsonConvert.DeserializeObject(configJson); if (config?.mcpServers?.unityMCP != null) {