I keeped getting asked about this privately, so I’m going to post a way to do it here. But I would really recommend using the Enhancement Pack or holding off until 1.1 comes out. (HiggyB: feel free to kill this if I’m stepping out of line)
This works for iPhone Basic. I have several other things going on in my code, but this should be close to working code. This will get you set up for launching the OpenFeint dashboard from within Unity and give you an idea of how to hop in/out of Unity and pass data back/forth.
AppController.h should look something like this:
#import "OpenFeint.h"
// Notice the OpenFeintDelegate
@interface AppController : NSObject<UIAccelerometerDelegate, OpenFeintDelegate>
{
UIWindow* _window;
bool _inUnity; //<- this is new too
}
@end
What’s going on here is a simple safety flag is added to prevent nasty things from happening if we double up our calls on the script side and also to short circuit the Repaint if we don’t need it. We’re also an OpenFeint delegate so, at a minimum, we’ll know when the dashboard closes.
AppController.mm:
Add these functions to the file (just below the accelerometer function and before the @implementation EAGLView)…
-(void) launchUnity
{
if (_inUnity)
return;
NSLog(@"AppController::launchUnity\n");
_inUnity = true;
[[UIApplication sharedApplication] setStatusBarOrientation: UIInterfaceOrientationPortrait];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_openfeint_dashboard"];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"_start_unity"];
UnitySetAudioSessionActive(true);
UnityPause(false);
}
- (void) launchFeint
{
if (!_inUnity)
return;
_inUnity = false;
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_start_unity"];
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"_openfeint_dashboard""];
UnitySetAudioSessionActive(false);
UnityPause(true);
[OpenFeint launchDashboardWithDelegate:self];
}
- (void) dashboardDidDisappear
{
[self launchUnity];
}
That takes care of switching in/out of Unity at will. Now we need to init OpenFeint. So find the applicationDidFinishLaunching function and make it something like this:
- (void) applicationDidFinishLaunching:(UIApplication*)application
{
printf_console("-> applicationDidFinishLaunching()\n");
[application setStatusBarHidden:TRUE];
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"_openfeint_dashboard"];
_inUnity = true;
char const* appPath = [[[NSBundle mainBundle] bundlePath]UTF8String];
UnityInitApplication(appPath);
// Obviously you'll need your credentials here!
[OpenFeint initializeWithProductKey:@"xxxxxxxxxxxx" andSecret:@"xxxxxxxxxxxx" andDisplayName:@"UnityApp" andSettings:nil andDelegate:self];
_timer = [NSTimer scheduledTimerWithTimeInterval:(1.0 / kFPS) target:self selector:@selector(Repaint) userInfo:nil repeats:YES];
[self registerAccelerometer];
}
Now we need to monitor the PlayerPrefs that may be set in Unity script and react accordingly. Find the Repaint function and add this logic to it…
- (void) Repaint
{
if (!_inUnity)
return;
if ([[NSUserDefaults standardUserDefaults] integerForKey:@"_openfeint_dashboard"] == 1)
[self launchFeint];
For build settings: go to “Get Info” on your project in Xcode. Click on the build tab, select all configurations, and search for “dtors”. Check the box for that setting. Now search for “other linker”. See how it says ? Don’t add to that!!! You have to deal with Debug/Release separaely. Select Release in the Configuration drop down and add -ObjC to the linker flags. Now select Debug and add -ObjC to the linker flags.
The easiest way to add the libraries is to right click on your App in the Targets list and “Add Existing Frameworks” and click on the [+] in that dialog. Hint: the SQLLite lib will be under the Dylibs category.
Shwew. I think that’s it. Now in your Unity script, just do PlayerPrefs.SetInt("_openfeint_dashboard", 1); whenever you want to pause Unity and bring up OpenFeint. 8)
Some caveats: this is just a primer to get you started. I would really think about the performance implications of checking tons of PlayerPrefs in the Repaint thread. All this code will do is launch OpenFeint, I leave it as an exercise to the reader on submitting high scores and achievements.
Finally, you’ll want a PostprocessBuildPlayer script to copy your files for you when you build in Unity so you can keep your modifications. (I’m not really a fan of having the script inject code)
I’m not a part of the 1.1 Beta, so I have no idea how much this will break when 1.1 is out.