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

Tagged: 

This topic contains 49 replies, has 33 voices, and was last updated by  Mark Supertramp 1 month, 1 week ago.

Viewing 5 posts - 46 through 50 (of 50 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!!

    #804

    Ха вот вы жжоте!

    #805

    Andreea Voina
    Participant

    Hi Glen! I`m stuck with an error that says:

    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.GUI.CallWindowDelegate (UnityEngine.WindowFunction func, Int32 id, Int32 instanceID, UnityEngine.GUISkin _skin, Int32 forceRect, Single width, Single height, UnityEngine.GUIStyle style) (at C:/buildslave/unity/build/Runtime/IMGUI/Managed/GUI.cs:1836)

    Also, the login works but it’s not showing my Facebook name.
    Can you please help me with this? I am using Facebook SDK 7.10.0. Thanks!

    • This reply was modified 3 months, 1 week ago by  Andreea Voina.
    #807

    Andreea Voina
    Participant

    Hi! Did you find a solution for this problem? I have the same issue but without any error.

    #809

    Mark Supertramp
    Participant

    Hey Glenn!
    Did you ever work with “FB.LogInWithPublishPermissions”?
    I am working on an app for school, where I want to create a Jpg and post it in the User’s Album. I think I have to ask for publish_actions permission but apparently is more to it than just a few changes to the code you have provided.
    If you managed to play with it, maybe you can give me some advice? I don’t seem to find a proper implementation.

    Thanks!
    Mark

Viewing 5 posts - 46 through 50 (of 50 total)

You must be logged in to reply to this topic.