Skip to content

Commit 74abebc

Browse files
committed
python Unity Windows 打包 python
1 parent 7e89629 commit 74abebc

File tree

2 files changed

+121
-1
lines changed

2 files changed

+121
-1
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#if UNITY_EDITOR
2+
using System;
3+
using System.IO;
4+
using UnityEditor;
5+
using UnityEditor.Callbacks;
6+
using UnityEngine;
7+
8+
public static class CopyPuertsPythonRuntimePostBuild
9+
{
10+
[PostProcessBuild(999)]
11+
public static void OnPostprocessBuild(BuildTarget target, string buildPath)
12+
{
13+
string sourceRelativePath = null;
14+
string destinationRelativePath = null;
15+
16+
17+
switch (target)
18+
{
19+
case BuildTarget.StandaloneOSX:
20+
// TODO
21+
break;
22+
case BuildTarget.StandaloneWindows64:
23+
sourceRelativePath = "Plugins/x86_64";
24+
destinationRelativePath = "Plugins/x86_64";
25+
break;
26+
default:
27+
break;
28+
}
29+
30+
if (sourceRelativePath == null || destinationRelativePath == null)
31+
{
32+
Debug.Log($"[PuertsPythonCopy] Skip target={target}, no configuration for this platform.");
33+
return;
34+
}
35+
36+
var productName = Path.GetFileNameWithoutExtension(buildPath);
37+
38+
const string packageName = "com.tencent.puerts.python";
39+
var pi = UnityEditor.PackageManager.PackageInfo.FindForPackageName(packageName);
40+
string packagePath = null;
41+
if (pi != null && !string.IsNullOrEmpty(pi.resolvedPath))
42+
{
43+
packagePath = pi.resolvedPath;
44+
}
45+
46+
try
47+
{
48+
if (string.IsNullOrEmpty(packagePath))
49+
{
50+
Debug.LogWarning($"[PuertsPythonCopy] Package not found: {packagePath}");
51+
return;
52+
}
53+
54+
var sourceDir = Path.GetFullPath(Path.Combine(packagePath, sourceRelativePath));
55+
if (!Directory.Exists(sourceDir))
56+
{
57+
Debug.LogWarning($"[PuertsPythonCopy] Source directory not found: {sourceDir}");
58+
return;
59+
}
60+
61+
var targetDir = Path.Combine(Path.GetDirectoryName(buildPath), $"{productName}_Data", destinationRelativePath);
62+
63+
64+
switch (target)
65+
{
66+
case BuildTarget.StandaloneOSX:
67+
// TODO
68+
break;
69+
case BuildTarget.StandaloneWindows64:
70+
string[] patterns = new[] { "LICENSE.txt", "*.pyd", "python.cat", "python.exe", "pythonw.exe", "python3*._pth", "python3*.zip" };
71+
foreach (string pattern in patterns)
72+
{
73+
foreach (var file in Directory.GetFiles(sourceDir, pattern))
74+
{
75+
var destFile = Path.Combine(targetDir, Path.GetFileName(file));
76+
File.Copy(file, destFile, true);
77+
}
78+
}
79+
break;
80+
default:
81+
break;
82+
}
83+
84+
85+
}
86+
catch (Exception ex)
87+
{
88+
Debug.LogError("[PuertsPythonCopy] Failed: " + ex);
89+
}
90+
}
91+
}
92+
#endif

unity/upms/python/Runtime/Src/Backends/BackendPython.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,35 @@ public override IntPtr CreateEnvRef()
3838
var pythonPrefix = System.IO.Path.Combine(AppContext.BaseDirectory, "runtimes", GetRuntimeIdentifier(), "native");
3939
PapiPythonNative.InitPythonByHome(pythonPrefix);
4040
#endif
41-
envRef = PapiPythonNative.CreatePythonPapiEnvRef();
41+
42+
#if UNITY_STANDALONE_WIN && !UNITY_EDITOR
43+
var pythonPrefix = System.IO.Path.Combine(UnityEngine.Application.dataPath, "Plugins\\x86_64");
44+
PapiPythonNative.InitPythonByHome(pythonPrefix);
45+
#endif
46+
#if UNITY_EDITOR
47+
const string packageName = "com.tencent.puerts.python";
48+
var pi = UnityEditor.PackageManager.PackageInfo.FindForPackageName(packageName);
49+
string packagePath = null;
50+
if (pi != null && !string.IsNullOrEmpty(pi.resolvedPath))
51+
{
52+
packagePath = pi.resolvedPath;
53+
}
54+
55+
string pythonHomeRelativePath = null;
56+
if (packagePath != null)
57+
{
58+
#if UNITY_EDITOR_WIN
59+
pythonHomeRelativePath = "Plugins/x86_64";
60+
var home = System.IO.Path.GetFullPath(System.IO.Path.Combine(packagePath, pythonHomeRelativePath));
61+
PapiPythonNative.InitPythonByHome(home);
62+
#endif
63+
}
64+
else
65+
{
66+
UnityEngine.Debug.LogWarning($"Package {packageName} not found. Python initialization may fail if the Python runtime is not in the system PATH.");
67+
}
68+
#endif
69+
envRef = PapiPythonNative.CreatePythonPapiEnvRef();
4270
return envRef;
4371
}
4472

0 commit comments

Comments
 (0)