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 15 posts - 16 through 30 (of 46 total)
  • Author
    Posts
  • #681

    Steven Alarcon
    Participant

    Thanks a lot Glenn ! I’m gonna share your work. Thanks !

    #684

    Shawn Lam
    Participant

    hi Glenn,

    i found that when using IOS, the profile picture cannot be loaded, it seems the FB.api never return the callback
    i am using FB SDK 7.4 and Unity 5.3 . Do you have such issue too ?

    thanks

    #690

    cameron wayne
    Participant

    Hey i need your help please can you help me for this please, i’m working before 4 days but have not any thing to do for facebook SDK..
    i have downloded SDK and now i’m at that point where facebook show in debug.log (“facebook is logged in”)
    But after that when i did see next tutorial 2nd, i dont have “”util”” script and no idea how to get “util.cs” script.

    In facebook SDK there is a script which is named by “GraphUtil.cs” having function

    public static string GetPictureQuery(string facebookID, int? width = null, int? height = null, string type = null, bool onlyURL = false)
    {
    string query = string.Format(“/{0}/picture”, facebookID);
    string param = width != null ? “&width=” + width.ToString() : “”;
    param += height != null ? “&height=” + height.ToString() : “”;
    param += type != null ? “&type=” + type : “”;
    if (onlyURL) param += “&redirect=false”;
    if (param != “”) query += (“?g” + param);
    return query;
    }

    but it dont have “GetPictureUrl” that you did use in your tutorial so haw can we add this in my
    “FB.API(GraphUtil.GetPictureQuery(“me”, 128, 128)” so how to handle this issue please help me…. for this…

    can you send me that scripts that you did uses in your tutorail so that i can do work with that tutorails refrence. i will wait for your reply.

    • This reply was modified 1 year, 5 months ago by  cameron wayne.
    #695

    Lewis Pettinger
    Participant

    Hi,

    Is there a way to do this for other users of facebook because obviously they won’t have the access token when they try and log in??

    Excellent tutorial though! Really useful!

    Thanks

    #696

    ricky bosman
    Participant

    Hi,

    I followed your tutorial, but after 25:00 I’m stuck with this error:

    400 Bad Request
    UnityEngine.Debug:Log(Object)
    FBscript:DisplayUsername(IResult) (at Assets/FBscript.cs:87)
    Facebook.Unity.<Start>c__Iterator1:MoveNext() (at Assets/FacebookSDK/SDK/Scripts/Utils/AsyncRequestString.cs:133)

    I can login but it’s not showing my Facebook name.

    Thanks.

    #697

    Lewis Pettinger
    Participant

    Is this possible to run on standalone builds as when it is built and run on my mac, the login button does nothing?

    Thanks in advance!

    #699

    ng jen
    Participant

    Hi Glenn. ty for the awesome tutorial! i want to ask how to allow everyone click the button ‘login using fb’ then their fb link, fb name, score, etc will be stored in PARSE?? how to connect this with parse??
    And is it compulsory to create game on fb in order for u to use ur fb account to play that game? As i dont want other people to key in the access token everytime they access the game? Ty and i hope to hear from you soon!

    #703

    Steven Alarcon
    Participant

    Hi glenn !

    When I run the program , there’s an error says: ArgumentException: Could not create sprite (0,0,128,128) from a 8×8 texture.

    Please reply thanks !

    #705

    amirkazim
    Participant

    there is an issue while sending score. Its not working in Facebook plugin 7.4 can some one guide me its not giving publish permission and secondly how to track an app install, Like user send a app invite to a 2nd user to power up his lives now how can I know that the other user have downloaded the app and I should reward him. kindly explain me with example thanks

    #707

    xiaoyu liu
    Participant

    Mr Grey, thank you so much for the video !
    Over here we are accessing the log in using access token, but what shall l do if we log in just using Facebook user name and password like in normal apps ? Thanks !

    #710

    Sulaiman Affandi
    Participant

    Hi thanks for the tutorial this is a great way to study and create some social properties on games or other apps components. I want to ask that my profile pic haven’t come up. I have a profile pic, but it won’t generate.

    Im using Unity 7.4 btw, I don’t know if its different or not. But the image won’t generate. Thanks for the tutorial and guidance

    #712

    Arpit Agarwal
    Participant

    hey, when I am trying to print my name an error is being generated, the program is reaching the UserName.text line (checked from debuging), the error is :

    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)
    FBScript.DisplayUsername (IResult result) (at Assets/Scripts/FBScript.cs:74)
    Facebook.Unity.AsyncRequestString+<Start>c__Iterator1.MoveNext () (at Assets/FacebookSDK/SDK/Scripts/Utils/AsyncRequestString.cs:133)

    Help would be highly appreciated !

    • This reply was modified 1 year, 4 months ago by  Arpit Agarwal.
    #725

    armin javidfar
    Participant

    Hi Glenn and tnx for your tutorials.. I have a problem, On an android device, when I press login butoon the dialog tries to load but it doesn’t. This problem also occured in sdk 7.4! i did everything you did in both tutorials. I hope you could help me figure this out.. tnx

    #726

    Sulaiman Affandi
    Participant

    I have the same problems as you guys are at, when i tested on an Android emulator, i still cant find out whether my code is wrong or my settings are wrong. I already tried at a real android device as well, but still cant work. My co workers said that try using a less dependable Unity tools. sometimes use a script to get data and set them as well. But i need a real fact about this matter thank you once again. I want to keep in touch. If someone knows this problem, please help us. Thank you

    #728

    armin javidfar
    Participant

    This problem is related to filtering facebook in Iran!!!

Viewing 15 posts - 16 through 30 (of 46 total)

You must be logged in to reply to this topic.