Grey Zoned

Facebook SDK 7.3.0 in Unity 5.3 – Login, Username & Profile Picture Tutorial

Since the last set of Facebook tutorials that I created there has been some changes to the SDK. We’re now on 7.3.0 at the time I write this post.

Some of the more notable changes are the new IResult replacing FBResult, FB.LogInWithReadPermissions as the new way of initialising the first login.

In the video tutorial I mentioned about the OnGUI bug which is currently in Facebook SDK 7.3.0 which I know has plagued some of you while trying to make my previous tutorials work. To be exact that bug is:

ArgumentException: You can only call GUI functions from inside OnGUI.

Facebook have mentioned they will fix this in the next release of the SDK. So for now we have to use a work around so we can carry on working!

To fix this, open up FacebookSDK/SDK/Scripts/PlatformEditor/EditorFacebookMockDialog.cs and find:

public void OnGUI(){

Then insert the following code after that line – so that it’s the first code inside of OnGUI:


if ( this.modalStyle == null )
{
	this.modalRect = new Rect(10, 10, Screen.width - 20, Screen.height - 20);
	this.modalStyle = new GUIStyle(GUI.skin.window);
	Texture2D texture = new Texture2D(1, 1);
	texture.SetPixel(0, 0, new Color(0.2f, 0.2f, 0.2f, 1.0f));
	texture.Apply();
	this.modalStyle.normal.background = texture;
}

After doing this you will still get a small error, but it’s ignorable since it doesn’t actually break anything.

I hope the tutorial helps, let me know in the forums if there are any bugs!

Here’s the full code from FBscript.cs for your convenience:


using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;

public class FBscript : MonoBehaviour {

	public GameObject DialogLoggedIn;
	public GameObject DialogLoggedOut;
	public GameObject DialogUsername;
	public GameObject DialogProfilePic;

	void Awake()
	{
		FB.Init (SetInit, OnHideUnity);
	}

	void SetInit()
	{

		if (FB.IsLoggedIn) {
			Debug.Log ("FB is logged in");
		} else {
			Debug.Log ("FB is not logged in");
		}

		DealWithFBMenus (FB.IsLoggedIn);

	}

	void OnHideUnity(bool isGameShown)
	{

		if (!isGameShown) {
			Time.timeScale = 0;
		} else {
			Time.timeScale = 1;
		}

	}

	public void FBlogin()
	{

		List<string> permissions = new List<string> ();
		permissions.Add ("public_profile");

		FB.LogInWithReadPermissions (permissions, AuthCallBack);
	}

	void AuthCallBack(IResult result)
	{

		if (result.Error != null) {
			Debug.Log (result.Error);
		} else {
			if (FB.IsLoggedIn) {
				Debug.Log ("FB is logged in");
			} else {
				Debug.Log ("FB is not logged in");
			}

			DealWithFBMenus (FB.IsLoggedIn);
		}

	}

	void DealWithFBMenus(bool isLoggedIn)
	{

		if (isLoggedIn) {
			DialogLoggedIn.SetActive (true);
			DialogLoggedOut.SetActive (false);

			FB.API ("/me?fields=first_name", HttpMethod.GET, DisplayUsername);
			FB.API ("/me/picture?type=square&height=128&width=128", HttpMethod.GET, DisplayProfilePic);

		} else {
			DialogLoggedIn.SetActive (false);
			DialogLoggedOut.SetActive (true);
		}

	}

	void DisplayUsername(IResult result)
	{

		Text UserName = DialogUsername.GetComponent<Text> ();

		if (result.Error == null) {

			UserName.text = "Hi there, " + result.ResultDictionary ["first_name"];

		} else {
			Debug.Log (result.Error);
		}

	}

	void DisplayProfilePic(IGraphResult result)
	{

		if (result.Texture != null) {

			Image ProfilePic = DialogProfilePic.GetComponent<Image> ();

			ProfilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());

		}

	}

}

If this tutorial helped please share it! If you would like to be notified when I release new tutorials then subscribe to my email list (the subscribe box is in the sidebar). Lastly, if you would like to help me then you can become a Patron at patreon.com/greyzoned.

Until next time, Happy Coding!

Glenn

Glenn Mason

Tutorial by Glenn Mason

I've been interested in Game Development since I was about 11 years old when I started by making levels for Duke Nukem 3D, later moving on to creating Half-Life and Counter-Strike maps. Once I got into coding I went down the route of learning to make websites and web applications. Now I'm a professional Web Developer, running my own freelance business. However, my heart is still in Game Development. I started using Unity late in 2013. Since then I've released two Apps on the App stores that use Unity.

Tutorial Discussion

Home Forums Facebook SDK 7.3.0 in Unity 5.3 – Login, Username & Profile Picture Tutorial

This topic contains 45 replies, has 30 voices, and was last updated by  Fakhar Ahmad 1 week, 3 days ago.

Viewing 46 post (of 46 total)
  • Author
    Posts
  • #802

    Fakhar Ahmad
    Participant

    Hey there Glenn. Amazing tutorial! it is working fine in unity editor but not working in android. Kindly help me!!

Viewing 46 post (of 46 total)

You must be logged in to reply to this topic.