Wednesday, April 9, 2014

How to invite friends using Facebook SDK for Unity3D

Yıu can call this code for inviting your facebook friends using Unity3D facebook SDK.


if(FB.IsLoggedIn)
{
    FB.AppRequest(
        message: "Come and Play This Game!",
        title: "My Game Invitation For Facebook Friend",
        callback: InviteCallback
    );
}

// ...

void InviteCallback(FBResult response)
{
Debug.Log("facebook invitation from Unity3D sent");
}



if(FB.IsLoggedIn)
{
    FB.AppRequest(
        message: "Come and Play This Game!",
        title: "My Game Invitation For Facebook Friend",
        callback: InviteCallback
    );
}

// ...

void InviteCallback(FBResult response)
{
Debug.Log("facebook invitation from Unity3D sent");
}



If your app is in sandbox mode, people won't get notified when the request is sent. So change your facebook application settings to "public"  in https://developers.facebook.com/apps page, goto "Status & Review" tab on le menu on the left. On the right you will see "Do you want to make this app and all its live features available to the general public?" question. Set its value to "YES" . If you cant modify YES/NO section then you have to give your application a contact email  on "Settings" tab on left menu.



Look for the request in https://www.facebook.com/appcenter/requests as the recipient and see if the request shows up there.



How to check facebok invitations: 

If you want you can also check invitation from FBResult response object, maybe even call how many people does this invitation is sent. An example code:

private void InviteCallback(FBResult result)
    {
        if (result != null)
        {
            var responseObject = Json.Deserialize(result.Text) as Dictionary<string, object>;
            IEnumerable<object> objectArray = (IEnumerable<object>)responseObject["to"];

            if (objectArray.Count() >= 3)
            {
                // 3 facebook invitations send. Give user a free item or bonus etc.
            }
        }
    }


Btw, facebook also gives us nice shiny login buttons, which can be downloaded from this address: https://developers.facebook.com/docs/facebook-login/checklist .






No comments:

Post a Comment