找回密码
 立即注册
查看: 352|回复: 0

unity 实现IOS原生方法调用

[复制链接]
发表于 2021-12-14 21:40 | 显示全部楼层 |阅读模式
在游戏中我们有时需要直接调用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) ,中需要
#include <stdlib.h>#ifdef __cplusplus extern "C" { #endiftypedef 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
懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )

GMT+8, 2024-9-23 01:32 , Processed in 0.089231 second(s), 25 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表