Unity & Facebook Integration: Part 1

Unity & Facebook Integration: Part 1

I have finally decided that my upcoming game will eventually have some social media integration, so I started working on a small proof of concept.

In this first part, I’ll be using a basic example to generate a build for Android and test in on an actual device. So let’s start with the basics.

Step 1: Create Facebook Application
  • Create a Facebook application through this link
  • Once created, you should get something like the following page:

Screen Shot 2016-04-29 at 11.21.10 PM

Step 2: Setup Facebook SDK for Unity

This will download a zip file containing a file named “facebook-unity-sdk-x.y.z.unitypackage”. I used the 7.5.0 release.

  • Import the package in Unity (all files), be that on a new project or an existing one.

I recommend you to start a new project. I will be just building an existing scene from the package.

To import a package in Unity, go to Assets > Import Package > Custom Package
and select the .unitypackage file.

You can also download the package directly through the asset store.

  • Notice a new “Facebook”option appeared on the top menu

Screen Shot 2016-04-29 at 11.53.36 PM.png

  • Go to Facebook > Edit Settings

Fill in an App Name, and copy and paste your App Id from the facebook application into the App Id field.

Step 3: Open example scene
  • In the Project pane, open the scene named “MainMenu” within the folder FacebookSDK > Examples
  • Go to File > Build Settings…
  • With the Build Settings modal opened, drag & drop all scenes fiels from FacebookSDK > Examples

Make sure “MainMenu” scene is at the top, with index = 0

You can go ahead and hit play on the editor, but nothing will work. I guess I can look into this for a new post in the future.

In this case, I will continue with Android specific steps. If you are looking for other platforms, you can check the following:

Other platforms are not yet supported, at least for the time being. The SDK is in BETA phase.

Step 4: Configure Android platform
  • Switch current platform to Android – File > Build Settings… > Platform > Android > Switch Platform

Prerequisite: Have Android SDK set in Unity

  • Unity > Preferences > External Tools > Browse and select the sdk folder from your file system
  • Set Android Minumum API Level

Go to File > Build Settings… > Player Settings…

Under Other Settings > Identification > Minumim API Level, select at least Android 4.0.3 or up.

  • Go to Facebook > Edit Settings and copy the Debug Android Key Hash
  • Back in your Facebook application, add Android platform
  • Paste the key in the Key Hashes field
  • Add a valid Google Play Package Name
  • Under Class Name, add “com.facebook.unity.FBUnityDeepLinkingActivity”
  • That’s it!

Save your scene, build the apk and transfer it to the device.

Step 4.5: A bump in the way, not cool (optional)

I had a problem at the last step, while generating the apk. The build failed.

I had the following error message:

“Unable to merge android manifest”

And some part in the error stack trace I found:

stdout[

Warning:[Temp\StagingArea\AndroidManifestmain.xml:25, D:\UnityWorkspace\FbTest\Temp\StagingArea\androidlibraries\facebookandroidwrapperrelease\AndroidManifest.xml:3] Main manifest has <usessdk android:targetSdkVersion=’22’> but library uses targetSdkVersion=’23’]

Not Cool.

To fix this:

  • Open the Android SDK Manager.
  • Download Android 6.0 (API 23)
  • Restart Unity
Step 5: Enjoy

You will now be able to see the following application:

Screenshot_2016-04-29-22-05-50

First tap FB.Init button. This will initialise the sdk.

Once its done (and succeeded), you will be able to login, logout, add write permissions to submit a link or picture, retrieve profile information and graph requests, among others.

fb post

Screen Shot 2016-04-30 at 1.02.14 AM.png

Share a link. Piece of cake!

Pretty cool huh? 🙂

Next Steps

For the next part, I will continue using the sdk but actually creating some content. Peace out!


 

Did you have any other problem? Did you like this?

Be my guest and leave a comment below!

References

https://developers.facebook.com/docs/unity/gettingstarted

https://developers.facebook.com/docs/unity/getting-started/android

http://answers.unity3d.com/questions/1088446/unable-to-merge-android-manifest.html

 

 

Advertisements

Look Mom, I have a blog!

 

TL; DR;

I will be sharing my experiences while making websites and games

Wow, I always wanted to write my own tl;dr; !, though I hope you guys keep on reading.

This is my first time doing something like this, so I think I should start by letting you know who I am and what I will be writing about.

I’m an uruguayan developer, living in my home country, with a full time job doing web stuff. Just started working in a new project using angular 1.x, so I may be sharing stuff about it.

At night, and usually late at night, I make games, mostly in Unity. So I’ll be also sharing my two cents on the cool and ugly things about my game dev experience. Maybe I’ll create and share an asset for the store, display some of my scripting adventures or just show you how badly someone can model characters. I could also be sharing how my new games are looking like so I get your so valuable opinions!

Either way, I like reading and learning new stuff, so chances are that I get to write about programming with Swift or modeling in Blender.

Other than that, I’m an avid coffee drinker who loves travelling, meeting new people and discovering new places, so I think this is a great way to not only share my nerdy tips, but also disconnect from wherever I am by spilling some words over here.

Welcome! and I hope you stay tuned 🙂