How unity calls the native API in Android and iOS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.