@@ -17,23 +17,15 @@ public class McpUnitySettings
1717 public const string PackageName = "com.gamelovers.mcp-unity" ;
1818 public const int RequestTimeoutMinimum = 10 ;
1919
20- #if UNITY_EDITOR_WIN
2120 private const string EnvUnityPort = "UNITY_PORT" ;
2221 private const string EnvUnityRequestTimeout = "UNITY_REQUEST_TIMEOUT" ;
23- #endif
22+ private const string SettingsPath = "ProjectSettings/McpUnitySettings.json" ;
2423
2524 private static McpUnitySettings _instance ;
26- private static readonly string SettingsPath = "ProjectSettings/McpUnitySettings.json" ;
2725
2826 // Server settings
29- #if ! UNITY_EDITOR_WIN
30- [ field: SerializeField ] // Note: On Windows, this property is persisted in per-user environment variables.
31- #endif
3227 public int Port { get ; set ; } = 8090 ;
3328
34- #if ! UNITY_EDITOR_WIN
35- [ field: SerializeField ] // Note: On Windows, this property is persisted in per-user environment variables.
36- #endif
3729 [ Tooltip ( "Timeout in seconds for tool request" ) ]
3830 public int RequestTimeoutSeconds { get ; set ; } = RequestTimeoutMinimum ;
3931
@@ -81,7 +73,6 @@ public void LoadSettings()
8173 JsonUtility . FromJsonOverwrite ( json , this ) ;
8274 }
8375
84- #if UNITY_EDITOR_WIN
8576 // Check for environment variable PORT
8677 string envPort = System . Environment . GetEnvironmentVariable ( EnvUnityPort ) ;
8778 if ( ! string . IsNullOrEmpty ( envPort ) && int . TryParse ( envPort , out int port ) )
@@ -93,7 +84,6 @@ public void LoadSettings()
9384 {
9485 RequestTimeoutSeconds = timeout ;
9586 }
96- #endif
9787 }
9888 catch ( Exception ex )
9989 {
@@ -113,14 +103,13 @@ public void SaveSettings()
113103 string json = JsonUtility . ToJson ( this , true ) ;
114104 File . WriteAllText ( SettingsPath , json ) ;
115105
116- #if UNITY_EDITOR_WIN
117106 // Set environment variable PORT for the Node.js process
118- // Note: This will only affect processes started after this point
119- // Note: EnvironmentVariableTarget.User should be used on .NET implementations running on Windows systems only .
120- // see: https://learn.microsoft.com/ja-jp/dotnet/api/system.environmentvariabletarget?view=net-8.0#fields
121- Environment . SetEnvironmentVariable ( EnvUnityPort , Port . ToString ( ) , EnvironmentVariableTarget . User ) ;
122- Environment . SetEnvironmentVariable ( EnvUnityRequestTimeout , RequestTimeoutSeconds . ToString ( ) , EnvironmentVariableTarget . User ) ;
123- #endif
107+ // EnvironmentVariableTarget.User and EnvironmentVariableTarget.Machine should be used on .NET implementations running on Windows systems only.
108+ // For non-Windows systems, User and Machine are treated as Process .
109+ // Using Process target for broader compatibility.
110+ // see: https://learn.microsoft.com/en-us/dotnet/api/system.environmentvariabletarget?view=net-8.0#remarks
111+ Environment . SetEnvironmentVariable ( EnvUnityPort , Port . ToString ( ) , EnvironmentVariableTarget . Process ) ;
112+ Environment . SetEnvironmentVariable ( EnvUnityRequestTimeout , RequestTimeoutSeconds . ToString ( ) , EnvironmentVariableTarget . Process ) ;
124113 }
125114 catch ( Exception ex )
126115 {
0 commit comments