Skip to content

Commit 72c73e9

Browse files
authored
Merge pull request Unity-Technologies#3 from Unity-Technologies/ios
ios/tvos works (simulator included)
2 parents 0dde215 + ac76221 commit 72c73e9

File tree

9 files changed

+96
-41
lines changed

9 files changed

+96
-41
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>

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@ Native code rendering is implemented for several platforms and graphics APIs:
2929
* Windows Store aka UWP (D3D11, D3D12)
3030
* WebGL (OpenGL ES)
3131
* Android (OpenGL ES, Vulkan)
32-
* ...more platforms (iOS, ...) coming soon, we just did not get around to adding project files yet.
32+
* iOS/tvOS (Metal; Simulator is supported if you use unity 2020+ and XCode 11+)
33+
* ...more platforms might be coming soon, we just did not get around to adding project files yet.
3334

3435
Code is organized as follows:
3536

UnityProject/Assets/Editor/MyBuildPostprocessor.cs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,39 @@ 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+
#if UNITY_2019_3_OR_NEWER
27+
string target = proj.GetUnityFrameworkTargetGuid();
28+
#else
29+
string target = proj.TargetGuidByName("Unity-iPhone");
30+
#endif
2931

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",
32+
string[] filesToCopy = new string[] {
33+
"PlatformBase.h", "RenderingPlugin.cpp",
34+
"RenderAPI_Metal.mm", "RenderAPI_OpenGLCoreES.cpp", "RenderAPI.cpp", "RenderAPI.h",
3835
};
39-
4036
for(int i = 0 ; i < filesToCopy.Length ; ++i)
4137
{
42-
var srcPath = Path.Combine("../PluginSource/source", filesToCopy[i]);
43-
var dstLocalPath = "Libraries/" + filesToCopy[i];
44-
var dstPath = Path.Combine(pathToBuiltProject, dstLocalPath);
38+
string srcPath = Path.Combine("../PluginSource/source", filesToCopy[i]);
39+
string dstLocalPath = "Libraries/" + filesToCopy[i];
40+
string dstPath = Path.Combine(pathToBuiltProject, dstLocalPath);
4541
File.Copy(srcPath, dstPath, true);
4642
proj.AddFileToBuild(target, proj.AddFile(dstLocalPath, dstLocalPath));
4743
}
4844

4945
File.WriteAllText(projPath, proj.WriteToString());
50-
#endif // #if UNITY_IOS
46+
#endif
5147
}
5248
}

UnityProject/Assets/Plugins/iOS.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#import "UnityAppController.h"
2+
#include "Unity/IUnityGraphics.h"
3+
4+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginLoad(IUnityInterfaces* unityInterfaces);
5+
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API UnityPluginUnload();
6+
7+
@interface MyAppController : UnityAppController
8+
{
9+
}
10+
- (void)shouldAttachRenderDelegate;
11+
@end
12+
@implementation MyAppController
13+
- (void)shouldAttachRenderDelegate
14+
{
15+
// unlike desktops where plugin dynamic library is automatically loaded and registered
16+
// we need to do that manually on iOS
17+
UnityRegisterRenderingPluginV5(&UnityPluginLoad, &UnityPluginUnload);
18+
}
19+
20+
@end
21+
IMPL_APP_CONTROLLER_SUBCLASS(MyAppController);

UnityProject/Assets/Plugins/iOS/RegisterPlugin.mm.meta

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnityProject/Assets/UseRenderingPlugin.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class UseRenderingPlugin : MonoBehaviour
1212
// For this example, we'll call into plugin's SetTimeFromUnity
1313
// function and pass the current time so the plugin can animate.
1414

15-
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
15+
#if (UNITY_IOS || UNITY_TVOS || UNITY_WEBGL) && !UNITY_EDITOR
1616
[DllImport ("__Internal")]
1717
#else
1818
[DllImport ("RenderingPlugin")]
@@ -22,7 +22,7 @@ public class UseRenderingPlugin : MonoBehaviour
2222

2323
// We'll also pass native pointer to a texture in Unity.
2424
// The plugin will fill texture data from native code.
25-
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
25+
#if (UNITY_IOS || UNITY_TVOS || UNITY_WEBGL) && !UNITY_EDITOR
2626
[DllImport ("__Internal")]
2727
#else
2828
[DllImport ("RenderingPlugin")]
@@ -32,14 +32,14 @@ public class UseRenderingPlugin : MonoBehaviour
3232
// We'll pass native pointer to the mesh vertex buffer.
3333
// Also passing source unmodified mesh data.
3434
// The plugin will fill vertex data from native code.
35-
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
35+
#if (UNITY_IOS || UNITY_TVOS || UNITY_WEBGL) && !UNITY_EDITOR
3636
[DllImport ("__Internal")]
3737
#else
3838
[DllImport ("RenderingPlugin")]
3939
#endif
4040
private static extern void SetMeshBuffersFromUnity (IntPtr vertexBuffer, int vertexCount, IntPtr sourceVertices, IntPtr sourceNormals, IntPtr sourceUVs);
4141

42-
#if (UNITY_IPHONE || UNITY_WEBGL) && !UNITY_EDITOR
42+
#if (UNITY_IOS || UNITY_TVOS || UNITY_WEBGL) && !UNITY_EDITOR
4343
[DllImport ("__Internal")]
4444
#else
4545
[DllImport("RenderingPlugin")]
@@ -90,7 +90,7 @@ private void SendMeshBuffersToPlugin ()
9090
// vertex layout (position+normal+color+UV). Since Unity 2019.3 it's easier
9191
// since there are APIs to query all that info.
9292
var colors = mesh.colors;
93-
mesh.colors = colors;
93+
mesh.colors = colors;
9494

9595
// However, mesh being dynamic also means that the CPU on most platforms can not
9696
// read from the vertex buffer. Our plugin also wants original mesh data,

0 commit comments

Comments
 (0)