SlideShare a Scribd company logo
Bridging the Gap – Plugin for Unity and iOS
There is always a need of finding out a way of communication from unity to XCode or XCode to unity
due to the absence of a direct way. For the first time when I faced this problem, had to spend almost
3-4 days of my app’s development cycle on finding the right way to make this bridge. Through this
blog post, I am going to share the method so as to help out any other developer like me. Creating a
bridge over unity and iOS requires coding at both ends. So, let’s discuss XCode side first.
According to Unity documentation, “Unity iOS supports automated plugin integration in a limited way.
All files with extensions .a,.m,.mm,.c,.cpp located in the Assets/Plugins/iOS folder will be merged into
the generated Xcode project automatically. However, merging is done by symlinking files from
Assets/Plugins/iOS to the final destination, which might affect some workflows. The.h files are not
included in the Xcode project tree, but they appear on the destination file system, thus allowing
compilation of .m/.mm/.c/.cpp files”.
So, we will create UnityIOSBridge.m and will place this class to the path “Assets/Plugins/iOS”.Unity
needs the plugins to be c named so it is a good practice to wrap up the methods which need to be called
from unity, inside extern “C”. But there is no hard and fast rule, you can create .m class and write your
c-named methods just outside the implementation part and you can call them from unity. The only
constraint is that you can not call these methods if you are building your App on simulator as unity
iOS plugins only work on device.
Also Read: Steps to Integrate Push Notification with Unity3D on iOS
Let’s do some coding e.g. in UnityIOSBridge.m class just write a method that can receive a string and
convert it to NSString, now UnityIOSBridge.m class should look like as follows:
#import "UnityIOSBridge.h"
void messageFromUnity(char *message)
{
NSString *messageFromUnity = [NSString stringWithUTF8String:message];
NSLog(@"%@",messageFromUnity);
}
@implementation UnityIOSBridge
@end
To call the above written method from Unity, we have to write a Unity script, so let’s create a file
UnityIOSBridge.cs.
using UnityEngine;
using System.Collections;
using System;
//This is needed to import iOS functions
using System.Runtime.InteropServices;
public class UnityIOSBridge : MonoBehaviour
{
/*
* Provide function decalaration of the functions defined in iOS
* and need to be called here.
*/
[System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void messageFromUnity(string message);
//Sends message to iOS
static void SendMessageToIOS()
{
messageFromUnity("Hello iOS!");
}
}
And it’s really as simple as it looks in the above code. Now to call a method written in Unity script
from iOS code, we can call UnitySendMessage(“UnityObjectName”, “UnityObject’sMethodName”,
“Your message”). In response, Unity will look for the UnityObject and then call that
UnityObject’sMethod to provide the message you passed. Now the UnityIOSBridge.m class should
look like:
#import "UnityIOSBridge.h"
void messageFromUnity(char *message)
{
NSString *messageFromUnity = [NSString stringWithUTF8String:message];
NSLog(@"%@",messageFromUnity);
}
@implementation UnityIOSBridge
-(void)sendMessageToUnity
{
UnitySendMessage(listenerObject, "messageFromIOS", "Hello Unity!");
}
@end
and the Unity script UnityIOSBridge.cs should look like:
using UnityEngine;
using System.Collections;
using System;
//This is needed to import iOS functions
using System.Runtime.InteropServices;
public class UnityIOSBridge : MonoBehaviour
{
/*
* Provide function decalaration of the functions defined in iOS
* and need to be called here.
*/
[System.Runtime.InteropServices.DllImport("__Internal")]
extern static public void messageFromUnity(string message);
//Sends message to iOS
static void SendMessageToIOS()
{
messageFromUnity("Hello iOS!");
}
//Provides messages sent from iOS
static void messageFromIOS(string message)
{
Debug.Log(message);
}
}
Well this was a very simple requirement but what if we want to do something more e.g. our plugin
should be able to notify Unity about the UIApplication delegate calls.There is no need to be worried
as we are going to implement that also but to do that we have to do some work around.Objective-C is
a runtime oriented language, which means that when it’s possible it defers decisions about what will
actually be executed from compile & link time to when it’s actually executing on the runtime.
Quick Tip: Download the readymade Push Notification Widget for Unity3D here.
This gives you a lot of flexibility in that, you can redirect messages to appropriate objects as you need
to or you can even intentionally swap method implementations etc. This requires the use of a runtime
which can introspect objects to see what they do & don’t respond to and dispatch methods
appropriately. So, we will take a simple example of “application: didFinishLaunchingWithOptions:”
delegate method UIApplicationDelagate. We will be creating a category class of UIApplication and
will implement load method. In load method, we will exchange the setDelagete method
implementation of UIApplication with the method setApp42Delegate method of our class as follows:
+(void)load
{
method_exchangeImplementations(class_getInstanceMethod(self,
@selector(setDelegate:)), class_getInstanceMethod(self,
@selector(setApp42Delegate:)));
}
-(void) setApp42Delegate:(id)delegate
{
static Class delegateClass = nil;
if(delegateClass == [delegate class])
{
return;
}
delegateClass = [delegate class];
exchangeMethodImplementations(delegateClass,
@selector(application:didFinishLaunchingWithOptions:),@selector(application:app42d
idFinishLaunchingWithOptions:), (IMP)
app42RunTimeDidFinishLaunching, "v@:::");
[self setApp42Delegate:delegate];
}
static void exchangeMethodImplementations(Class class, SEL oldMethod, SEL
newMethod, IMP impl, const char * signature)
{
Method method = nil;
//Check whether method exists in the class
method = class_getInstanceMethod(class, oldMethod);
if (method)
{
//if method exists add a new method
class_addMethod(class, newMethod, impl, signature);
//and then exchange with original method implementation
method_exchangeImplementations(class_getInstanceMethod(class, oldMethod),
class_getInstanceMethod(class, newMethod));
}
else
{
//if method does not exist, simply add as orignal method
class_addMethod(class, oldMethod, impl, signature);
}
}
BOOL app42RunTimeDidFinishLaunching(id self, SEL _cmd, id application, id
launchOptions)
{
BOOL result = YES;
if ([self
respondsToSelector:@selector(application:app42didFinishLaunchingWithOptions:)])
{
result = (BOOL) [self application:application
app42didFinishLaunchingWithOptions:launchOptions];
}
else
{
[self applicationDidFinishLaunching:application];
result = YES;
}
[[UIApplication sharedApplication]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)];
return result;
}
Let’s walk through the above code snippets, the method “setApp42Delegate” calls our
“exchangeMethodImplementations” that adds the “app42RunTimeDidFinishLaunching” to
UIApllication class and exchanges the implementations of “app42RunTimeDidFinishLaunching” with
“application: didFinishLaunchingWithOptions:” if it exists.This way we can have the access of all the
UIApplicationDelagate methods such as “applicationDidEnterBackground:”,
“application:didRegisterForRemoteNotificationsWithDeviceToken:” etc, without making changes
directly to the Unity generated Xcode project.You can download the source code of our Unity plugin
for iOS push notifications from Git Repo.
Get started with Unity3D backend with App42 here. If you have any questions or need any further
assistance, please feel free to write us at support@shephertz.com

