#import "SDKManager.h" #import // douyou SDK #include "UnityInterface.h" // Example header file where UnitySendMessage is declared @implementation SDKManager //单例构造当前类 + (instancetype)sharedInstance { static SDKManager *sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[SDKManager alloc] init]; sharedInstance.shouldLog = YES; // 初始值为 YES 或者根据需求设定 }); return sharedInstance; } //实例化sdk - (void)InitWithDigitMap:(NSString *)gameId ring:(NSString *)adId shouldLog:(BOOL)shouldLog{ if (shouldLog != self.shouldLog) { self.shouldLog = shouldLog; } [[Promotion stillInstance] moleGetGray:gameId swap:adId invitedBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"初始化成功:%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"InitSuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"初始化失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"InitError"]; }]; } //调起登陆 - (void)Login { [[Promotion stillInstance] sleetHairBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"登录成功:%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LoginSuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"登录失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LoginError"]; } justRainMouthBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"切换账号回调"); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"SwitchAccounts"]; }]; } //退出登陆 - (void)Logout { [[Promotion stillInstance] forSwedishBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"登出成功%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"LogoutSuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"登出失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"LogoutError"]; }]; } // 调起支付 - (void)PaymentWithJson:(NSDictionary *)paymentInfo { [[Promotion stillInstance] wrapHasBlock:paymentInfo invitedBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"支付成功:%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"PaySuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"支付失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"PayError"]; }]; } // 角色升级上报 - (void)RoleInfoWithJson:(NSDictionary *)roleInfo { [[Promotion stillInstance] fusionRhythmBlock:roleInfo invitedBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"角色升级上报成功:%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportSuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"角色升级上报失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"RoleInfoReportError"]; }]; } // 跳转URL - (void)SdkJumpUrl { [[Promotion stillInstance] JumpUrlWithBlock:^(NSDictionary *params) { if (self.shouldLog) { NSLog(@"跳转成功%@", params); } [self sendJsonToUnity:params gameObject:"IosReceiverGameObj" methodName:"JumpUrlSuccess"]; } failureBlock:^(NSError *error) { if (self.shouldLog) { NSLog(@"跳转失败:%@", error); } [self sendErrorToUnity:error gameObject:"IosReceiverGameObj" methodName:"JumpUrlError"]; }]; } //发送正常消息给unity - (void)sendJsonToUnity:(NSDictionary *)jsonData gameObject:(const char *)gameObject methodName:(const char *)methodName { NSError *error; NSData *json = [NSJSONSerialization dataWithJSONObject:jsonData options:0 error:&error]; if (!json) { if (self.shouldLog) { NSLog(@"Error converting dictionary to JSON: %@", error.localizedDescription); } return; } NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; const char *jsonStringC = [jsonString UTF8String]; UnitySendMessage(gameObject, methodName, jsonStringC); } //发送报错消息给unity - (void)sendErrorToUnity:(NSError *)error gameObject:(const char *)gameObject methodName:(const char *)methodName { NSDictionary *errorInfo = @{ @"error_code" : @(error.code), @"error_message" : error.localizedDescription // You can add more details from NSError if needed }; NSError *jsonError; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:errorInfo options:0 error:&jsonError]; if (!jsonData) { if (self.shouldLog) { NSLog(@"Error converting error info to JSON: %@", jsonError.localizedDescription); } return; } NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; const char *jsonStringC = [jsonString UTF8String]; UnitySendMessage(gameObject, methodName, jsonStringC); } @end