Skip to content

Commit 3a5d17a

Browse files
committed
Fix to persist WebSocket ports on non-Windows OS
1 parent 22cc678 commit 3a5d17a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Editor/UnityBridge/McpUnitySettings.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public class McpUnitySettings
2020
private static readonly string SettingsPath = "ProjectSettings/McpUnitySettings.json";
2121

2222
// Server settings
23+
#if !UNITY_EDITOR_WIN
24+
[field: SerializeField] // Note: On Windows, this property is persisted in per-user environment variables.
25+
#endif
2326
public int Port { get; set; } = 8090;
2427

2528
[Tooltip("Whether to automatically start the MCP server when Unity opens")]
@@ -69,12 +72,14 @@ public void LoadSettings()
6972
JsonUtility.FromJsonOverwrite(json, this);
7073
}
7174

75+
#if UNITY_EDITOR_WIN
7276
// Check for environment variable PORT
7377
string envPort = System.Environment.GetEnvironmentVariable("UNITY_PORT");
7478
if (!string.IsNullOrEmpty(envPort) && int.TryParse(envPort, out int port))
7579
{
7680
Port = port;
7781
}
82+
#endif
7883
}
7984
catch (Exception ex)
8085
{
@@ -93,10 +98,14 @@ public void SaveSettings()
9398
// Save settings to McpUnitySettings.json
9499
string json = JsonUtility.ToJson(this, true);
95100
File.WriteAllText(SettingsPath, json);
96-
101+
102+
#if UNITY_EDITOR_WIN
97103
// Set environment variable PORT for the Node.js process
98104
// Note: This will only affect processes started after this point
105+
// Note: EnvironmentVariableTarget.User should be used on .NET implementations running on Windows systems only.
106+
// see: https://learn.microsoft.com/ja-jp/dotnet/api/system.environmentvariabletarget?view=net-8.0#fields
99107
System.Environment.SetEnvironmentVariable("UNITY_PORT", Port.ToString(), System.EnvironmentVariableTarget.User);
108+
#endif
100109
}
101110
catch (Exception ex)
102111
{

0 commit comments

Comments
 (0)