Unity在Android和iOS中如何调用Native API (1)

本文主要是对unity中如何在Android和iOS中调用Native API进行介绍。

首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调用。利用这一特性,可以扩展unity的功能。例如集成和调用第三方库。同时为了满足对unity接口的一致性,可以考虑在android和iOS上提供相同的接口供C#调用。

这里列举以下两个例子。

1. 1. 以弹出一个覆盖部分屏幕的webview为例来说明如何从C#调用Native接口。

2. 2. 简单的C# -> C++ -> Java/ObjC -> C#的异步回调实现(会在下一期中给出实现)

由于android和iOS平台加载库的方式不同(android为动态加载,iOS为静态加载),在C#中针对不同平台对dll 接口的引用声明是不一样的。本例对应的接口声明如下:

 
 
  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. namespace CallbackManager 
  35.     public delegate void ResultCallback(int result); 
  36.      
  37.     public class SumNumManager{ 
  38.         public SumNumManager() 
  39.         { 
  40.         } 
  41.          
  42.         private ResultCallback resultCallback; 
  43.  
  44.         public void SetResultCallBack(ResultCallback callback) 
  45.         { 
  46.             resultCallback = callback; 
  47.         } 
  48.              
  49.         public void SendResult(int result) 
  50.         { 
  51.             resultCallback(result); 
  52.         } 
  53.     } 
  54.  
  55. public class TestCallbackManager { 
  56.  
  57.     public static CallbackManager.SumNumManager sumNumCallback = new CallbackManager.SumNumManager(); 
  58.      

 

1. 如何打开webview

由于从C#调用C++的接口,需要在C++层分别给出相应的接口实现,如下所示:

★ Android 平台

 
 
  1. extern "C" { 
  2. void OpenWebView(const char *url){ 
  3.         __android_log_print(ANDROID_LOG_INFO, "TestUnity""START ; invoking OpenWebView() = %s", url); 
  4.  
  5.         JNIEnv *env = getJNIEnv(); 
  6.         initJni(env); 
  7.  
  8. jmethodID mid = env->GetStaticMethodID(jniClass, "OpenWebView""(Ljava/lang/String;)V"); 
  9.  
  10.         if (env->ExceptionCheck()) { 
  11.             env->ExceptionDescribe(); 
  12.         } 
  13.  
  14.         jstring openUrl = (env)->NewStringUTF(url); 
  15.         env->CallStaticVoidMethod(jniClass, mid, openUrl); 
  16.  
  17.         if (env->ExceptionCheck()) { 
  18.             env->ExceptionDescribe(); 
  19.         } 
  20.  
  21.         __android_log_print(ANDROID_LOG_INFO, "TestUnity""END ; invoking OpenWebView()"); 
  22.         return
  23.     } 

 

★ iOS平台

 

 
 
  1. #ifdef __cplusplus 
  2. extern "C" { 
  3. #endif 
  4. void OpenWebView(const char* url) { 
  5.      
  6.     std::string webUrl(url); 
  7.     WebviewController::getInstance()->loadURL(webUrl).show(); 
  8.     return
  9. #ifdef __cplusplus 
  10. #endif 

本例运行时的截图如下所示:

 

本文出自 “Roket的空间” 博客,请务必保留此出处http://roketruan.blog.51cto.com/5114948/870457

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Game Center * iTunes Setup * Init The Game Center * Leaderboards * Achievements * Changes * Friends * Real-Time Multiplayer * Trun Based Multiplayer In-App purchases (Cons. / Non-Cons.) * iTunes Setup * Coding Guidelines * Transactions Validation * Restoring Purchases * Store Product View Social Sharing * Facebook Post * Twitter Post * Native Sharing Dialog * Sending e-mail * Documentation iAd App Network * Banners * Interstitials * 9 Gravity Options supported * Ability to set Custom x, y banner point * iTunes Setup * Coding Guidelines Camera And Gallery * Taking Photo from Camera * Loading Texture from Gallery * Saving Texture to Gallery * Saving Screenshot to Gallery * Coding Guidelines iCloud * iTunes Setup * Coding Guidelines * Cloud Kit More features * Video API * Media Player API * Local Notifications * Push Notifications * Shared App API * Date Time Picker * Poups and Pre-loaders * System Native Events * Flash Like Events as gift Playmaker * Avaliable Actions List * iAd Playmaker Actions Guide * InApp Purchasing with Playmaker Supported 3-rd party plugins: * Playmaker - Artists and Designers: Realize your creative vision without coding! Unlock the power of Unity. * Simple IAP System - Takes the complexity out of in-app purchases (IAPs) and the billing process as a whole by providing a one-stop solution for managing IAPs. * OneSignal Boost Engagement With Smart Push Notifications. * Soomla Grow - With SOOMLA Grow, traditional analytics fade away. Instead of knowing your users based only on data you collect, you can now study their behavior across thousands of games. All Source Code is Open! IOS6, IOS7, IOS8, IOS9 - supported 8.1: New Features Cloud Kit. Read more: https://goo.gl/qxMHNf Fixes: Unity 5.3 compatibility Apple TV compilation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值