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 43 replies, has 28 voices, and was last updated by  Alvin Carter 2 months, 3 weeks ago.

Viewing 15 posts - 1 through 15 (of 44 total)
  • Author
    Posts
  • #585
    Glenn Mason
    Glenn Mason
    Keymaster

    This is the forum topic about the first Facebook SDK 7.3.0 in Unity 5.3 tutorial in the series.

    (http://greyzoned.com/tutorials/facebook-sdk-7-3-0-in-unity-5-3-login-username-profile-picture-tutorial/)

    Please post any questions in this thread.

    Glenn

    #612

    david dumitru
    Participant

    So everything work fine on Unity but when i am on android device and i press Facebook log in button nothing happen. I need some help. Thanks

    #613
    Glenn Mason
    Glenn Mason
    Keymaster

    Hi David welcome to the forum.

    Do you get any bugs on compiling? Android manifest is all fine too?

    #616

    Patrick CJ Medina
    Participant

    Thanks for this tutorial! It works very well. Not sure if it will work on an actual Android device though since I haven’t tried it yet.
    Anyway, my question is how do I add the E-mail and Password fields onto this one? I mean the tutorial lets me, only me, to log-in. What if I want to log my friend’s account in or something? I’m a beginner in coding C# and in using Unity so yeah, I understand the basics but things like this makes my head explode without the help of some expert like you, Mr. Glenn! πŸ™‚

    #619
    Glenn Mason
    Glenn Mason
    Keymaster

    Hi Patrick,

    As far as I’m aware you can’t actually have the username and login in your app. That is unless Facebook is logged out of the device that you’re on and then you try logging in then. The prompt that will come up should allow username and password to be entered before showing the permissions request.

    On a side note, it isn’t possible to actually log Facebook out from your app, if you was thinking of going down that route.

    I would suggest then if you wanted to log in with another account, you would first open the Facebook app on the device and then log out with that. Also log out of any Facebook logged in inside any web browsers. Then try the Facebook login from your app that you’ve made with this tutorial.

    Hope that helps!

    #620

    Patrick CJ Medina
    Participant

    Ah yes! So you mean once the user or the owner of the device has his Facebook logged in on the Facebook App itself, that is the account that will be logged in in my App? Now I get it. It’s cool then! πŸ™‚
    It’s just a little bit of confusing for me cause believe it or not, I don’t use smartphones even though I’m developing an Android app LOL!
    Thanks Glenn! πŸ™‚

    #622

    Patrick CJ Medina
    Participant

    Oh, and another question, (sorry for double posting) when I log my Facebook in on the Main Menu, and then I go to a new scene by clicking a button, and then I go back to the main menu again, Facebook is now logged out.

    In short, how do I make it so that Facebook is logged in on the whole game or application? Thank you for your response! πŸ™‚

    #623
    Glenn Mason
    Glenn Mason
    Keymaster

    I noticed that a few days after I recorded that tutorial! I plan on addressing that in the next tutorial.

    I’ll go into more detail with that tutorial, but its along the lines of making FB global or changing it’s scope to be accessed everywhere, which would make it work in all scenes. I need to look at how I did it in an old game that had facebook integration and then I’ll cover it in the next tutorial. Sorry I can’t be more help at this time!

    #624

    Patrick CJ Medina
    Participant

    Oh, that’s fine! πŸ™‚ Thank you for sharing your time by making these tutorials. They’re really helpful. πŸ™‚

    #629

    Leonardo Silva
    Participant

    I tried install openssl, but not happens.
    http://prntscr.com/9xq5ly
    Someone know how to fix??

    #634

    Patrick CJ Medina
    Participant

    Facebook SDK 7.4.0 is out now. The OnGui problem is now fixed. πŸ™‚

    #646

    Liang Wei Hao
    Participant

    Hi Mr Glenn.

    Thank you for making this tutorial. It really benefits me for the project I doing on tomorrow.
    I will like to ask a few questions, sorry for any inconvenient.
    Can I will know will this works on window phone game as well? I will also like to implement “Share” function, will it works if I refer to a portion of codes in your older series?

    I am thinking to implement the “public void ShareWithFriend()” function.

    Thanks
    Allen

    #650
    Glenn Mason
    Glenn Mason
    Keymaster

    I’m not sure about windows phones sorry, I’ve never used them nor do I have any intention of going near them. I’ll just be focusing on Android and iOS in my tutorials.

    As for sharing and inviting, new tutorial is uploading right now πŸ™‚

    #679

    Steven Alarcon
    Participant

    Hi there Glenn ! Can you help me ? I followed your codes and I got an error says : ArgumentException: You can only call GUI functions form inside onGUI.

    I hope you will reply ASAP. Because its our system in our thesis . Thanks !

    #680
    Glenn Mason
    Glenn Mason
    Keymaster

    Hi Steven, please rewatch the tutorial specifically at 10 minutes and 22 seconds in. I talk about this issue in the tutorial and show you how to fix it. Alternatively, download and install version 7.4.0 of Facebook SDK.

Viewing 15 posts - 1 through 15 (of 44 total)

You must be logged in to reply to this topic.