More Related Content

PPT
Angular 8
DOCX
Learning MVC Part 3 Creating MVC Application with EntityFramework
ODT
Spring IOC advantages and developing spring application sample
PPTX
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
PPTX
Learning C# iPad Programming
PPTX
Angular modules in depth
PDF
I pad uicatalog_lesson02
PDF
Inversion of control using dependency injection in Web APIs using Unity Conta...
Angular 8
Learning MVC Part 3 Creating MVC Application with EntityFramework
Spring IOC advantages and developing spring application sample
LEARNING  iPAD STORYBOARDS IN OBJ-­‐C LESSON 1
Learning C# iPad Programming
Angular modules in depth
I pad uicatalog_lesson02
Inversion of control using dependency injection in Web APIs using Unity Conta...

Similar to Bridging the gap – plugin for unity and i os (20)

PDF
Plugin For Unity
PDF
Unity3D Plugins Development Guide
PPTX
Binding Objective-C Libraries, Miguel de Icaza
PDF
Building a Cross-Platform Mobile SDK in Rust.pdf
KEY
C# on the iPhone with MonoTouch Glasgow
PDF
AKiOSMagic Manual
PDF
Building static libraries for iOS with CocoaPods
PPT
Cross-Platform Mobile Development in Visual Studio
PDF
C# On The iPhone with MonoTouch at DDD8
PDF
Pentesting iOS Apps - Runtime Analysis and Manipulation
PDF
Bootstrapping iPhone Development
PPTX
Introduction to MonoTouch
PDF
Write native iPhone applications using Eclipse CDT
PDF
Advanced iOS Build Mechanics, Sebastien Pouliot
PDF
NYU hacknight, april 6, 2016
PDF
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
PPT
iPhone development from a Java perspective (Jazoon '09)
PPT
Ios development
KEY
iPhone Development Intro
PDF
GaMBi: Chiptunes on iOS and Mac
Plugin For Unity
Unity3D Plugins Development Guide
Binding Objective-C Libraries, Miguel de Icaza
Building a Cross-Platform Mobile SDK in Rust.pdf
C# on the iPhone with MonoTouch Glasgow
AKiOSMagic Manual
Building static libraries for iOS with CocoaPods
Cross-Platform Mobile Development in Visual Studio
C# On The iPhone with MonoTouch at DDD8
Pentesting iOS Apps - Runtime Analysis and Manipulation
Bootstrapping iPhone Development
Introduction to MonoTouch
Write native iPhone applications using Eclipse CDT
Advanced iOS Build Mechanics, Sebastien Pouliot
NYU hacknight, april 6, 2016
Разработка кросс-платформенного кода между iPhone < -> Windows с помощью o...
iPhone development from a Java perspective (Jazoon '09)
Ios development
iPhone Development Intro
GaMBi: Chiptunes on iOS and Mac
Ad

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Approach and Philosophy of On baking technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Machine learning based COVID-19 study performance prediction
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Big Data Technologies - Introduction.pptx
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Mobile App Security Testing_ A Comprehensive Guide.pdf
NewMind AI Weekly Chronicles - August'25 Week I
Digital-Transformation-Roadmap-for-Companies.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
MIND Revenue Release Quarter 2 2025 Press Release
Building Integrated photovoltaic BIPV_UPV.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Approach and Philosophy of On baking technology
MYSQL Presentation for SQL database connectivity
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
The Rise and Fall of 3GPP – Time for a Sabbatical?
Understanding_Digital_Forensics_Presentation.pptx
Machine learning based COVID-19 study performance prediction
Network Security Unit 5.pdf for BCA BBA.
Big Data Technologies - Introduction.pptx
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Diabetes mellitus diagnosis method based random forest with bat algorithm
Ad

Bridging the gap – plugin for unity and i os

  • 1. Bridging the Gap – Plugin for Unity and iOS There is always a need of finding out a way of communication from unity to XCode or XCode to unity due to the absence of a direct way. For the first time when I faced this problem, had to spend almost 3-4 days of my app’s development cycle on finding the right way to make this bridge. Through this blog post, I am going to share the method so as to help out any other developer like me. Creating a bridge over unity and iOS requires coding at both ends. So, let’s discuss XCode side first. According to Unity documentation, “Unity iOS supports automated plugin integration in a limited way. All files with extensions .a,.m,.mm,.c,.cpp located in the Assets/Plugins/iOS folder will be merged into the generated Xcode project automatically. However, merging is done by symlinking files from Assets/Plugins/iOS to the final destination, which might affect some workflows. The.h files are not included in the Xcode project tree, but they appear on the destination file system, thus allowing compilation of .m/.mm/.c/.cpp files”. So, we will create UnityIOSBridge.m and will place this class to the path “Assets/Plugins/iOS”.Unity needs the plugins to be c named so it is a good practice to wrap up the methods which need to be called from unity, inside extern “C”. But there is no hard and fast rule, you can create .m class and write your c-named methods just outside the implementation part and you can call them from unity. The only constraint is that you can not call these methods if you are building your App on simulator as unity iOS plugins only work on device. Also Read: Steps to Integrate Push Notification with Unity3D on iOS
  • 2. Let’s do some coding e.g. in UnityIOSBridge.m class just write a method that can receive a string and convert it to NSString, now UnityIOSBridge.m class should look like as follows: #import "UnityIOSBridge.h" void messageFromUnity(char *message) { NSString *messageFromUnity = [NSString stringWithUTF8String:message]; NSLog(@"%@",messageFromUnity); } @implementation UnityIOSBridge @end To call the above written method from Unity, we have to write a Unity script, so let’s create a file UnityIOSBridge.cs. using UnityEngine; using System.Collections; using System; //This is needed to import iOS functions using System.Runtime.InteropServices; public class UnityIOSBridge : MonoBehaviour { /* * Provide function decalaration of the functions defined in iOS * and need to be called here. */ [System.Runtime.InteropServices.DllImport("__Internal")] extern static public void messageFromUnity(string message); //Sends message to iOS static void SendMessageToIOS() {
  • 3. messageFromUnity("Hello iOS!"); } } And it’s really as simple as it looks in the above code. Now to call a method written in Unity script from iOS code, we can call UnitySendMessage(“UnityObjectName”, “UnityObject’sMethodName”, “Your message”). In response, Unity will look for the UnityObject and then call that UnityObject’sMethod to provide the message you passed. Now the UnityIOSBridge.m class should look like: #import "UnityIOSBridge.h" void messageFromUnity(char *message) { NSString *messageFromUnity = [NSString stringWithUTF8String:message]; NSLog(@"%@",messageFromUnity); } @implementation UnityIOSBridge -(void)sendMessageToUnity { UnitySendMessage(listenerObject, "messageFromIOS", "Hello Unity!"); } @end and the Unity script UnityIOSBridge.cs should look like: using UnityEngine; using System.Collections; using System; //This is needed to import iOS functions using System.Runtime.InteropServices; public class UnityIOSBridge : MonoBehaviour { /* * Provide function decalaration of the functions defined in iOS * and need to be called here. */
  • 4. [System.Runtime.InteropServices.DllImport("__Internal")] extern static public void messageFromUnity(string message); //Sends message to iOS static void SendMessageToIOS() { messageFromUnity("Hello iOS!"); } //Provides messages sent from iOS static void messageFromIOS(string message) { Debug.Log(message); } } Well this was a very simple requirement but what if we want to do something more e.g. our plugin should be able to notify Unity about the UIApplication delegate calls.There is no need to be worried as we are going to implement that also but to do that we have to do some work around.Objective-C is a runtime oriented language, which means that when it’s possible it defers decisions about what will actually be executed from compile & link time to when it’s actually executing on the runtime. Quick Tip: Download the readymade Push Notification Widget for Unity3D here. This gives you a lot of flexibility in that, you can redirect messages to appropriate objects as you need to or you can even intentionally swap method implementations etc. This requires the use of a runtime which can introspect objects to see what they do & don’t respond to and dispatch methods appropriately. So, we will take a simple example of “application: didFinishLaunchingWithOptions:” delegate method UIApplicationDelagate. We will be creating a category class of UIApplication and will implement load method. In load method, we will exchange the setDelagete method implementation of UIApplication with the method setApp42Delegate method of our class as follows: +(void)load {
  • 5. method_exchangeImplementations(class_getInstanceMethod(self, @selector(setDelegate:)), class_getInstanceMethod(self, @selector(setApp42Delegate:))); } -(void) setApp42Delegate:(id)delegate { static Class delegateClass = nil; if(delegateClass == [delegate class]) { return; } delegateClass = [delegate class]; exchangeMethodImplementations(delegateClass, @selector(application:didFinishLaunchingWithOptions:),@selector(application:app42d idFinishLaunchingWithOptions:), (IMP) app42RunTimeDidFinishLaunching, "v@:::"); [self setApp42Delegate:delegate]; } static void exchangeMethodImplementations(Class class, SEL oldMethod, SEL newMethod, IMP impl, const char * signature) { Method method = nil; //Check whether method exists in the class method = class_getInstanceMethod(class, oldMethod); if (method) { //if method exists add a new method class_addMethod(class, newMethod, impl, signature); //and then exchange with original method implementation method_exchangeImplementations(class_getInstanceMethod(class, oldMethod), class_getInstanceMethod(class, newMethod)); } else { //if method does not exist, simply add as orignal method class_addMethod(class, oldMethod, impl, signature); } } BOOL app42RunTimeDidFinishLaunching(id self, SEL _cmd, id application, id launchOptions) { BOOL result = YES; if ([self respondsToSelector:@selector(application:app42didFinishLaunchingWithOptions:)]) { result = (BOOL) [self application:application app42didFinishLaunchingWithOptions:launchOptions]; }
  • 6. else { [self applicationDidFinishLaunching:application]; result = YES; } [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound | UIRemoteNotificationTypeAlert)]; return result; } Let’s walk through the above code snippets, the method “setApp42Delegate” calls our “exchangeMethodImplementations” that adds the “app42RunTimeDidFinishLaunching” to UIApllication class and exchanges the implementations of “app42RunTimeDidFinishLaunching” with “application: didFinishLaunchingWithOptions:” if it exists.This way we can have the access of all the UIApplicationDelagate methods such as “applicationDidEnterBackground:”, “application:didRegisterForRemoteNotificationsWithDeviceToken:” etc, without making changes directly to the Unity generated Xcode project.You can download the source code of our Unity plugin for iOS push notifications from Git Repo. Get started with Unity3D backend with App42 here. If you have any questions or need any further assistance, please feel free to write us at support@shephertz.com