Hi there!
I’ve been working on a cocoa based frontend for Unity, in order to be able to use GUI built in Xcode on top of my Unity games / apps.
The frontend is mostly based on the blurst tutorial over at http://technology.blurst.com/a-cocoa-based-frontend-for-unity-iphone-applications/ and I got it up and running, working like a charm to launch the app on my cocoa frontend, switch over to Unity, and add UIViews on top of the unity view to control my game.
I have, however, run into a problem that is giving me gray hairs.
While im in my cocoa frontend main view (the main menu of the game), I want to display modal view controllers, such as Popover views on the iPad. Bringing them up works well, but my app is running in both landscape right left orientations, and when I rotate the iPad the modal views do not rotate. I can see my main view rotating in the background when using form sheet modal views for example, but it is being completely ignored by the actual modal view.
Weird thing is, If I close the app down into the background and open it again, the modal views have been rotated.
Also, sometimes, the orientations does change once for the modal views, and then stop working.
Now, when I launch my app, I launch both my frontend, then load up Unity but hide it straight away (so it will be faster to just load a scene later, and I wont get the double loading screens). If I change this to not load Unity but only my frontend, the orientations are working as intended.
My code for launching the frontend is looking like this:
/*
* Launches the frontend menu system
*/
- (void)launchFrontend
{
// re-sync the PlayerPrefs file
[FBPlayerPrefs readPrefsFile];
// Create the window if we don't already have one
if([UIApplication sharedApplication].keyWindow == nil)
{
NSLog(@"Creating new window");
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
testView = [[MainMenuViewController alloc] initWithNibName:@"MainMenuViewController" bundle:nil];
[window addSubview:testView.view];
//Pre-load unity before app launches, then put frontend back in control
[self launchUnity:@""];
}
// Check to see if any views are exclusive/multitouch (ie find the Unity
// EAGLView). Temporarily disable it, and tag it so we can find it later
for(UIView *v in window.subviews)
{
NSLog(@"Found View %@", v);
if(v.exclusiveTouch v.multipleTouchEnabled)
{
NSLog(@"Found Unity view");
v.exclusiveTouch = NO;
v.multipleTouchEnabled = NO;
v.tag = UNITY_VIEW_TAG;
v.hidden = YES;
}
}
[window addSubview:testView.view];
}
Launching Unity runs like this:
- (void)launchUnity:(NSString *)scene
{
// Tell Unity loading scene to stop holding
[FBPlayerPrefs setInt:1 withKey:@"_start_unity"];
[FBPlayerPrefs saveAndUnload];
// If we've not yet started the Unity content, run its startup
if(unityController == nil)
{
unityController = [[AppController alloc] init];
[unityController applicationDidFinishLaunching:[UIApplication sharedApplication]];
const char *theScene = [scene UTF8String];
UnitySendMessage("NativeBridge", "LoadScene", theScene);
UIApplication *app = [UIApplication sharedApplication];
window = [app keyWindow];
}
// If we've already got Unity content running, show it and return its
// Exclusive/multitouch status
else
{
for(UIView *v in window.subviews)
{
if(v.tag == UNITY_VIEW_TAG)
{
//Remove any old UI elements if they exist
for (UIView *view in [[[[UIApplication sharedApplication] keyWindow] rootViewController].view subviews]) {
[view removeFromSuperview];
[view release];
NSLog(@"Removed UI element");
}
[FBPlayerPrefs setInt:1 withKey:@"_add_gui"];
const char *theScene = [scene UTF8String];
v.exclusiveTouch = YES;
v.multipleTouchEnabled = YES;
v.hidden = NO;
[window bringSubviewToFront:v];
}
}
}
}
The key part in the above method being [unityController applicationDidFinishLaunching:[UIApplication sharedApplication]]; where I launch the unity views.
If I comment out [self launchUnity:@“”]; from the frontend launch, the rotations for modal views are working as intended.
UIViews that I add as subviews to the Unity view all rotate as expected.
Does anyone have any idea what might be causing this behaviour? I will be grateful for any pointers or suggestions, I’m quite new to Unity still, so got a lot to learn.