How does Unity call the Native API and androidios in Android and iOS?

Source: Internet
Author: User

How does Unity call the Native API and androidios in Android and iOS?

This article describes how to call Native APIs in Android and iOS in unity.

First, unity supports Calling C ++ dll in C #, so that C ++ interfaces can be provided in Android and iOS to be called in unity. With this feature, you can extend the functions of unity. For example, integrate and call third-party libraries. To ensure consistency of the unity interface, you can provide the same interface on android and iOS for C # to call.

The following two examples are provided.

1. Take a webview that overwrites some screens as an example to illustrate how to call the Native Interface from C.

2. Simple asynchronous callback implementation of c #-> C ++-> Java/ObjC-> C # (implementation will be provided in the next issue)

Because the android and iOS platforms load libraries in different ways (android is dynamic loading and iOS is static loading), the reference declarations for dll interfaces on different platforms in C # are different. The interface declaration for this example is as follows:

 1 public class CallNativeAPI {  2       3 #if UNITY_EDITOR  4     public static void OpenWebView(string url) {  5         return;  6     }  7       8     public static void SumNum(int v1, int v2) {  9         TestUnityEditor.SumNum(v1, v2); 10          11         return; 12     } 13 #elif UNITY_IPHONE 14     [DllImport ("__Internal")] 15     public static extern void OpenWebView(string url); 16     [DllImport ("__Internal")] 17     public static extern void SumNum(int v1, int v2);    18 #elif UNITY_ANDROID 19     [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] 20     public static extern void OpenWebView(string url); 21     [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] 22     public static extern void SumNum(int v1, int v2); 23 #endif   24      25     public static void SumNumForResult(int v1, int v2, CallbackManager.ResultCallback callback) { 26          27         TestCallbackManager.sumNumCallback.SetResultCallBack(new CallbackManager.ResultCallback(callback)); 28  29         SumNum(v1, v2); 30  31         return; 32     } 33 } 34  35 namespace CallbackManager 36 { 37     public delegate void ResultCallback(int result); 38      39     public class SumNumManager{ 40         public SumNumManager() 41         { 42         } 43          44         private ResultCallback resultCallback; 45  46         public void SetResultCallBack(ResultCallback callback) 47         { 48             resultCallback = callback; 49         } 50              51         public void SendResult(int result) 52         { 53             resultCallback(result); 54         } 55     } 56 } 57  58 public class TestCallbackManager { 59  60     public static CallbackManager.SumNumManager sumNumCallback = new CallbackManager.SumNumManager(); 61      62 }

 

Reprinted:

How does Unity call Native APIs in Android and iOS?

Related Article

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.