Last active
October 18, 2019 10:24
-
-
Save mwrites/1503aee29d75cc0beb451fa176a1333a to your computer and use it in GitHub Desktop.
Bridge.mm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// Bridge.m | |
// Unity-iPhone | |
// | |
// Created by Mathieu Tan on 2019/10/17. | |
// | |
#import "UnityString_C++.h" | |
#import "UnityInterface.h" | |
extern "C" | |
{ | |
id NativeJSONObject(NSData *data) { | |
if (!data) { | |
return nil; | |
} | |
NSError *error = nil; | |
id retId = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | |
if (error) { | |
NSLog(@"%s trans data to obj with error: %@", __func__, error); | |
return nil; | |
} | |
return retId; | |
} | |
void iOSAsyncCall(const char *gameObject, const char * callbackMethod, const char *arg) { | |
NSString *str = NSStringFromUnityString(arg); | |
NSLog(@"iOS received char %@", str); | |
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding]; | |
NSDictionary *dic = NativeJSONObject(data); | |
NSLog(@"iOS received dic %@", [dic debugDescription]); | |
// NSMutableDictionary *newDic = [NSMutableDictionary dictionaryWithDictionary:dic]; | |
// NSString *nsstringMessage = [NSString stringWithCString:arg encoding:NSUTF8StringEncoding]; | |
NSError *error; | |
NSData *json = [NSJSONSerialization dataWithJSONObject:@{@"integer": @0, @"boolean": @0, @"str": @"world"} options:NSJSONWritingPrettyPrinted error:&error]; | |
if (error) { | |
NSLog(@"error serializing %@", dic); | |
} | |
NSString *jsonString = [[NSString alloc] initWithData:json encoding:NSUTF8StringEncoding]; | |
NSLog(@"iOS preparing to send back message %@", jsonString); | |
UnitySendMessage(gameObject, callbackMethod, UnityStringFromNSString(jsonString)); | |
} | |
void iOSSendString(const char * message) { | |
NSString *nsStringMessage = NSStringFromUnityString(message); | |
NSLog(@"Unity told me: %@", nsStringMessage); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment