Unity如何在Android和iOS中調用原生API
首先,unity支持在C#中調用C++ dll,可以在Android和iOS中提供C++接口在unity中調用。有了這個功能,unity的功能可以擴展。比如集成和調用第三方庫。同時為了滿足unity界面的壹致性,可以考慮在android和iOS上提供相同的界面供C#調用。這裏有兩個例子。這個例子對應的接口聲明如下:公共類callnativeapi { # if unity _ editor public static void open webview(string URL){ return;} public static void sum num(int v 1,int v2) { TestUnityEditor。SumNum(v1,v2);返回;} # elif UNITY _ IPHONE[DllImport(_ _ Internal))]public static extern void open webview(string URL);[DllImport(“_ _ Internal”)]public static extern void sum num(int v 1,int v2);# elif UNITY _ ANDROID[DllImport(" libtestunity ",calling conventi on = calling conventi on。cdecl)]public static extern void open webview(string URL);[DllImport ("libtestunity ",calling conventi on = calling conventi on。cdecl)]public static extern void sum num(int v 1,int v2);#endif公共靜態void SumNumForResult(int v 1,int v2,CallbackManager。result callback callback){ testcallbackmanager . sumnumcallback . setresultcallback(new callback manager。ResultCallback(回調));SumNum(v1,v2);返回;} }命名空間CallbackManager {公共委托void result callback(int result);public class SumNumManager { public SumNumManager(){ } private result callback result callback;public void SetResultCallBack(result callback callback){ result callback = callback;} public void send result(int result){ result callback(result);} } }公共類TestCallbackManager {公共靜態CallbackManager。SumNumManager sumNumCallback = new callback manager。SumNumManager();}