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:
- 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. 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