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 4 months, 3 weeks ago.

Viewing 15 posts - 16 through 30 (of 37 total)
  • Author
    Posts
  • #714

    Arpit Agarwal
    Participant

    Hey I am facing a problem in building it for android, haven’t used Singletons but done all the other things. The error is :

    UnityException: Build failure
    Unable to merge android manifests. See the Console for more details. See the Console for details.
    UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuild (System.String title, System.String message)
    UnityEditor.Android.PostProcessor.CancelPostProcess.AbortBuildPointToConsole (System.String title, System.String message)
    UnityEditor.Android.PostProcessor.PostProcessRunner.RunAllTasks (UnityEditor.Android.PostProcessor.PostProcessorContext context)
    UnityEditor.Android.PostProcessAndroidPlayer.PostProcess (BuildTarget target, System.String stagingAreaData, System.String stagingArea, System.String playerPackage, System.String installPath, System.String companyName, System.String productName, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry)
    UnityEditor.Android.AndroidBuildPostprocessor.PostProcess (BuildPostProcessArgs args)
    UnityEditor.PostprocessBuildPlayer.Postprocess (BuildTarget target, System.String installPath, System.String companyName, System.String productName, Int32 width, Int32 height, System.String downloadWebplayerUrl, System.String manualDownloadWebplayerUrl, BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.BuildReporting.BuildReport report) (at C:/buildslave/unity/build/Editor/Mono/BuildPipeline/PostprocessBuildPlayer.cs:177)
    UnityEditor.HostView:OnGUI()

    Any help is highly appreciated !!

    Cheers!

    #716

    Sulaiman Affandi
    Participant

    i already implemented the social sharing app and it works on unity, but i already tried using an android emulator like Bluestack for example, if pressed login it wont redirect to an authorized info facebook. The login button on android apk just display a click login button. But on Unity, it can be redirected and saw profile name and profile pic invitation sharing and challenge

    #717

    Arpit Agarwal
    Participant

    I am not able to build the apk for my unity project. when I build the project it shoots up the above mentioned error, unable to merger android manifest files.
    I checked other places the error is produced when different manifest files(from different plugins) have different values for same variable, I am unable to make out which variable is creating this error for me. If you can figure it out ( seeing the above mentioned error ) would help a lot!!!

    #718

    Sulaiman Affandi
    Participant

    +Arpit Agarwal try deleting some Android Manifest code. Maybe you have more than one assets that contains an Android Manifest

    #723

    armin javidfar
    Participant

    when I press login button on a real android device, the dialog screen tries to load, but it doesn’t! why is that??

    #729

    armin javidfar
    Participant

    My problem was related to facebook filtering in Iran!

    #731

    Douglas Martins
    Participant

    Hi everyone , I’m a little problem when I try to test the application in my android . When I click the button to log on facebook, happens appears a white screen message ” Invalid Scope : Public” I am trying to find the solution but so far nothing . Does anyone know how to solve this problem ? I’m sorry for bad English.
    Screenshot of my cellphone

    Thanks 🙂

    #735

    MaxOnar
    Participant

    @Douglas Martins:
    I guess it goes wrong in FBlogin(), since this is where you specify the scopes.
    It should be:
    permissions.Add("public_profile");
    instead of just public.

    ——————–

    I also have a problem, hope someone can help 🙂

    When I close the app and restart it, I have to press Login button again. Then it says I already gave permission.
    Seems that when calling DealWithFBMenus(FB.IsLoggedIn) in Awake(), FB.IsLoggedIn is false.
    It only gets true a few moments later.

    I’m currently keep checking if it turns true, and then auto login. But I’m sure this is the wrong way 🙂
    Any tips?

    • This reply was modified 1 year, 3 months ago by  MaxOnar.
    #737

    Eric Asinas
    Participant

    Many thanks for this great tutorial!
    It worked perfectly for Android devices but when I tried to build for iOS, it failed to compile. I was only able to export it to XCode but when I tried to compile a lot of errors came out and the build failed. I thought it was the code so I tried building the basic examples that come with the FB SDK instead, but its the same case, build failed. I am using Unity 5.3 and FB SDK 7.4, I tried integrating FB SDK 7.5 but same scenario happens. The weird thing is, if I downgrade to FB SDK 7.3, it compiles perfectly on Xcode. Any ideas or similar issues? Have you tried building this example for iOS devices?

    #738

    Mad App
    Participant

    Hi and thank you. Share and everything works great . Thank you lots. But was wondering how I could share screenshot? Seems there every tutorial is different. And I barely know programming. Could you by any chance provide us with the code needed to share screenshot of the game? Thank you so much.

    #741

    li ju
    Participant

    Hello!
    I have a issue about FB.AppRequest();
    AppRequest(
    string message,
    OGActionType actionType,
    string objectId,
    IEnumerable<object> filters = null,
    IEnumerable<string> excludeIds = null,
    int? maxRecipients = null,
    string data = “”,
    string title = “”,
    FacebookDelegate<IAppRequestResult> callback = null)
    Now, I want to send scores to my facebook friends by AppRequest, but i can’t get the objectId , i don’t know it is what,can you help me?

    Thanks for your help.

    #752

    Michael Giles
    Participant

    Hi. Where you mention NOT using the GetAppLink and instead just typing your app URL instead…… Do you still use the same code with th Getapplink part and just add the url string or is it diffrenent code? if possible can you put the coding here for me to copy in or have you shown it on your website? many thanks

    #753

    Michael Giles
    Participant

    At 28.36 on your video I enter the facebook token but I end up with this error

    NullReference: Object Reference not set to an instance of an object FBscript.AuthCallBack (IResult result) (at Assets/FBscript.cs:41

    when I click on the error it takes me Fbscript.cs and shows Line:

    FacebookManager.Instance.IsLoggedIn = true;

    please help everything was working up to that point

    #759

    arhan arhan
    Participant

    Hey Glenn, im trying to get my game to work on Facebook, im using SDK 7.5, in the editor, i manually insert my user access token, click on “Send Success” and it works just fine. Now when i host it on a domain and tell Facebook to use the https that i have uploaded my game on, this is what i get http://imgur.com/hZYuy01
    Im not sure what the problem is, ive exhausted myself attempting to get it to work, heres my script. Any help would be greatly appreciated 😉

    using UnityEngine;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using Facebook.Unity;

    public class FBScript : MonoBehaviour {
    private string canYouBeatScore;

    public string ProfileName { get; set; }

    public bool IsLoggedIn { get; set; }

    public Sprite ProfilePic { get; set; }

    public ScoreManager theScoreManager;

    public GameObject startButton;
    public GameObject settingsButton;
    public GameObject aboutButton;
    public GameObject fbLoginButton;

    void Awake ()

    {
    if (!FB.IsInitialized) {
    FB.Init (SetInit, OnHideUnity);
    } else {
    IsLoggedIn = FB.IsLoggedIn;
    }
    }

    void Start ()
    {
    canYouBeatScore = “Can you beat my score? I got ” + theScoreManager.hiScoreCount;

    }

    void SetInit ()

    {

    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) {
    startButton.SetActive (true);
    settingsButton.SetActive (true);
    aboutButton.SetActive (true);
    fbLoginButton.SetActive (false);
    } else {
    startButton.SetActive (false);
    settingsButton.SetActive (false);
    aboutButton.SetActive (false);
    fbLoginButton.SetActive (true);
    }

    }

    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);

    }

    public void Share()
    {
    FB.FeedShare (
    string.Empty,
    new Uri(“https://somethingsomethign”),
    “stuff”,
    “stuff”,
    canYouBeatScore,
    new Uri(“http://picture”),
    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”);
    }
    }

    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);

    }

    }
    }

    #760

    Josh King
    Participant

    Hi Glenn! Your tutorials look excellent and are well received. I’m going to go through them regardless, but first I wanted to know how useful this integration will be for purposes of an asynchronous turn based multiplayer game. For instance, will this work alongside a back end service like GameSparks or App42? Also will it be useful for cross platform play, such as one player on a Windows standalone build, and another playing on Android, or Android vs IOS, etc.? I’m only just starting to research these types of things, but I am very well versed with Unity singleplayer scripting, and basic Unity networking. Any help pointing me in the right direction would be greatly appreciated. And I’ll be sure to follow up after I go through your tutorials as well. Thanks!

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

You must be logged in to reply to this topic.