在游戲中我們有時(shí)需要直接調(diào)用IOS相關(guān)的接口代碼哩治,如喚起iOS的原生彈窗(GDPR,ATT彈窗等)艾猜,獲取國(guó)家信息等等买喧,其實(shí)本質(zhì)就是從C#端,調(diào)用對(duì)應(yīng)的iOS的C++代碼匆赃。
有兩種方法淤毛,一種是利用UnitySendMessage來(lái)實(shí)現(xiàn);一種是利用delegates算柳;
第一種UnitySendMessage:
這種方式要簡(jiǎn)單一些低淡,但是會(huì)有一些限制
使用UnitySendMessage("GameObjectName1", "MethodName1", "Message to send"); 方法
這種方式需要場(chǎng)景中有一個(gè)對(duì)應(yīng)的名字“GameObjectName1”的GameObject,同時(shí)該對(duì)象上掛載了一個(gè)繼承自MonoBehaviour的腳本瞬项,該腳本中有一個(gè)名為“MethodName1”的方法蔗蹋,最后一個(gè)參數(shù)為傳遞的參數(shù),可有可無(wú)滥壕。
注意:這種方法是異步的纸颜,而且會(huì)延遲一幀執(zhí)行
第二種 delegates
C#端的對(duì)應(yīng)接口需要,以如下形式添加
//添加MonoPInvokeCallback绎橘;C++中 會(huì)有一個(gè)對(duì)應(yīng)的 OnViewConfirm 方法指針(名字要一致)
[MonoPInvokeCallback(typeof(OnViewConfirm))]
static void calledByNativeC() { //該方法必須為靜態(tài)函數(shù)
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); //這里把對(duì)應(yīng)的C#回調(diào)函數(shù)傳給C++
}
對(duì)應(yīng)的 C++ (.cpp)或者 Objective-C++(.mm) ,(Assets\Plugins\iOS\iOSPlatformHelper)中需要
#include <stdlib.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef void (*OnViewConfirm)(); //定義對(duì)應(yīng)的指針函數(shù)
static OnViewConfirm s_calledFromCSharp = NULL;
void showAlertDialog_native()
{
...
if (s_calledFromCSharp != NULL)
{
s_calledFromCSharp(); //觸發(fā)對(duì)應(yīng)的回調(diào)函數(shù)
}
...
}
void showAlertDialog(const char *title, const char *message, const char **buttons, const char **urls, int buttonCount, OnViewConfirm viewClosed)
{
...
s_calledFromCSharp = viewClosed; //接收C#傳過(guò)來(lái)的函數(shù)指針
...
showAlertDialog_native();
}
最后順便提一下胁孙,著這種平臺(tái)方法的整合
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;
}
}
//各種版本號(hào),用戶id称鳞,國(guó)家涮较,原生彈窗等的屬性或者方法
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{}
相關(guān)內(nèi)容:
https://docs.unity3d.com/2020.3/Documentation/Manual/PluginsForIOS.html
http://www.reibang.com/p/0c61444c9b28
利用第一種方式實(shí)現(xiàn)的原生iOS窗口調(diào)用方法
http://www.reibang.com/p/75cef8007e75