@@ -10,43 +10,65 @@ public class MyBuildPostprocessor
1010 [ PostProcessBuild ]
1111 public static void OnPostprocessBuild ( BuildTarget target , string pathToBuiltProject )
1212 {
13- if ( target == BuildTarget . iOS )
13+ if ( target == BuildTarget . iOS || target == BuildTarget . tvOS )
1414 OnPostprocessBuildIOS ( pathToBuiltProject ) ;
1515 }
1616
1717 private static void OnPostprocessBuildIOS ( string pathToBuiltProject )
1818 {
19- // We use UnityEditor.iOS.Xcode API which only exists in iOS editor module
20- #if UNITY_IOS
21-
19+ // We use UnityEditor.iOS.Xcode API which only exists in iOS editor module
20+ #if UNITY_IOS || UNITY_TVOS
2221 string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj" ;
2322
2423 UnityEditor . iOS . Xcode . PBXProject proj = new UnityEditor . iOS . Xcode . PBXProject ( ) ;
2524 proj . ReadFromString ( File . ReadAllText ( projPath ) ) ;
26- string target = proj . TargetGuidByName ( "Unity-iPhone" ) ;
2725
28- Directory . CreateDirectory ( Path . Combine ( pathToBuiltProject , "Libraries/Unity" ) ) ;
26+ //2019.3
27+ string target = proj . GetUnityFrameworkTargetGuid ( ) ;
2928
30- string [ ] filesToCopy = new string [ ]
31- {
32- "PlatformBase.h" ,
33- "RenderAPI_Metal.mm" ,
34- "RenderAPI_OpenGLCoreES.cpp" ,
35- "RenderAPI.cpp" ,
36- "RenderAPI.h" ,
37- "RenderingPlugin.cpp" ,
29+ string [ ] filesToCopy = new string [ ] {
30+ "PlatformBase.h" , "RenderingPlugin.cpp" ,
31+ "RenderAPI_Metal.mm" , "RenderAPI_OpenGLCoreES.cpp" , "RenderAPI.cpp" , "RenderAPI.h" ,
3832 } ;
39-
4033 for ( int i = 0 ; i < filesToCopy . Length ; ++ i )
4134 {
42- var srcPath = Path . Combine ( "../PluginSource/source" , filesToCopy [ i ] ) ;
43- var dstLocalPath = "Libraries/" + filesToCopy [ i ] ;
44- var dstPath = Path . Combine ( pathToBuiltProject , dstLocalPath ) ;
35+ string srcPath = Path . Combine ( "../PluginSource/source" , filesToCopy [ i ] ) ;
36+ string dstLocalPath = "Libraries/" + filesToCopy [ i ] ;
37+ string dstPath = Path . Combine ( pathToBuiltProject , dstLocalPath ) ;
4538 File . Copy ( srcPath , dstPath , true ) ;
4639 proj . AddFileToBuild ( target , proj . AddFile ( dstLocalPath , dstLocalPath ) ) ;
4740 }
4841
42+ // on ios/tvos we need to register native plugins in trampoline
43+ // as the code needed for that is minuscle we just codegen it
44+ string registerCode = @"
45+ #import ""UnityAppController.h""
46+ #include ""Unity/IUnityGraphics.h""
47+
48+ extern ""C"" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
49+ extern ""C"" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
50+
51+ @interface MyAppController : UnityAppController
52+ {
53+ }
54+ - (void)shouldAttachRenderDelegate;
55+ @end
56+ @implementation MyAppController
57+ - (void)shouldAttachRenderDelegate
58+ {
59+ // unlike desktops where plugin dynamic library is automatically loaded and registered
60+ // we need to do that manually on iOS
61+ UnityRegisterRenderingPluginV5(&UnityPluginLoad, &UnityPluginUnload);
62+ }
63+
64+ @end
65+ IMPL_APP_CONTROLLER_SUBCLASS(MyAppController);
66+ " ;
67+ const string registerPath = "Libraries/RegisterPlugin.mm" ;
68+ File . WriteAllText ( Path . Combine ( pathToBuiltProject , registerPath ) , registerCode ) ;
69+ proj . AddFileToBuild ( target , proj . AddFile ( registerPath , registerPath ) ) ;
70+
4971 File . WriteAllText ( projPath , proj . WriteToString ( ) ) ;
50- #endif // #if UNITY_IOS
72+ #endif
5173 }
5274}
0 commit comments