Unity在Android和iOS中如何调用Native API
展开全部
首先unity支持在C#中调用C++ dll,这样可以在Android和iOS中提供C++接口在unity中调用。利用这一特性,可以扩展unity的功能。例如集成和调用第三方库。同时为了满足对unity接口的一致性,可以考虑在android和iOS上提供相同的接口供C#调用。这里列举以下两个例子。本例对应的接口声明如下:public class CallNativeAPI { #if UNITY_EDITOR public static void OpenWebView(string url) { return; } public static void SumNum(int v1, int v2) { TestUnityEditor.SumNum(v1, v2); return; } #elif UNITY_IPHONE [DllImport ("__Internal")] public static extern void OpenWebView(string url); [DllImport ("__Internal")] public static extern void SumNum(int v1, int v2); #elif UNITY_ANDROID [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] public static extern void OpenWebView(string url); [DllImport ("libtestunity", CallingConvention = CallingConvention.Cdecl)] public static extern void SumNum(int v1, int v2); #endif public static void SumNumForResult(int v1, int v2, CallbackManager.ResultCallback callback) { TestCallbackManager.sumNumCallback.SetResultCallBack(new CallbackManager.ResultCallback(callback)); SumNum(v1, v2); return; } } namespace CallbackManager { public delegate void ResultCallback(int result); public class SumNumManager{ public SumNumManager() { } private ResultCallback resultCallback; public void SetResultCallBack(ResultCallback callback) { resultCallback = callback; } public void SendResult(int result) { resultCallback(result); } } } public class TestCallbackManager { public static CallbackManager.SumNumManager sumNumCallback = new CallbackManager.SumNumManager(); }
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询