Skip to content

Commit bcb6f85

Browse files
committed
ios (even on simulator) now works
Fixed both plugin not being registered (so no plugin was happening) and metal omn simulator
1 parent 0dde215 commit bcb6f85

File tree

4 files changed

+53
-35
lines changed

4 files changed

+53
-35
lines changed

PluginSource/source/PlatformBase.h

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,18 @@
99
// UNITY_WIN - Windows (regular win32)
1010
// UNITY_OSX - Mac OS X
1111
// UNITY_LINUX - Linux
12-
// UNITY_IPHONE - iOS
12+
// UNITY_IOS - iOS
13+
// UNITY_TVOS - tvOS
1314
// UNITY_ANDROID - Android
1415
// UNITY_METRO - WSA or UWP
1516
// UNITY_WEBGL - WebGL
1617
#if _MSC_VER
1718
#define UNITY_WIN 1
1819
#elif defined(__APPLE__)
19-
#if defined(__arm__) || defined(__arm64__)
20-
#define UNITY_IPHONE 1
20+
#if TARGET_OS_TV
21+
#define UNITY_TVOS 1
22+
#elif TARGET_OS_IOS
23+
#define UNITY_IOS 1
2124
#else
2225
#define UNITY_OSX 1
2326
#endif
@@ -46,7 +49,7 @@
4649
#define SUPPORT_OPENGL_UNIFIED 1
4750
#define SUPPORT_OPENGL_CORE 1
4851
#define SUPPORT_VULKAN 0 // Requires Vulkan SDK to be installed
49-
#elif UNITY_IPHONE || UNITY_ANDROID || UNITY_WEBGL
52+
#elif UNITY_IOS || UNITY_TVOS || UNITY_ANDROID || UNITY_WEBGL
5053
#ifndef SUPPORT_OPENGL_ES
5154
#define SUPPORT_OPENGL_ES 1
5255
#endif
@@ -59,7 +62,7 @@
5962
#define SUPPORT_OPENGL_CORE 1
6063
#endif
6164

62-
#if UNITY_IPHONE || UNITY_OSX
65+
#if UNITY_IOS || UNITY_TVOS || UNITY_OSX
6366
#define SUPPORT_METAL 1
6467
#endif
6568

PluginSource/source/RenderAPI_Metal.mm

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,8 @@
139139
// Let's assume we're rendering into BGRA8Unorm...
140140
pipeDesc.colorAttachments[0].pixelFormat= MTLPixelFormatBGRA8Unorm;
141141

142-
// OSX/tvOS insist on more modern enums
143-
#if (UNITY_IPHONE && TARGET_OS_TV) || UNITY_OSX
144142
pipeDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
145143
pipeDesc.stencilAttachmentPixelFormat = MTLPixelFormatDepth32Float_Stencil8;
146-
#else
147-
pipeDesc.depthAttachmentPixelFormat = MTLPixelFormatDepth32Float;
148-
pipeDesc.stencilAttachmentPixelFormat = MTLPixelFormatStencil8;
149-
#endif
150144

151145
pipeDesc.sampleCount = 1;
152146
pipeDesc.colorAttachments[0].blendingEnabled = NO;
@@ -180,10 +174,9 @@
180174
if (type == kUnityGfxDeviceEventInitialize)
181175
{
182176
m_MetalGraphics = interfaces->Get<IUnityGraphicsMetal>();
183-
NSBundle* metalBundle = m_MetalGraphics->MetalBundle();
184-
MTLVertexDescriptorClass = [metalBundle classNamed:@"MTLVertexDescriptor"];
185-
MTLRenderPipelineDescriptorClass = [metalBundle classNamed:@"MTLRenderPipelineDescriptor"];
186-
MTLDepthStencilDescriptorClass = [metalBundle classNamed:@"MTLDepthStencilDescriptor"];
177+
MTLVertexDescriptorClass = NSClassFromString(@"MTLVertexDescriptor");
178+
MTLRenderPipelineDescriptorClass = NSClassFromString(@"MTLRenderPipelineDescriptor");
179+
MTLDepthStencilDescriptorClass = NSClassFromString(@"MTLDepthStencilDescriptor");
187180

188181
CreateResources();
189182
}

PluginSource/source/RenderAPI_OpenGLCoreES.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010

1111
#include <assert.h>
12-
#if UNITY_IPHONE
12+
#if UNITY_IOS || UNITY_TVOS
1313
# include <OpenGLES/ES2/gl.h>
1414
#elif UNITY_ANDROID || UNITY_WEBGL
1515
# include <GLES2/gl2.h>

UnityProject/Assets/Editor/MyBuildPostprocessor.cs

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)