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
0 commit comments