2025-08-08 10:06:12 +08:00
using UnityEditor ;
using UnityEngine ;
2025-08-21 03:59:49 +08:00
namespace MCPForUnity.Editor.Helpers
2025-08-08 10:06:12 +08:00
{
/// <summary>
/// Handles automatic installation of the Python server when the package is first installed.
/// </summary>
[InitializeOnLoad]
public static class PackageInstaller
{
2025-08-21 03:59:49 +08:00
private const string InstallationFlagKey = "MCPForUnity.ServerInstalled" ;
2025-08-08 10:06:12 +08:00
static PackageInstaller ( )
{
// Check if this is the first time the package is loaded
if ( ! EditorPrefs . GetBool ( InstallationFlagKey , false ) )
{
// Schedule the installation for after Unity is fully loaded
EditorApplication . delayCall + = InstallServerOnFirstLoad ;
}
}
private static void InstallServerOnFirstLoad ( )
{
try
{
2025-08-21 03:59:49 +08:00
Debug . Log ( "<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: Installing Python server..." ) ;
2025-08-08 10:06:12 +08:00
ServerInstaller . EnsureServerInstalled ( ) ;
// Mark as installed
EditorPrefs . SetBool ( InstallationFlagKey , true ) ;
2025-08-21 03:59:49 +08:00
Debug . Log ( "<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: Python server installation completed successfully." ) ;
2025-08-08 10:06:12 +08:00
}
catch ( System . Exception ex )
{
2025-08-21 03:59:49 +08:00
Debug . LogError ( $"<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: Failed to install Python server: {ex.Message}" ) ;
Debug . LogWarning ( "<b><color=#2EA3FF>MCP-FOR-UNITY</color></b>: You may need to manually install the Python server. Check the MCP for Unity Editor Window for instructions." ) ;
2025-08-08 10:06:12 +08:00
}
}
}
}