This article is mainly about how to invoke the native API in unity in Android and iOS.
First, Unity supports calling C + + DLLs in C # so that the C + + interface can be called in unity in Android and iOS. With this feature, you can extend unity's capabilities. such as integration and invocation of third-party libraries. In order to satisfy the consistency of the Unity interface, consider providing the same interface on Android and iOS for C # calls.
The following two examples are listed here.
1.1. An example of how to call the native interface from C # is to pop up a webview that overrides a portion of the screen.
2.2. Asynchronous callback implementations for C #, simple C #, C + +, JAVA/OBJC (will be implemented in the next installment)
Because Android and iOS platforms load libraries differently (Android is dynamically loaded and iOS is statically loaded), reference declarations for DLL interfaces on different platforms in C # are not the same. This example corresponds to the following interface declaration:
1 Public classCallnativeapi {2 3 #ifUnity_editor4 Public Static voidOpenwebview (stringURL) { 5 return; 6 } 7 8 Public Static voidSumnum (intV1,intv2) { 9 Testunityeditor.sumnum (v1, v2);Ten One return; A } - #elifUnity_iphone -[DllImport ("__internal")] the Public Static extern voidOpenwebview (stringURL); -[DllImport ("__internal")] - Public Static extern voidSumnum (intV1,intv2); - #elifUnity_android +[DllImport ("libtestunity", CallingConvention =callingconvention.cdecl)] - Public Static extern voidOpenwebview (stringURL); +[DllImport ("libtestunity", CallingConvention =callingconvention.cdecl)] A Public Static extern voidSumnum (intV1,intv2); at #endif - - Public Static voidSumnumforresult (intV1,intv2, Callbackmanager.resultcallback callback) { - -TestCallbackManager.sumNumCallback.SetResultCallBack (NewCallbackmanager.resultcallback (callback)); - in Sumnum (v1, v2); - to return; + } - } the * namespaceCallbackmanager $ { Panax Notoginseng Public Delegate voidResultcallback (intresult); - the Public classsumnummanager{ + PublicSumnummanager () A { the } + - PrivateResultcallback Resultcallback; $ $ Public voidSetresultcallback (Resultcallback callback) - { -Resultcallback =callback; the } - Wuyi Public voidSendresult (intresult) the { - resultcallback (result); Wu } - } About } $ - Public classTestcallbackmanager { - - Public StaticCallbackmanager.sumnummanager Sumnumcallback =NewCallbackmanager.sumnummanager (); A +}
Reprint to:
How unity calls the native API in Android and iOS
How unity calls the native API in Android and iOS