Unity Native IOS Plugin: Bridge between Swift and Unity3D

Habibur Rahman Ovie
4 min readOct 6, 2020

--

Unity3D is a powerful game engine with strong native support. Today I am talking about the native IOS plugin in Unity. Unity can not directly communicate with Swift. So we need Objective-C for communicating with swift and Unity. Some days ago When I try to create an IOS plugin, I did not find any proper instructions for this and misguided. So I decided to share my findings here. Let me clear first, here I share my way of creating the Unity IOS plugin. It may not be the best practice but an effective one.

Steps:
1. Create an IOS Framework project: Create one swift, one objective-C, and one header file. Initially No coding.
2. SetUp Unity Project: Create a new Unity project and move those above files into the unity project and build it for the IOS platform
3. Modify IOS Code: Now open the built project with Xcode and modify our previously created swift and Objective C file.

Step 1: Create the IOS Framework Project:
Create a new IOS framework project from XCode. Then make a group named

Create a new IOS Framework Project

Source. Inside the Source group Create a new swift file named UnityPlugin.swift, a header file named UnityPlugin-Bridging-Header.h, and an Objective-C file named UnityPluginBridge.mm. Don`t forget to rename the extension of the Objective-C file. Change it from .m to .mm. So the file name will be UnityPluginBridge.mm. That's all for Step 1.

Project structure.

Step 2: SetUp Unity Project
Create a Unity project and create a folder named Plugins inside the Asset folder. Inside Plugins create another folder iOS and inside the iOS folder Create UnityIosPlugin folder. Now copy the Source folder from our IOS framework project into the UnityIosPlugin folder of the Unity project. Then Create a new folder named Editor inside the UnityIosPlugin folder and Create a new c# script on the Editor folder named SwiftPostProcess.cs

Unity Project Structure

Now add the following code into SwiftPostProcess.cs script.

Be sure to change the following two lines in SwiftPostProcess.cs script according to your project structure. Please look at the following image to get more ideas.

After changing the SwiftPostProcess.cs script. Create another script PluginHelper.cs. This script will communicate with the IOS plugin. Write the following code in that script and attach this script in the scene with any gameObject.

PluginHelper.cs

[DllImport(“__Internal”)]
private static extern int _addTwoNumberInIOS(int a, int b);

In this script above two lines indicates that in our IOS plugin there is a function named _addTwoNumberInIOS which takes two integers and returns one integer value. We do not write the _addTwoNumberInIOS function in our Objective-C class yet. Don`t worry we will add it later. Other codes of this script are self-explanatory. In this script, we just call the _addTwoNumberInIOS function at the start and print the return value in a text view. Now time to build your project on the IOS platform.

Step 3: Modify our IOS plugin codes and Run it on an IOS device.

After building the project open it in Xcode. You can find your plugin codes in the Xcode project. We will modify it now.

Plugin File location in Xcode

Open UnityPlugin.swift file from Xcode and paste the following code.

Here @obc indicates that it can be called from an Objective-C file. Now build the Xcode and open UnityPluginBridge.mm file. This Objective-C file will be communicating with Unity. The UnityPluginBridge.mm file is the bridging medium between swift and Unity. We need to call the swift function from this script. Paste the following code in that script.

Here we write a function named _addTwoNumberInIOS which take two integer parameter and return one integer. Can you recognize this function? Yes, you are right we call this function from unity in PluginHelper.cs script. Inside the _addTwoNumberInIOS function we call the swift function that we wrote before and return that result. The return value of this function will be received by Unity and show it in the text view. Now build and run the Xcode project in an IOS device. If everything was right then you can see the result.

If you try to run it on the device, you may see this error dyld: Library not loaded . To resolve this look here

That`s all. I think you get the primary idea about the Unity IOS Plugin. Now its time to write some charismatic code in swift.

You will get the full project here.

--

--

Habibur Rahman Ovie
Habibur Rahman Ovie

Responses (6)