Unity3d+Cocoa Integration Test 01 ( Yes, I am lazy XD )

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 :stuck_out_tongue:

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

Wow… How is this hosted within unity?

Thanks! This is really interesting!

BR,
Juha

Well, this picture tell all the story :slight_smile:

The files marked as green are copy from the apple’s sample project, the orange are newly made for the unity cocoa and let them work together. finally the blue one are for the json conversions.

and this is the plug-in for unity talk to the outside world.

[UCC.cs]

using UnityEngine;
using System.Runtime.InteropServices;

public class UCC
{
	//----- UCC_Echo	
	[DllImport ("__Internal")]
   	private static extern void UCC_Echo(string mms);
   
  	public static void Echo(string mms)
  	{
  		if (Application.platform == RuntimePlatform.IPhonePlayer)
  		{ 			
  			UCC_Echo(mms);
  		}
  		else
  		{
  			Debug.Log("-----> UCC_Echo:"+mms);
  		}
  	}
	
	//----- UCC_NSRemote	
	[DllImport ("__Internal")]
   	private static extern void UCC_Remote(string mms);
   
  	public static void Remote(string mms) 
  	{ 	
  		if (Application.platform == RuntimePlatform.IPhonePlayer)
  		{ 			
  			UCC_Remote (mms);
  		}
  		else if (Application.isEditor)
  		{
  			//TextAsset feed = (TextAsset) Resources.Load("feed");
  			//GameObject.Find("readout").SendMessage("json", feed.text );  			
  			//Debug.Log(feed.text);
  			
  			Debug.Log("-----> UCC_Remote:"+mms);
  			
  			string str = "{\"feed\":[{\"magnitude\":\"3.500000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/ak10277169.php\",\"location\":\"Kodiak Island region, Alaska\",\"latitude\":\"58.416800\",\"longitude\":\"-153.504200\"},{\"magnitude\":\"2.600000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/pr11205000.php\",\"location\":\"Virgin Islands region\",\"latitude\":\"18.316900\",\"longitude\":\"-64.822900\"},{\"magnitude\":\"4.900000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc00053a9.php\",\"location\":\"offshore Tarapaca, Chile\",\"latitude\":\"-20.169400\",\"longitude\":\"-70.591900\"},{\"magnitude\":\"5.000000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc0005398.php\",\"location\":\"offshore Tarapaca, Chile\",\"latitude\":\"-20.142900\",\"longitude\":\"-70.643600\"},{\"magnitude\":\"3.000000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/ci10980541.php\",\"location\":\"Baja California, Mexico\",\"latitude\":\"32.126300\",\"longitude\":\"-115.234300\"},{\"magnitude\":\"2.800000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/ci10980525.php\",\"location\":\"Southern California\",\"latitude\":\"33.220500\",\"longitude\":\"-116.097300\"},{\"magnitude\":\"5.500000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc000534v.php\",\"location\":\"Guam region\",\"latitude\":\"13.031300\",\"longitude\":\"145.453900\"},{\"magnitude\":\"4.300000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc000534d.php\",\"location\":\"Alaska Peninsula\",\"latitude\":\"54.751200\",\"longitude\":\"-161.060900\"},{\"magnitude\":\"4.900000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc000533x.php\",\"location\":\"Kepulauan Barat Daya, Indonesia\",\"latitude\":\"-7.385100\",\"longitude\":\"128.440300\"},{\"magnitude\":\"4.900000\",\"usgs_url\":\"http://earthquake.usgs.gov/earthquakes/recenteqsww/Quakes/usc000533r.php\",\"location\":\"Costa Rica\",\"latitude\":\"10.656500\",\"longitude\":\"-85.844200\"}]}";

  			
  			GameObject.Find("readout").SendMessage("json", str );
   		}
  	}
}

646963--23124--$process.png

Genius Apple_Motion! On a different note, I sent you a private message. Cheers!

This is very interesting. Nice work.

Best Regards,
Matt.

Really great work. Is it possible to overlay the unity ‘viewport’ on top of webkit?

I mean overlay unity on a webpage like an ‘iframe’. So, like a ‘free’ canvas?