Come chiamare funzioni native di iOS da Unity

Leggi i documenti, è abbastanza facile e diretto: Costruire plugin per iOS e Unity - Manuale: Native Plugins and Low-level Native Plugin Interface. questo è tutto ciò di cui avrai bisogno.

Fai da te, prova, fallisci e impara!

Ora vediamo come possiamo fare questo:

Nel tuo progetto Unity, crea una cartella Assets/Plugin/iOS. I file direttamente in quella cartella (non possono essere in una sottocartella) vengono automaticamente aggiunti al tuo progetto iOS quando fai creare a Unity una build di iOS.

Creeremo un file testplugin.mm in quella cartella. In quel file, metteremo il codice per il lato iOS del plugin. Unity ha bisogno che i plugin abbiano un nome C. Quindi, lo avvolgiamo in 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. Il tuo script otterrebbe quindi quel messaggio e potresti elaborarlo come vuoi.

Non scrivo codice qui su quora per tali domande genneralmente, ma sembra che tu abbia avuto fortuna con questo. Grazie per A2A Rohith Shenoy