After few weeks to learn the integration between unity3d cocoa, finally found that it is not needed to modify any code from the original xCode sample project.
http://www.youtube.com/watch?v=eitY8-XFLKo
And the following made that happen… Oh, May I lazy forever XD
#import "UCC_Callback.h"
#import "MTJSON.h"
#import "RootViewController.h"
#import "SeismicXMLAppDelegate.h"
#define UCC_TAG 24601
void UCC_Echo(const char * bytes)
{
//----- Echo
NSString *str = [NSString stringWithUTF8String:bytes];
NSLog(@"-----> UCC_Echo:%@", str);
[UCC_Callback gameobject:@"readout" func:@"json" mms:[NSString stringWithFormat:@"{echo:%@}", str]];
}
void UCC_Remote(const char * bytes)
{
//----- for UILabel
NSString *str = [NSString stringWithUTF8String:bytes];
NSLog(@"-----> UCC_Remote:%@", str);
//----- for Unity3d
UIWindow *window = [UIApplication sharedApplication].keyWindow;
NSLog(@"-----> UCC_Remote:%@", window);
for(UIView *v in window.subviews)
{
NSLog(@"-----> UCC_Remote:%@", v);
if(v.exclusiveTouch v.multipleTouchEnabled)
{
v.exclusiveTouch = NO;
v.tag = UCC_TAG;
}
}
UIView *u3d_view = [window viewWithTag:UCC_TAG];
if(u3d_view)
{
CGRect rect = [u3d_view bounds];
UIView *ucc_view = [[UIView alloc] initWithFrame:rect];
[u3d_view addSubview:ucc_view];
NSLog(@"-----> UCC_Remote:%@", u3d_view);
//----- UIView
float w = rect.size.width;
float h = rect.size.height;
UIView *ui_view = [[UIView alloc] initWithFrame:CGRectMake(0,h/2,w,h/2)];
//----- SeismicXML
RootViewController *rootvc = [[RootViewController alloc ] init];
UINavigationController *nav = [[ UINavigationController alloc] init];
[nav pushViewController:rootvc animated:NO];
[ui_view addSubview:nav.view];
nav.view = ui_view;
SeismicXMLAppDelegate *s = [[SeismicXMLAppDelegate alloc]init];
s.window = window;
s.rootViewController = rootvc;
s.navigationController = nav;
[s applicationDidFinishLaunching:[UIApplication sharedApplication]];
//----- UILabel
CGRect rect2 = CGRectMake(w/3*2,h/16*7.5,w,h/16);
UILabel *scoreLabel = [[UILabel alloc ] initWithFrame:rect2 ];
scoreLabel.text = str;
scoreLabel.textColor = [UIColor whiteColor];
scoreLabel.backgroundColor = [UIColor blackColor];
scoreLabel.alpha = 0.75;
[[nav.view superview] insertSubview:scoreLabel aboveSubview:nav.view];
//------ Release
[scoreLabel release];
[rootvc release];
[nav release];
[ui_view release];
[ucc_view release];
}
}
For pass back the data to Unity, it’s only need 2 line of code insert to the original project.
[SeismicXMLAppDelegate.h]
#import "UCC_Callback.h"
[SeismicXMLAppDelegate.m]
- (void)addEarthquakesToList:(NSArray *)earthquakes {
// insert the earthquakes into our rootViewController's data source (for KVO purposes)
[self.rootViewController insertEarthquakes:earthquakes];
[UCC_Callback sendMessage:earthquakes]; // Yeah ! That's all XD
}
Plus, a new callback class for translate the data.
[UCC_Callback.h]
@interface UCC_Callback : NSObject
{
}
+(void)sendMessage:(NSArray *)earthquakes;
+(void)gameobject:(NSString *)gameobject func:(NSString *)func mms:(NSString *)mms;
@end
[UCC_Callback.m]
#import "UCC_Callback.h"
#import "MTJSON.h"
#import "Earthquake.h"
@implementation UCC_Callback
+(void)sendMessage:(NSArray *)earthquakes
{
Earthquake *e;
NSDictionary *dict;
NSMutableArray *json = [NSMutableArray array];
NSInteger n = [earthquakes count];
NSInteger i;
for (i = 0; i < n; i++)
{
e = [earthquakes objectAtIndex:i];
NSString *usgs_url = [e.USGSWebLink absoluteString];
NSString *location = e.location;
NSString *latitude = [NSString stringWithFormat:@"%f", e.latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", e.longitude];
NSString *magnitude = [NSString stringWithFormat:@"%f", e.magnitude];
dict = [NSDictionary dictionaryWithObjectsAndKeys: usgs_url, @"usgs_url",
location, @"location",
latitude, @"latitude",
longitude, @"longitude",
magnitude, @"magnitude", nil];
[json insertObject:dict atIndex:i];
}
//----- Json
NSString *str = [NSString stringWithFormat:@"{\"feed\":%@}", [MTJSON MTJSONObjectToString:json]];
[self gameobject:@"readout" func:@"json" mms:str];
}
+(void)gameobject:(NSString *)gameobject func:(NSString *)func mms:(NSString *)mms
{
#if !(TARGET_IPHONE_SIMULATOR)
UnitySendMessage([gameobject UTF8String], [func UTF8String], [mms UTF8String]);
#endif
NSLog(@"-----> UCC_Callback:%@", mms);
}
@end
Yes, that’s all converted to JSON ! So that, I could use the same code for web deployment too ! Lazy ![]()
Remark: I use MTJSON to do the JSON conversion, also, I made a port version for it to run inside the unity too.
https://github.com/mysterioustrousers/MTJSON
