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 2 months, 1 week ago.

Viewing 15 posts - 31 through 45 (of 50 total)
  • Author
    Posts
  • #734

    Kostadin Tachev
    Participant

    for SDK 7.5.0 there is some fix for line 217 in FacebookBase you can include this:

    if ( resultContainer.ResultDictionary == null )
    {
    resultContainer.ResultDictionary = new Dictionary< string, object >( 1 );
    }
    resultContainer.ResultDictionary[Constants.CallbackIdKey]
    = this.CallbackManager.AddFacebookDelegate(loginCallback);
    this.OnLoginComplete(resultContainer);

    resultContainer.ResultDictionary[Constants.CallbackIdKey]
    = this.CallbackManager.AddFacebookDelegate(loginCallback);
    this.OnLoginComplete(resultContainer);

    #743

    Jeeva 3M
    Participant

    Hi,

    Thanks for the tutorial..
    I get the following error while importing Facebook SDK into Unity3d.
    Assets/FacebookSDK/Examples/Scripts/ConsoleBase.cs(27,23): error CS0234: The type or namespace name SceneManagement' does not exist in the namespaceUnityEngine’. Are you missing an assembly reference?

    I use Unity 5.2.3… is it the problem??? or anything else?? Should I use Unity 5.3??

    Thanks
    Jeeva

    #744

    Hi im watching your updated videos, but there isnt one for the leaderboard/scores video

    So i have been attempting to just work round the old code, i hit a wall wehn it came to the Scores CallBack bit

    scoresList = Util.DeserializeScores (result.Text);

    This line has changed since then, but cant figure out what the update is, and ive searched alot. i know result.Text is now, result.RawResult as it works, but for the Util im stuck..

    Any chance you know the update please?

    Thanks- D

    #757

    Hugo Mendes
    Participant

    Hi Glenn! Thanks for the tutorial!

    I´m with a strange problem. I´ve made everything just like you did in the vídeo, and copied your code. But i receive this error : The associated script can not be loaded. Please fix any compile erros and assign a valid script.

    i don´t what to do anymore.

    Thanks.

    #762

    Burak Güney
    Participant

    Hi bro, thanks for the video its very helpful, but I got an error, after finishing code about getting the first name I got this error:
    KeyNotFoundException: The given key was not present in the dictionary.
    System.Collections.Generic.Dictionary`2[System.String,System.Object].get_Item (System.String key) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/Dictionary.cs:150)
    Can you help me to fix it because I tried to fix this, I could not find any clue about this error?

    #766

    Hassan Shaitou
    Participant

    Hello Glenn Mason thanks for your nice tutorials , really big help you make for people…
    I have a question regarding Facebook Unity SDK Customizing I mean:
    1: I am using SDK V7.8.0(Latest one) with Unity 5.0.2
    2: I integrated sdk correctly and everything worked ,but I want tow things:
    2-1: In other games there is a login and password dialogue to fill your Facebook account in it, how to get this window.
    2-2: How to change the form of these dialogue…

    #767

    Tayyab Saleem
    Participant

    Hey! thanks for such tutorials…
    but i got an error that could not create sprite (0,0,128,128) from 8*8 texture… how can i fix it? as i am new to game development so didn’t know very much…

    #772

    Han Wu
    Participant

    I have a little question. When I click on btn_login, and go to on click(). I cannot find FBlogin() method in FBscript. Can u help me solve this problem??

    #775

    organic
    Participant

    Hi Glen,
    thank you for the great tutorial. Unfortunately, I’ve just got an issue with FB SDK and it’s probably related with authorisation.
    Could you give me some tips how to solve it?
    Appreciate 🙂

    Graph API error: 400 Bad Request
    UnityEngine.Debug:Log(Object)
    FacebookManager:AuthCallBack(IResult) (at Assets/Scripts/FacebookManager.cs:51)
    Facebook.Unity.CallbackManager:TryCallCallback(Object, IResult) (at Assets/Facebook/Scripts/CallbackManager.cs:86)
    Facebook.Unity.CallbackManager:CallCallback(Object, IResult) (at Assets/Facebook/Scripts/CallbackManager.cs:66)
    Facebook.Unity.CallbackManager:OnFacebookResponse(IInternalResult) (at Assets/Facebook/Scripts/CallbackManager.cs:53)
    Facebook.Unity.FacebookBase:OnAuthResponse(LoginResult) (at Assets/Facebook/Scripts/FacebookBase.cs:266)
    Facebook.Unity.Editor.EditorFacebook:OnLoginComplete(String) (at Assets/Facebook/Scripts/PlatformEditor/EditorFacebook.cs:268)
    Facebook.Unity.FacebookGameObject:OnLoginComplete(String) (at Assets/Facebook/Scripts/FacebookGameObject.cs:57)
    Facebook.Unity.Editor.EditorFacebookMockDialog:SendErrorResult(String) (at Assets/Facebook/Scripts/PlatformEditor/EditorFacebookMockDialog.cs:79)
    Facebook.Unity.Editor.Dialogs.MockLoginDialog:<SendSuccessResult>m__0(IGraphResult) (at Assets/Facebook/Scripts/PlatformEditor/MockDialogs/MockLoginDialog.cs:71)
    Facebook.Unity.<Start>c__Iterator0:MoveNext() (at Assets/Facebook/Scripts/Utils/AsyncRequestString.cs:132)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    • This reply was modified 1 year, 3 months ago by  organic.
    #777

    organic
    Participant

    Hi Glen,
    thank you for the great tutorial. Unfortunately, I’ve just got an issue with FB SDK and it’s probably related with authorisation.
    Could you give me some tips how to solve it?
    Appreciate 🙂

    Graph API error: 400 Bad Request
    UnityEngine.Debug:Log(Object)
    FacebookManager:AuthCallBack(IResult) (at Assets/Scripts/FacebookManager.cs:51)
    Facebook.Unity.CallbackManager:TryCallCallback(Object, IResult) (at Assets/Facebook/Scripts/CallbackManager.cs:86)
    Facebook.Unity.CallbackManager:CallCallback(Object, IResult) (at Assets/Facebook/Scripts/CallbackManager.cs:66)
    Facebook.Unity.CallbackManager:OnFacebookResponse(IInternalResult) (at Assets/Facebook/Scripts/CallbackManager.cs:53)
    Facebook.Unity.FacebookBase:OnAuthResponse(LoginResult) (at Assets/Facebook/Scripts/FacebookBase.cs:266)
    Facebook.Unity.Editor.EditorFacebook:OnLoginComplete(String) (at Assets/Facebook/Scripts/PlatformEditor/EditorFacebook.cs:268)
    Facebook.Unity.FacebookGameObject:OnLoginComplete(String) (at Assets/Facebook/Scripts/FacebookGameObject.cs:57)
    Facebook.Unity.Editor.EditorFacebookMockDialog:SendErrorResult(String) (at Assets/Facebook/Scripts/PlatformEditor/EditorFacebookMockDialog.cs:79)
    Facebook.Unity.Editor.Dialogs.MockLoginDialog:<SendSuccessResult>m__0(IGraphResult) (at Assets/Facebook/Scripts/PlatformEditor/MockDialogs/MockLoginDialog.cs:71)
    Facebook.Unity.<Start>c__Iterator0:MoveNext() (at Assets/Facebook/Scripts/Utils/AsyncRequestString.cs:132)
    UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)

    • This reply was modified 1 year, 3 months ago by  organic.
    #782

    helder ventura
    Participant

    Hi hello
    Tanks for The Tutorial
    i get one error on the script EditorFacebookMockDialog
    the error is a local variable `texture’ cannot be declared in this scope because it will give a different mean to texture
    How Can I Fix it i am newb on scripting

    #783

    Leandro Saley
    Participant

    Hello Glenn!

    Can you tutor me to Use the Unity Facebook App for me to Put the MT4 trading Platform on my facebook account to serve as a Virtual Machine that can do the Auto trading 24 hours 5 days a week? This I think great use of your tutor rather than only for games.

    Thanks a lot and I am waiting for your reply to my e-mail account sadoli25@gmail.com.

    One of your funs, Leandro.

    #789

    Alvin Carter
    Participant

    Hey there, I have problem in this, Everything is ok BUT, While building it is giving error “Failed to re-build Package” something like that. I have tried to change build version from sdk manager, still same error. And then try to remove that script and Facebook SDK folder from project then, it build normally and everything well then. I think problem is from Facebook SDK or something android manifest??? Please help 🙂

    #790

    Alvin Carter
    Participant

    That Problem Solved… But new Problem Occurred After log in to android it is saying error ” Ivalid Scope: public_profile”… PLEASE

    #801

    Paul Kumar
    Participant

    Hi Glenn,

    Is there anyway I can store the access token as a token string? I am trying to integrate the fb auth provided bu fb and using it with the firebase authentication pluggin. I am confused on how Ican pass the fb token into firebase and it would really help if you could inform me in any way of storing this token

    Thanks a bunch, Paul

Viewing 15 posts - 31 through 45 (of 50 total)

You must be logged in to reply to this topic.