Cómo llamar a funciones nativas de iOS desde Unity

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:

  1. extern "C" 
  2. int _add(int x, int y) 
  3. // Just a simple example of returning an int value 
  4. return x + y; 
  5. // Returns a char* (a string to Unity) 
  6. char* _helloWorldString() 
  7. // We can use NSString and go to the c string that Unity wants 
  8. NSString *helloString = @"Hello World"; 
  9. // 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. 
  10. return cStringCopy([helloString UTF8String]); 
  11.  
  12. //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. 
  13. char* cStringCopy(const char* string) 
  14. if (string == NULL) 
  15. return NULL; 
  16. char* res = (char*)malloc(strlen(string) + 1); 
  17. strcpy(res, string); 
  18. 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.

  1. using UnityEngine; 
  2. using System.Collections; 
  3. // We need this one for importing our IOS functions 
  4. using System.Runtime.InteropServices; 
  5. public class TestPlugin : MonoBehaviour 
  6.  
  7. #if UNITY_IPHONE 
  8. [DllImport ("__Internal")] 
  9. private static extern int _add(int x, int y); 
  10.  
  11. // 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.  
  12.  
  13. [DllImport ("__Internal")] 
  14. private static extern string _helloWorldString(); 
  15.  
  16. #endif 
  17. // Now make methods that you can provide the iOS functionality 
  18.  
  19. static int Add(int x, int y) 
  20. int result = 0; 
  21. // We check for UNITY_IPHONE again so we don't try this if it isn't iOS platform. 
  22. #if UNITY_IPHONE 
  23. // 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. 
  24. if (Application.platform == RuntimePlatform.IPhonePlayer) 
  25. result = _add(x, y); 
  26. #endif 
  27.  
  28. return result; 
  29. static string HelloWorldString() 
  30. string helloWorld = ""; 
  31.  
  32. #if UNITY_IPHONE 
  33. if (Application.platform == RuntimePlatform.IPhonePlayer) 
  34. helloWorld = _helloWorldString(); 
  35. #endif 
  36.  
  37.  

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