Grey Zoned

Facebook SDK 7.4.0 in Unity 5.3 Tutorial – Singletons, Inviting & Sharing

Lets learn how to invite and share our game.

Watch the full video for an in-depth explanation of how to put this all together. However if you just want a quick cheat sheet of how to do shares and invites then look no further than the following code:

Sharing

 


public void Share()
	{
		FB.FeedShare (
			string.Empty,
			new Uri("http://linktoga.me"),
			"Hello this is the title",
			"This is the caption",
			"Check out this game",
			new Uri("https://i.ytimg.com/vi/NtgtMQwr3Ko/maxresdefault.jpg"),
			string.Empty,
			ShareCallback
		);
	}

	void ShareCallback(IResult result)
	{
		if (result.Cancelled) {
			Debug.Log ("Share Cancelled");
		} else if (!string.IsNullOrEmpty (result.Error)) {
			Debug.Log ("Error on share!");
		} else if (!string.IsNullOrEmpty (result.RawResult)) {
			Debug.Log ("Success on share");
		}
	}

Inviting


public void Invite()
	{
		FB.Mobile.AppInvite (
			new Uri("http://linktoga.me"),
			new Uri("https://i.ytimg.com/vi/NtgtMQwr3Ko/maxresdefault.jpg"),
			InviteCallback
		);
	}

	void InviteCallback(IResult result)
	{
		if (result.Cancelled) {
			Debug.Log ("Invite Cancelled");
		} else if (!string.IsNullOrEmpty (result.Error)) {
			Debug.Log ("Error on invite!");
		} else if (!string.IsNullOrEmpty (result.RawResult)) {
			Debug.Log ("Success on Invite");
		}
	}

Check the resources sidebar where you can download the full source code of this tutorial.

If you have any issues, please ask your questions in the support thread from the forum below this article or go to the full thread on the forums here

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.4.0 in Unity 5.3 Tutorial – Singletons, Inviting & Sharing

Tagged: 

