I actually have been looking for the answer to this same question recently. There isn’t very much documentation on this. Community is basically our best shot at demystifying technical issues. Has anyone else done this before?
I think I have a general idea of how it works, but I don’t want to lead you down the wrong road since I have not actually done it myself. But here is my general understanding of it.
There is no way to communicate between Unity and XCode directly. So you have to create a bridge, like you mentioned. This bridge requires coding on both ends.
From the documentation link you posted (you are on the right track)
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, you need to create a UnityIOSBridge.m and place that 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 a simulator, as Unity iOS plugins only works on devices (as far as I know).
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 method from Unity, we have to write a Unity script, so let’s create the 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!");
}
}
But with this you require access to binary files. Because you received a cocoapod, there are more steps than what I outlined. I am trying to figure those out.
But here are my thoughts about the cocoapod. The files you need, such as the .h and .m files are automatically generated by the cocoapod when you import the library or framework. These are Objective-C files. What does your podfile look like in Unity after you import? You may have all that you need. Rename the .m file to .mm, and that will get you to the start of my post. Again, I have not done this myself, I am just guessing :-).
And as a reference, I have been looking at this article to learn (mostly):