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:
Step 2: Setup Facebook SDK for Unity
- Next, download the official 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
- 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:
- Web – https://developers.facebook.com/docs/unity/getting-started/canvas
- iOS – https://developers.facebook.com/docs/unity/getting-started/ios
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:
Warning:[Temp\StagingArea\AndroidManifest–main.xml:25, D:\UnityWorkspace\FbTest\Temp\StagingArea\android–libraries\facebook–android–wrapper–release\AndroidManifest.xml:3] Main manifest has <uses–sdk android:targetSdkVersion=’22’> but library uses targetSdkVersion=’23’]
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:
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.
Pretty cool huh? 🙂
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!