@@ -20,6 +20,9 @@ public class McpUnitySettings
20
20
private static readonly string SettingsPath = "ProjectSettings/McpUnitySettings.json" ;
21
21
22
22
// Server settings
23
+ #if ! UNITY_EDITOR_WIN
24
+ [ field: SerializeField ] // Note: On Windows, this property is persisted in per-user environment variables.
25
+ #endif
23
26
public int Port { get ; set ; } = 8090 ;
24
27
25
28
[ Tooltip ( "Whether to automatically start the MCP server when Unity opens" ) ]
@@ -69,12 +72,14 @@ public void LoadSettings()
69
72
JsonUtility . FromJsonOverwrite ( json , this ) ;
70
73
}
71
74
75
+ #if UNITY_EDITOR_WIN
72
76
// Check for environment variable PORT
73
77
string envPort = System . Environment . GetEnvironmentVariable ( "UNITY_PORT" ) ;
74
78
if ( ! string . IsNullOrEmpty ( envPort ) && int . TryParse ( envPort , out int port ) )
75
79
{
76
80
Port = port ;
77
81
}
82
+ #endif
78
83
}
79
84
catch ( Exception ex )
80
85
{
@@ -93,10 +98,14 @@ public void SaveSettings()
93
98
// Save settings to McpUnitySettings.json
94
99
string json = JsonUtility . ToJson ( this , true ) ;
95
100
File . WriteAllText ( SettingsPath , json ) ;
96
-
101
+
102
+ #if UNITY_EDITOR_WIN
97
103
// Set environment variable PORT for the Node.js process
98
104
// 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
99
107
System . Environment . SetEnvironmentVariable ( "UNITY_PORT" , Port . ToString ( ) , System . EnvironmentVariableTarget . User ) ;
108
+ #endif
100
109
}
101
110
catch ( Exception ex )
102
111
{
0 commit comments