This topic contains 36 replies, has 28 voices, and was last updated by  Jeniffer P 3 weeks, 3 days ago.

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

    The latest tutorial covers Sharing, inviting and the singleton pattern. I’m anticipating some of you may have questions regarding the GetAppLink part. Please ask any question about this tutorial in this thread.

    Glenn

    #660

    Raducu Mihai
    Participant

    Hi there I have a problem when trying to share on facebook from my Android device. It loads the facebook dialog that allows me to share, and then it goes back to my unity app. It dosen’t allow me to click post or cancel, it just turns off the facebook app and goes back to my unity app. Any idea why is that happening?

    #661
    Glenn Mason
    Glenn Mason
    Keymaster

    That’s very weird. What permissions do you call on login? Are you running the most recent Unity, Facebook SDK, and your JDK and JRE environments are up-to-date?

    #666

    John Wheeler
    Participant

    Hi,
    Nice tutorial, looking forward to seeing the update for scores.
    Can you update the download files to reflect the code used in the video please.

    John

    #667
    Glenn Mason
    Glenn Mason
    Keymaster

    Hi John, welcome to the forums.

    The files on the site should be as it was in the tutorial. I zipped them up right after I finished recording. Which part in particular isn’t the same? Is it to do with the GetAppLink part? I may have somehow managed to screw something up! haha

    #668

    John Wheeler
    Participant

    Hi Glenn,

    Looks as if it was packaged at the point when it only had the two buttons Login and profile Pic, there are no share, invite buttons or code.

    https://www.dropbox.com/s/bqym8t6xetmixpe/Screenshot%202016-02-12%2022.23.49.png?dl=0

    John

    #669

    Patrick CJ Medina
    Participant

    Hey Glenn!
    Thanks for uploading the tutorial! šŸ™‚

    My question:

    What do I need to modify if let’s say my Share, Invite, and Challenge buttons are on a different scene and not on the same scene as the Login?
    #SorryImaNoob

    #673
    Glenn Mason
    Glenn Mason
    Keymaster

    @john Wheeler:

    Interesting, not sure what happened but it does appear from your screenshot that that would be the download for the first tutorial. Sorry about that, thanks for mentioning it though. I’ve repackaged it and reuploaded the correct one this time. It’s updated on the tutorial page and I’ll also include it here too for your convenience:

    Facebook-API-tutorial-pt2.zip

    @Patrick CJ Medina:

    Hi Patrick, your welcome for the tutorial!

    Well the beauty of using the singleton method now is that the share, invite and challenge functionality can be called from any scene in your app.

    If you’re on a different scene from the one where your user logs in, then you would need to give that scene an empty game object and attach a new script to it. In that script create a public function that calls the share function from the singleton. for example:

    public void Share()
    {
    FacebookManager.Instance.Share();
    }

    Then in your new scene select your button then attach an onclick event. Drag in the empty gameobject which contains the script with the function which I described above. Then in the dropdown for the button function to trigger on click, select the script and then the share() function. Now when you click on that button it will call the share function from your FacebookManager singleton. Of course you would probably want to show or hide that button based on if the user is logged in or not. So you would create a similar DealWithFBMenus function in your scene script which checks if FB.IsLoggedIn == true, then show the share button. Call DealWithFBMenus on Awake in that scene.

    #674

    John Wheeler
    Participant

    Hi Glen,

    Thanks for the updated files.

    Check out the new Friends Smash Unity Example, it has been updated SDK 7 and does show the users profile picture running on IOS, although the leaderboard pictures do not show.

    It also uses a singleton as the main file.

    John

    • This reply was modified 1 year, 2 months ago by  John Wheeler.
    • This reply was modified 1 year, 2 months ago by  John Wheeler.
    #677

    Animesh Mathur
    Participant

    Hi, I am facing a weird bug, everything works fine during Debug but after build I think either GameObject is not loading or script is not running. I am sure its either of these as I have checked through creating a button using GUI and the button is there during Debug but after Build that GUI button is not visible.

    Any idea what might have gone wrong?

    #682

    Mahad Saleem
    Participant

    Hey!
    Great work on the tutorial Glen! I am stuck on a problem and cant find any solution to it. What i am trying to do is i want to send a gift like a “fuel tank” in a racing game to a friend on facebook through my android app. I am able to send just simple game requests uptil now but unable to send gifts. Moreover, when a gift is sent, i need to check if the other user accepts my gift or not. If he accepts it, i should be able to trace it.
    Can you please help me out?
    Regards.

    #689

    Fabrizio Lo Grasso
    Participant

    hi,I did not understand what I have to insert instead of FB.GetAppLink (DealWithAppLink);

    you can enter the right code?

    #692

    Elie Maalouly
    Participant

    Hello Glen,

    Really like your videos, they are really helpful!
    I created a fully working scores API with the Facebook SDK 7.4.0 with the help of the above code and the other tutorials found on greyzoned.
    Though on Android when I login to Facebook or try to upload a new score, the game stops and the user can’t do anything until the action is done, and this icon shows up (working/loading icon)

    http://puu.sh/nvPVp/35e2c345be.png

    The question is: Is there a way to login and upload the scores to Facebook in the background? without interrupting the user.

    Elie

    #700

    Michal Krlin
    Participant

    Great tutorial, thanks! But i have a question. Is possible to figure out how many friends player invited?
    For example, with AppRequest() you can figure out numer of sended requests by some code like this, but sadly you can only request already playing members, what is unuseful for me…

     public void Request()
     {
      FB.AppRequest
      (
       "text!",
       null,
       null,
       null, 
       null, 
       null, 
       null,
       delegate (IAppRequestResult result)
       {
    
        if (result.Error == null)
        {
         object value;
    
         if (result.ResultDictionary.TryGetValue("to", out value))
         {
          string omg = value.ToString();       
    
          List<string> wtf = omg.Split(',').ToList();
    
          Debug.Log("Number of sended requests: " + wtf.Count());
    
         }
        }
       }
      );
     }
    

    Is possible to do something like that with FB.Mobile.AppInvite?
    And sorry for my english.

    #706

    amirkazim
    Participant

    kindly can any body tell me how to know that a user have installed the app on our app invite? I want to reward the 100 points on each install through app invite.

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

You must be logged in to reply to this topic.