Adding Facebook support lets users share information and invite friends to join them creating awareness about your game. This is often done in combination with rewards as a means of encouraging the user to use Facebook. Game Framework contains helper functions and components that let you easily add Facebook functionality to your game and removing the complexity of initialisation and callbacks.
Facebook usage requires that you first install and setup the Facebook Unity Plugin, following the Facebook Getting Started guide to setup and add an App Id. You will then need to enable Game Framework integration with Facebook through the Menu | Windows| Game Framework | Integration Window.
You can then start using Facebook by adding the FacebookManager component to your scene (recommended onto a _GameScope gameobject). Within FacebookManager you can configure the permissions that your game requires along with other setup and usage information.
FacebookManager is a singleton and can be accessed through FacebookManager.Instance. Several methods and properties are available which are described in the API documentation. Some of the most important are also discussed further below.
With the Facebook SDK setup, connecting to and from Facebook is as simple as calling the the Login() and Logout() methods.
// Login to Facebook
// Logout from Facebook
GameFramework handles all the callback’s in the background for you and will try and login, get basic user data including the users profile picture and optionally the users friends who are using the app. There are several messages that are sent out to reflect the different login states however it is generally not necessary to worry about these.
GameFramework provides the FacebookConnection component to make logging in and out from your game very easy. This component takes 3 gameobjects that represent the not connected, connecting and connected states. It also provides Login and Logout methods so you can easily hook up buttons to perform this functionality. FacebookConnection will monitor the current status and show the correct gameobjects accordingly. You might typically have a login button hooked up to the Login method on your not connected gameobject, a connecting button or label connecting gameobject, and a logout button hooked up to the Logout method on your connected gameobject,
Showing Based On State
You will likely need to display different things based upon the current Facebook connection and permission state.
EnableIfUserDataLoaded can be used to show one of two gameobjects based upon whether the user is connected and logged into Facebook or not. You can use this to show information encouraging them to log into Facebook if they aren’t logged in, or Facebook related information or controls if they are logged in. You can also use the FacebookConnection component mentioned above (optionally hooking up Login / Logout) to achieve a similar effect, but with an additional ‘Connecting’ state.
In a similar fashion, EnableIfPermissionsGranted can be used to show one of two gameobjects based upon whether the user has granted certain permissions. As a user can deny certain permission you can use this prompt them for further permissions or show other information based upon the status of the permissions that you specify.
Finally, EnableIfFacebookSDK can be used to show different gameobjects depending upon whether the Facebook SDK is installed and activated through the integrations window as shown above. This can be used if you are targeting different builds, or reusing code across multiple games where only some instances support Facebook.
Letting users easily post about your game is a great way of creating awareness. In Game Framework, you can do this with a single line of code (or component) and all the complexities of logging in etc. will be automatically handled for you. If the user is not currently logged into Facebook then they will be automatically logged in.
ShareLink provides several optional parameters to let you specify the title, content, link and more however if not specified then default values from the localisation file and FacebookManager component will be used. See the API documentation for further information. If you want to reward a user for posting then you can listed for a FacebookShareLinkMessage andreward as appropriate.
You can also drag the OnButtonClickShareLink component onto any UI button to automatically hook up the buttons on click event with a ShareLink call.
Inviting Friends (AppRequest)
You can also easily let users invite friends to play your game. The number of friends invited is tracked so you can offer rewards to encourage them to do this. Like with ShareLink, you can do this with a single line of code (or component) and all the complexities of logging in etc. will be automatically handled for you. If the user is not currently logged into Facebook then they will be automatically logged in.
AppRequest provides several optional parameters to let you specify the title, content, link and more however if not specified then default values from the localisation file and FacebookManager component will be used. See the API documentation for further information.
You can also drag the OnButtonClickAppRequest component onto any UI button to automatically hook up the buttons on click event with an AppRequest call.
If the Facebook SDK is enabled then the gameover window will also give options for posting to Facebook.
The extras bundle contains a full sample demonstrating usage of most of the above features.
If you have any thoughts or suggestions on improvements or additions to the Facebook features then please let us know!