Mini Tutorial: How to post score of your Unity iOS game to Facebook?

-without shelling out $65 😉

Well, if you have $65 to spare, just check out Prime31’s Social Networking plugin (you can even get Twitter!).

Link: http://www.prime31.com/unity/

If you don’t, like 1-broke-girl/me, read on…

There are two parts to this, Unity side and Xcode side. We must find a way for Unity and Xcode to be friends and talk to one another, you know call each others’ functions, access each others’ variables etc.

First let’s take advantage of NSUserDefaults and PlayerPrefs to save some variables (you may encrypt the score variable if you are afraid of cheaters).

PlayerPrefs in Unity…

PlayerPrefs.SetString(“score”, score.ToString());

… can be read in Xcode using…

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *score = [defaults objectForKey:@”score”];

There, we know how to pass variables, what about functions…?

In order to do that, I read this tut, which is in Simplified Chinese!  (http://xys289187120.blog.51cto.com/3361352/705415). The gist of that tutorial is that you create this other class (let’s just call it Facebook.cs):

using UnityEngine;  
using System.Runtime.InteropServices;  

public class Facebook : MonoBehaviour {

    [DllImport(“__Internal”)]  
     private static extern void _PressButton0 ();  

     public static void ActivateButton0 ()  
     {  if (Application.platform != RuntimePlatform.OSXEditor)   
        {   _PressButton0 ();  
        }  
     }  
}

The _PressButton0() will actually call some code in Xcode (we’ll get to that).

Someone else in Unity has to call ActivateButton0, a GUI button, perhaps?

if(GUI.Button(new Rect(0, 0, 130, 235), “Facebook”))
{       Facebook.ActivateButton0();
}

So when the player clicks on the GUI button, ActivateButton0() will be called which will in turn call _PressButton0().

But where’s _PressButton0()?

We create a ViewController class in Xcode (let’s just call it MyView.m):

#import “MyView.h”
#import “AppController.h”

@implementation MyView

void _PressButton0()  
{   AppController *appController = (AppController*)[[UIApplication sharedApplication] delegate];

    [appController feedDialogButtonClicked];
}  

@end

So there’s _PressButton0()!

Now let’s do the Facebook related things. Go to Facebook’s Developer site and follow the tutorial: https://developers.facebook.com/docs/mobile/ios/build/

Instead of putting some stuff in ApplicationDidFinishLaunchingWithIOptions… I placed everything in a function I called feedDialogButtonClicked.

– (void) feedDialogButtonClicked {
   
    facebook = [[Facebook alloc] initWithAppId:@”221872691249521” andDelegate:self];
   
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@”FBAccessTokenKey”]
        && [defaults objectForKey:@”FBExpirationDateKey”]) {
        facebook.accessToken = [defaults objectForKey:@”FBAccessTokenKey”];
        facebook.expirationDate = [defaults objectForKey:@”FBExpirationDateKey”];
    }
   
    /**
    if (!)
    {   ;
    }
    **/
   
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSString *level = [defaults objectForKey:@”level”];
    NSString *score = [defaults objectForKey:@”score”];
   
    NSMutableDictionary *params =
    [NSMutableDictionary dictionaryWithObjectsAndKeys:
     [NSString stringWithFormat: @”I just scored %@ in the %@ Level of Maru Penguin!”, score, level], @”name”,
     @”“, @”caption”,
     @”Get Maru Penguin for free in the iTunes Store”, @”description”,
     @”http://itunes.apple.com/tw/app/maru-penguin/id521096937?mt=8”, @”link”,
     @”http://a2.mzstatic.com/us/r1000/082/Purple/v4/3a/42/b5/3a42b5dc-5452-9dde-cdc8-24e24fb82363/486SkNsbbo2zaF3glfCuo0-temp-upload.iomrjeon.320×480-75.jpg”, @”picture”,
     nil]; 
    ;
}

I had a little problem with fbDidLogin (the one mentioned in the tutorial), good thing this other tutorial solved it for me: http://ebrentnelson.blogspot.com/2012/02/fbdidlogin-never-calledwhy.html

I ended up commenting out:

/**
    if (!)
    {   ;
    }
    **/

Because I seem to be able to post feeds to my Facebook wall even without it (don’t know why, anyone care to explain?).

Another helpful link: How to include a link in my feed-post using FBConnect from iPhone app? (http://stackoverflow.com/questions/5574433/how-to-include-a-link-in-my-feed-post-using-fbconnect-from-iphone-app) This answer in this post explains stuff that you can include in your Feed Dialog pretty clearly.

And um, I think that is it. That bunch of codes can post your scores from Unity iOS to Facebook.

Now my other problem is, how to add a share link to the feed my app posted? Anyone, help?

Also check out an old blog post of mine about how to post pictures from Cocos2D iPhone to Facebook: http://purplelilgirl.tumblr.com/post/9406805856/howtoaddfacebooktococos2diphone

EDIT:

Since generated feeds don’t get the share button (I Google-d for 2 days and found nothing, at the end of it, it was a Which Avengers are You? quiz that helped me solve my problem) , do you know what I eventually did? I went back to that Cocos2D blog post and did it that way (That’s what the Avengers app did, by the way, I’m Hawkeye :D). I posted a photo of the results screen, lol, which is actually what my boss suggested in the first place. And since when you post photos, you can include captions (no advertising though), so there. Problem solved -ish.

EDIT:

Okay, I am apparently not a very good Googler, since only saw this today: http://forum.unity3d.com/threads/122681-Free-facebook-Plugin-for-Unity-iOS Free, Facebook, Unity, iOS, all the keywords that I’ve been searching for, all along was in the Unity Forums!

.

.

.

By the way, we made an app (the one in the sample), it’s a game and it’s free and it stars a penguin named Maru in search of yummy fishies around the world (so far he only got to Asia)…

Link: http://itunes.apple.com/tw/app/maru-penguin/id521096937?mt=8

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s