Lee la documentación es bastante fácil y sencillo: Building Plugins for iOS and Unity - Manual: Native Plugins and Low-level Native Plugin Interface. eso es todo lo que necesitarás.
Hazlo tú mismo, prueba, falla y aprende!
Ahora veamos cómo podemos hacer esto:
En tu proyecto Unity, crea una carpeta Assets/Plugin/iOS. Los archivos que están directamente en esa carpeta (no pueden estar en una subcarpeta) se añaden automáticamente a tu proyecto iOS cuando haces que Unity cree una compilación iOS.
Crearemos un archivo testplugin.mm en esa carpeta. En ese archivo, ponemos el código para el lado iOS del plugin. Unity necesita que los plugins tengan un nombre c. Así que lo envolvemos en un extern "C". As the Building Plugins for iOS doc says while implementing the plugin you must ensure the functions are declared with C linkage to avoid name mangling issues.
Here is an example plugin:
- extern "C"
- {
- int _add(int x, int y)
- {
- // Just a simple example of returning an int value
- return x + y;
- }
- // 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]);
- }
- }
- //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;
- }
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
- {
- #if UNITY_IPHONE
- [DllImport ("__Internal")]
- private static extern int _add(int x, int y);
- // 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();
- #endif
- // Now make methods that you can provide the iOS functionality
- static int Add(int x, int y)
- {
- int result = 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)
- {
- result = _add(x, y);
- }
- #endif
- return result;
- }
- static string HelloWorldString()
- {
- string helloWorld = "";
- #if UNITY_IPHONE
- if (Application.platform == RuntimePlatform.IPhonePlayer)
- {
- helloWorld = _helloWorldString();
- }
- #endif
- }
- }
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 callUnitySendMessage("TestPluginListener", "ListenerMethod", "My test message"); in your iOS code. Su secuencia de comandos a continuación, obtener ese mensaje y se puede procesar como quieras.
No escribo código aquí en quora para tales preguntas gennerally, pero parece que tienes suerte con él. Gracias por el A2A Rohith Shenoy