diff --git a/unity/upms/python/Editor/CopyPuertsPythonRuntimePostBuild.cs b/unity/upms/python/Editor/CopyPuertsPythonRuntimePostBuild.cs new file mode 100644 index 0000000000..858b776172 --- /dev/null +++ b/unity/upms/python/Editor/CopyPuertsPythonRuntimePostBuild.cs @@ -0,0 +1,92 @@ +#if UNITY_EDITOR +using System; +using System.IO; +using UnityEditor; +using UnityEditor.Callbacks; +using UnityEngine; + +public static class CopyPuertsPythonRuntimePostBuild +{ + [PostProcessBuild(999)] + public static void OnPostprocessBuild(BuildTarget target, string buildPath) + { + string sourceRelativePath = null; + string destinationRelativePath = null; + + + switch (target) + { + case BuildTarget.StandaloneOSX: + // TODO + break; + case BuildTarget.StandaloneWindows64: + sourceRelativePath = "Plugins/x86_64"; + destinationRelativePath = "Plugins/x86_64"; + break; + default: + break; + } + + if (sourceRelativePath == null || destinationRelativePath == null) + { + Debug.Log($"[PuertsPythonCopy] Skip target={target}, no configuration for this platform."); + return; + } + + var productName = Path.GetFileNameWithoutExtension(buildPath); + + const string packageName = "com.tencent.puerts.python"; + var pi = UnityEditor.PackageManager.PackageInfo.FindForPackageName(packageName); + string packagePath = null; + if (pi != null && !string.IsNullOrEmpty(pi.resolvedPath)) + { + packagePath = pi.resolvedPath; + } + + try + { + if (string.IsNullOrEmpty(packagePath)) + { + Debug.LogWarning($"[PuertsPythonCopy] Package not found: {packagePath}"); + return; + } + + var sourceDir = Path.GetFullPath(Path.Combine(packagePath, sourceRelativePath)); + if (!Directory.Exists(sourceDir)) + { + Debug.LogWarning($"[PuertsPythonCopy] Source directory not found: {sourceDir}"); + return; + } + + var targetDir = Path.Combine(Path.GetDirectoryName(buildPath), $"{productName}_Data", destinationRelativePath); + + + switch (target) + { + case BuildTarget.StandaloneOSX: + // TODO + break; + case BuildTarget.StandaloneWindows64: + string[] patterns = new[] { "LICENSE.txt", "*.pyd", "python.cat", "python.exe", "pythonw.exe", "python3*._pth", "python3*.zip" }; + foreach (string pattern in patterns) + { + foreach (var file in Directory.GetFiles(sourceDir, pattern)) + { + var destFile = Path.Combine(targetDir, Path.GetFileName(file)); + File.Copy(file, destFile, true); + } + } + break; + default: + break; + } + + + } + catch (Exception ex) + { + Debug.LogError("[PuertsPythonCopy] Failed: " + ex); + } + } +} +#endif \ No newline at end of file diff --git a/unity/upms/python/Runtime/Src/Backends/BackendPython.cs b/unity/upms/python/Runtime/Src/Backends/BackendPython.cs index 5724ee8046..df69ef746a 100644 --- a/unity/upms/python/Runtime/Src/Backends/BackendPython.cs +++ b/unity/upms/python/Runtime/Src/Backends/BackendPython.cs @@ -38,7 +38,35 @@ public override IntPtr CreateEnvRef() var pythonPrefix = System.IO.Path.Combine(AppContext.BaseDirectory, "runtimes", GetRuntimeIdentifier(), "native"); PapiPythonNative.InitPythonByHome(pythonPrefix); #endif - envRef = PapiPythonNative.CreatePythonPapiEnvRef(); + +#if UNITY_STANDALONE_WIN && !UNITY_EDITOR + var pythonPrefix = System.IO.Path.Combine(UnityEngine.Application.dataPath, "Plugins\\x86_64"); + PapiPythonNative.InitPythonByHome(pythonPrefix); +#endif +#if UNITY_EDITOR + const string packageName = "com.tencent.puerts.python"; + var pi = UnityEditor.PackageManager.PackageInfo.FindForPackageName(packageName); + string packagePath = null; + if (pi != null && !string.IsNullOrEmpty(pi.resolvedPath)) + { + packagePath = pi.resolvedPath; + } + + string pythonHomeRelativePath = null; + if (packagePath != null) + { +#if UNITY_EDITOR_WIN + pythonHomeRelativePath = "Plugins/x86_64"; + var home = System.IO.Path.GetFullPath(System.IO.Path.Combine(packagePath, pythonHomeRelativePath)); + PapiPythonNative.InitPythonByHome(home); +#endif + } + else + { + UnityEngine.Debug.LogWarning($"Package {packageName} not found. Python initialization may fail if the Python runtime is not in the system PATH."); + } +#endif + envRef = PapiPythonNative.CreatePythonPapiEnvRef(); return envRef; }