開發(fā)
通過(guò)C代碼及函數(shù)指針回調(diào)方式:
iOS代碼蚂夕,在SDK中創(chuàng)建.mm文件:
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "TWUnityBridgeSDK.h"
#if defined(__cplusplus)
extern "C"
{
#endif
///Unity調(diào)用iOS通用方法
extern void unityToiOS(const char* jsonStr);
///定義函數(shù)指針
typedef void(*CALLBACK_TOUnity)(const char*);
///iOS調(diào)用Unity通用函數(shù)指針對(duì)象
static CALLBACK_TOUnity callback_to_unity = nil;
///Unity注冊(cè)回調(diào)方法
void iosCallBacktoUnity(CALLBACK_TOUnity callback);
#if defined(__cplusplus)
}
#endif
#ifdef DEBUG
#define NSLog(format, ...) printf("[%s] %s [第%d行] %s\n", __TIME__, __FUNCTION__, __LINE__, [[NSString stringWithFormat:format, ## __VA_ARGS__] UTF8String]);
#endif
///Unity調(diào)用iOS通用方法
void unityToiOS(const char* jsonStr)
{
// 創(chuàng)建NSString從C字符串
NSString* str = [NSString stringWithUTF8String:jsonStr];
[TWUnityBridgeSDK unityToiOSWithJson:str callBackHandler:^(NSString *data, NSError *error) {
if(callback_to_unity != nil) {
NSLog(@"iOSToUnity=======---%@", data);
char* commandchar = (char*) [data UTF8String];
callback_to_unity(commandchar); //字符串發(fā)給C#
} else {
NSLog(@"callback_to_unity為空=======444---%@", data);
}
}];
}
///Unity注冊(cè)回調(diào)方法
void iosCallBacktoUnity(CALLBACK_TOUnity callback)
{
callback_to_unity = callback;
if (callback == nil) {
NSLog(@"Unity注冊(cè)回調(diào)失敗=======");
}
}
Unity代碼
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
using AOT;
public class SDKIOS : MonoBehaviour
{
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void callbackN0Delegate(IntPtr data);
[DllImport("__Internal")]
public static extern void iosCallBacktoUnity(IntPtr callback);
[DllImport("__Internal")]
private static extern void unityToiOS(string str);
// Start is called before the first frame update
public void Start()
{
RegisterCallback();
// 調(diào)用外部方法,傳遞字符串參數(shù)到iOS
unityToiOS("{\"type\": 1, \"data\": {\"msg\": \"調(diào)用iOSSDK初始化\",\"appName\": \"兔玩\"}}");
}
public void testBtnClick()
{ //測(cè)試跳轉(zhuǎn)原生H5
// unityToiOS("{\"type\": 3, \"data\": {\"url\": \"https://y-test.tuwan.com/activity/483Cea4771?v=1717391952164#/\", \"isPopView\":0}}");
//測(cè)試調(diào)用原生支付
unityToiOS("{\"type\":3,\"data\":\"{\\\"json\\\":\\\"{\\\\\\\"goodsId\\\\\\\":\\\\\\\"1000020\\\\\\\",\\\\\\\"orderNo\\\\\\\":\\\\\\\"51df30b7c41b4b02adb309a687ef1356\\\\\\\",\\\\\\\"orderToken\\\\\\\":\\\\\\\"67b36e731657f3627ade429e53cd42ab\\\\\\\",\\\\\\\"data\\\\\\\":{\\\\\\\"price\\\\\\\":18,\\\\\\\"bewrite\\\\\\\":\\\\\\\"126鉆石\\\\\\\"},\\\\\\\"platform\\\\\\\":0,\\\\\\\"typeId\\\\\\\":2129}\\\",\\\"payPoint\\\":\\\"d_diandian2\\\"}\"}");
//測(cè)試調(diào)用隱私協(xié)議彈窗
}
private void RegisterCallback()
{
callbackN0Delegate callbackN0_delegate = SendDataToGame;
//將Delegate轉(zhuǎn)換為非托管的函數(shù)指針
IntPtr intptrN0_delegate = Marshal.GetFunctionPointerForDelegate(callbackN0_delegate);
//調(diào)用非托管函數(shù)
iosCallBacktoUnity(intptrN0_delegate);
}
//原生層發(fā)送數(shù)據(jù)到游戲?qū)? [MonoPInvokeCallback(typeof(callbackN0Delegate))]
static void SendDataToGame(IntPtr data)
{
var dataStr = Marshal.PtrToStringAnsi(data);
Debug.Log(dataStr);
}
}