Creating iOS plugins for Unity

 

 Creating iOS plugins for Unity

This is just a quick starter tutorial on making a simple iOS plugin for Unity.
In your Unity project, create a folder Assets/Plugin/iOS. Files directly in that folder (can’t be in a subfolder) automatically get added to your iOS project when you have Unity create an iOS build.
We’ll create a testplugin.mm file in that folder. In that file, we put the code for the iOS side of the plugin. Unity needs the plugins to be c named. So, we wrap it in an extern “C”.
Here is an example plugin:

     
extern "C"
{
int _pow2(int x)
{
// Just a simple example of returning an int value
return x * x;
}
// Returns a char* (a string to Unity)
char* _helloWorldString()
{
// We can use NSString and go to the c string that Unity wants
NSString *helloString = @"Hello World";
// UTF8String method gets us a c string. Then we have to malloc a copy to give to Unity. I reuse a method below that makes it easy.
return cStringCopy([helloString UTF8String]);
}
// Here is an example of getting a string from Unity
char* _combineStrings(const char* cString1, const char* cString2)
{
// This shows we can create two NSStrings* from the c strings from Unity
NSString *string1 = CreateNSString(cString1);
NSString *string2 = CreateNSString(cString2);
NSString *combinedString = [NSString stringWithFormat:@"%@ %@", string1, string2];
// Same as before, have to go to a c string and then malloc a copy of it to give to Unity
return cStringCopy([combinedString UTF8String]);
}
}
//I also like to include these two convenience methods to convert between c string and NSString*. You need to return a copy of the c string so that Unity handles the memory and gets a valid value.
char* cStringCopy(const char* string)
{
if (string == NULL)
return NULL;
char* res = (char*)malloc(strlen(string) + 1);
strcpy(res, string);
return res;
}
// This takes a char* you get from Unity and converts it to an NSString* to use in your objective c code. You can mix c++ and objective c all in the same file.
static NSString* CreateNSString(const char* string)
{
if (string != NULL)
return [NSString stringWithUTF8String:string];
else
return [NSString stringWithUTF8String:""];
}
view raw gistfile1.c hosted with ❤ by GitHub
And that’s it for a simple iOS plugin. Now we need a Unity script to use it.
Create a file called TestPlugin.cs in Unity.

 
using UnityEngine;
using System.Collections;
// We need this one for importing our IOS functions
using System.Runtime.InteropServices;
public class TestPlugin : MonoBehaviour
{
// Use this #if so that if you run this code on a different platform, you won't get errors.
#if UNITY_IPHONE
[DllImport ("__Internal")]
private static extern int _pow2(int x);
// For the most part, your imports match the function defined in the iOS code, except char* is replaced with string here so you get a C# string.
[DllImport ("__Internal")]
private static extern string _helloWorldString();
[DllImport ("__Internal")]
private static extern string _combineStrings(string cString1, string cString2);
#endif
// Now make methods that you can provide the iOS functionality
static int Pow2(int x)
{
int pow = 0;
// We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform.
#if UNITY_IPHONE
// Now we check that it's actually an iOS device/simulator, not the Unity Player. You only get plugins on the actual device or iOS Simulator.
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
pow = _pow2(x);
}
#endif
// TODO: You could test for Android, PC, Mac, Web, etc and do something with a plugin for them here.
return pow;
}
static string HelloWorldString()
{
string helloWorld = "";
// We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform.
#if UNITY_IPHONE
// Now we check that it's actually an iOS device/simulator, not the Unity Player. You only get plugins on the actual device or iOS Simulator.
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
helloWorld = _helloWorldString();
}
#endif
// TODO: You could test for Android, PC, Mac, Web, etc and do something with a plugin for them here.
return helloWorld;
}
static string CombineStrings(string string1, string string2)
{
string combinedString = "";
// We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform.
#if UNITY_IPHONE
// Now we check that it's actually an iOS device/simulator, not the Unity Player. You only get plugins on the actual device or iOS Simulator.
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
combinedString = _combineStrings(string1, string2);
}
#endif
// TODO: You could test for Android, PC, Mac, Web, etc and do something with a plugin for them here.
return combinedString;
}
view raw gistfile1.cs hosted with ❤ by GitHub
We can also pass values to and from Unity/iOS with PlayerPrefs an NSUserDefaults. PlayerPrefs.SetString(“key”, “value”) in a Unity script actually creates/updates that key with that value in NSUserDefaults since it’s just a wrapper around NSUserDefaults that Unity can use. So, your iOS code can update values in NSUserDefaults and then you can get those values with PlayerPrefs in your Unity scripts.
Another option to pass back a value from your iOS code to Unity is at any time in your iOS code, you can call UnitySendMessage("UnityObjectName", "UnityObject'sMethodName", "Some message here"). That makes Unity look for that object in your scene and then call that method and give it that string. So, if you create a TestPluginListener.cs script and have a method void ListenerMethod(string message), you could create a TestPluginListener object in your Scene, add that script component, then call UnitySendMessage("TestPluginListener", "ListenerMethod", "My test message"); in your iOS code. Your script would then get that message and you could process it however you want.

이 블로그의 인기 게시물

둘 중 누군가 그녀를 죽였다, 범인 해설

[MAC OS X] mds_stores 100% CPU usage

tips more on unity ...