unity 实现IOS原生方法调用

在游戏中我们有时需要直接调用IOS相关的接口代码,如唤起iOS的原生弹窗(GDPR,ATT弹窗等),获取国家信息等等,其实本质就是从C#端,调用对应的iOS的C++代码。
有两种方法,一种是利用UnitySendMessage来实现;一种是利用delegates;

第一种UnitySendMessage:
这种方式要简单一些,但是会有一些限制
使用UnitySendMessage("GameObjectName1", "MethodName1", "Message to send"); 方法
这种方式需要场景中有一个对应的名字“GameObjectName1”的GameObject,同时该对象上挂载了一个继承自MonoBehaviour的脚本,该脚本中有一个名为“MethodName1”的方法,最后一个参数为传递的参数,可有可无。
注意:这种方法是异步的,而且会延迟一帧执行

第二种 delegates

C#端的对应接口需要,以如下形式添加

//添加MonoPInvokeCallback;C++中 会有一个对应的 OnViewConfirm 方法指针(名字要一致)
[MonoPInvokeCallback(typeof(OnViewConfirm))]
static void calledByNativeC() {              //该方法必须为静态函数
       viewCallBack?.Invoke();
}

private delegate void OnViewConfirm();
private static Action viewCallBack;

[DllImport("__Internal")]
private static extern void showAlertDialog(string title, string message, string []buttons, string []urls, int buttonCount, OnViewConfirm calledByNativeC);

public override void showSystemAlertDialog(string title, string message, string []buttons, string []urls, Action callBack)
        {
            viewCallBack = callBack;
            showAlertDialog(title, message, buttons, urls, buttons.Length, calledByNativeC);  //这里把对应的C#回调函数传给C++
        }

对应的 C++ (.cpp)或者 Objective-C++(.mm) ,(Assets\Plugins\iOS\iOSPlatformHelper)中需要

#include <stdlib.h>
#ifdef __cplusplus 
extern "C" { 
#endif

typedef void (*OnViewConfirm)();               //定义对应的指针函数
static OnViewConfirm s_calledFromCSharp = NULL;
void showAlertDialog_native()
{
...
if (s_calledFromCSharp != NULL)
{
        s_calledFromCSharp();       //触发对应的回调函数
}
...
}

void showAlertDialog(const char *title, const char *message, const char **buttons, const char **urls, int buttonCount, OnViewConfirm viewClosed)
{
...
s_calledFromCSharp = viewClosed;       //接收C#传过来的函数指针
...
showAlertDialog_native();
}

最后顺便提一下,着这种平台方法的整合

public abstract class PlatformTool
{
  private static PlatformTool _instance = null;
        public static PlatformTool Instance
        {
            get
            {
                if (_instance == null)
                {

#if UNITY_EDITOR_WIN
                    _instance = new PlatformToolWindows();
#elif UNITY_ANDROID
                    _instance = new PlatformToolAndroid();
#elif UNITY_IOS
                    _instance = new PlatformToolIOS();
#else
                    _instance = new PlatformToolOther();
#endif
                }


                return _instance;
            }
        }

//各种版本号,用户id,国家,原生弹窗等的属性或者方法
public virtual string getGuestAccount()
        {
            return getDeviceID();
        }

public virtual string getDeviceID()
        {
            if (_deviceUniqueIdentifier.Length == 0)
            {
                _deviceUniqueIdentifier = SystemInfo.deviceUniqueIdentifier;
            }

            return _deviceUniqueIdentifier;
        }

}
public class PlatformToolWindows : PlatformTool{}
public class PlatformToolAndroid : PlatformTool{}
public class PlatformToolIOS : PlatformTool{}
public class PlatformToolOther : PlatformTool{}

相关内容:
https://docs.unity3d.com/2020.3/Documentation/Manual/PluginsForIOS.html
https://www.jianshu.com/p/0c61444c9b28
利用第一种方式实现的原生iOS窗口调用方法
https://www.jianshu.com/p/75cef8007e75

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容