I am trying to create a plugin from unity to x-code.but i don’t have any knowledge about the x-code.
what i have done so far is:
created a custom class in C#:
public class CRBinding : MonoBehaviour {
[DllImport("__Internal")]
private static extern void _PrintMessageFromUnity (string message);
public static void PrintMessageToXcode(string message)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
_PrintMessageFromUnity(message);
}
}
[DllImport("__Internal")]
private static extern void _SetBadgeNumber (int badgeNumber);
public static void SetBadgeNumber(int badgeNumber)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
_SetBadgeNumber(badgeNumber);
}
}
}
also created a class in x-code :
NSString* CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
}
// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
void _SetBadgeNumber(int number)
{
[UIApplication sharedApplication].applicationIconBadgeNumber = number;
}
void _PrintMessageFromUnity(const char* message)
{
NSString formattedMessage = CreateNSString(message);
NSLog(@"This was sent from Unity %@",formattedMessage);
}
}
but it gives me some error with unitypause() in x-code’s AppController.mm file while building the project.
can anyone tell me what i am doing wrong with this.or any other simple way to create a plugin for unity3d to x-code???
i have done my code for unity to x-Code,but still not able to get back from x-code to unity…???
here is my code files…
Unity COde File
/// <summary>
/// Unity to Xcode tutorial.
/// </summary>
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class CRBinding : MonoBehaviour {
public static bool xcodeCalled;
[DllImport("__Internal")]
//[DllImport ("CRBinding.mm")]
private static extern void _PrintMessageFromUnity (string message);
public static void PrintMessageToXcode(string message)
{
Debug.Log("in CRBinding.cs -> PrintMessageToXcode()");
if (Application.platform != RuntimePlatform.OSXEditor)
{
Debug.Log("in CRBinding.cs -> PrintMessageToXcode() -> if()");
xcodeCalled = true;
_PrintMessageFromUnity("Hi this is message from unity");
}
}
[DllImport("__Internal")]
//[DllImport ("CRBinding.mm")]
private static extern void _SetBadgeNumber (int badgeNumber);
public static void SetBadgeNumber(int badgeNumber)
{
if (Application.platform != RuntimePlatform.OSXEditor)
{
_SetBadgeNumber(badgeNumber);
}
}
}
X-Code Code Files
// This is just a helper method
// Converts C style string to NSString
#import "AppDelegate.m"
NSString* CreateNSString (const char* string)
{
if (string)
return [NSString stringWithUTF8String: string];
else
return [NSString stringWithUTF8String: ""];
}
// When native code plugin is implemented in .mm / .cpp file, then functions
// should be surrounded with extern "C" block to conform C function naming rules
extern "C" {
/* NSString * _PrintMessageFromUnity(const char* string)
{
NSString *formattedMessage = CreateNSString(string);
NSLog(@"This was sent from Unity ");
return formattedMessage;
}*/
NSString * _PrintMessageFromUnity(const char* string)
{
NSLog(@"This was sent from Unity ");
AppDelegate* obj_AppDelegate = [[AppDelegate alloc]init];
[obj_AppDelegate launchFrontend];
NSString *formattedMessage = CreateNSString(string);
return formattedMessage;
}
}