Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions unity/upms/python/Editor/CopyPuertsPythonRuntimePostBuild.cs
Original file line number Diff line number Diff line change
@@ -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
30 changes: 29 additions & 1 deletion unity/upms/python/Runtime/Src/Backends/BackendPython.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading