Tuesday, April 1, 2014

iOS plugin for Unity3d

Standard
Hello All:

In this post I will explain how to create an iOS plugin to access native APIs of  iOS or to call 3rd party libraries like ads integration. Basic information on how to create iOS plug-in can be found here.



Creating iOS plugin involves two part same as Android Plugin
1. Writing iOS native code
2. Writing a C# wrapper to access functionality written in #1

Let us start with #1

We will create a simple logger class which can be used to log some details.

First we will create .m file to expose the functionality of the wrapper.

Create a file and name it Logger.m (You can name it whatever you like but keep the extension as .m) and paste below code.
 
        
#import "LoggerHelper.h"
// Converts C style string to NSString
#define GetStringParam( _x_ ) ( _x_ != NULL ) ? [NSString stringWithUTF8String:_x_] : [NSString stringWithUTF8String:""]
extern void _log(const char* message)
{
 LoggerHelper* helper = [[LoggerHelper alloc]init];
    [helper log:GetStringParam(message)];
}


Now create file and name it LoggerHelper.h and paste below code.
 
        
#import <Foundation/Foundation.h>
@interface LoggerHelper: NSObject
{
    
}
- (void) log:(NSString *) message;
@end


Now create file and name it LoggerHelper.m and paste below code.
 
        #import "LoggerHelper.h"
@implementation LoggerHelper
- (void) log: (NSString *) message
{
      NSLog(@"Passed message = %@", message);
    
}


In order to let Unity3d know that we have plug-in to use, we need to put all these files in a particular folder.
Create this folder structure in your unity project
Assets-->Plugins-->iOS

and copy all files in iOS folder. Now we are ready to test this.




Add a new C# script file to Unity3d project and paste below code in that


 
            
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
public class iOSPluginTest : MonoBehaviour
{
#if UNITY_IPHONE 
  [DllImport("__Internal")]
 extern static private void _log(string message);
#endif
    #region Methods
    void OnGUI()
    {
        if (GUI.Button(new Rect(100, 300, 200, 100), "Log Message"))
        {
#if UNITY_IPHONE
     _log (string.Format ("{0}", "Sample Message"));
#endif
        }
       
    }
    #endregion
}

 
Attach this script to MainCamera and save the scene.

Now connect iOS device and hit Build And Run. 
You will see one button, clicking on the button will show you log message in XCode console.


Hope this helps.

Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

5 comments :

  1. Thanks for the turorial. ! I really think this site deserves more attention.
    Keep up the good work!

    ReplyDelete
    Replies
    1. Thanks Yifu for kind words. Hope you like other posts also :)

      Delete
  2. You need to have a:
    using System.Runtime.InteropServices;
    statement in top of the unity c# script. Otherwise Unity will not recognize:
    [DllImport("__Internal")]

    ReplyDelete
  3. Hello, Can you tell how do I compile the code into .a file ??

    ReplyDelete