FB Sharing part-2
Make a button in unity4.6 or NGUI or In your favourite
Make the button text to Log in to FB so that u know that this button is for login.
GoTo FBHolder.cs script and change the :
1: private void SetInt()
2:
3: {
4:
5: Debug.Log("FB Init Done");
6:
7: if (FB.IsLoggedIn)
8:
9: {
10:
11: Debug.Log("FB LOGGED IN SUCCESS");
12:
13: }
14:
15: else
16:
17: {
18:
19: FB_Login();
20:
21: }
22:
23: }
To
1: private void SetInt()
2:
3: {
4:
5: Debug.Log("FB Init Done");
6:
7: if (FB.IsLoggedIn)
8:
9: {
10:
11: Debug.Log("FB LOGGED IN SUCCESS");
12:
13: }
14:
15: else
16:
17: {
18:
19: }
20:
21: }
And Make FB_Login() public so you can access via buttons.
Now make two public variables in FBHolder.cs:
1: public GameObject FBLoggedIn;
2:
3: public GameObject FBNotLoggedIn;
And drag the related objects From Hirarchy to Inspector.
Make a method in FBHolder.cs
1: void DealWithFBMenus(bool isLoggedIn)
2:
3: {
4:
5: if (isLoggedIn)
6:
7: {
8:
9: FBLoggedIn.SetActive(true);
10:
11: FBNotLoggedIn.SetActive(false);
12:
13: }
14:
15: else
16:
17: {
18:
19: FBLoggedIn.SetActive(false);
20:
21: FBNotLoggedIn.SetActive(true);
22:
23: }
24:
25: }
26:
Now call this method in :
1: private void AutoCallBack(FBResult result)
2:
3: {
4:
5: // Debug.Log(FB.IsLoggedIn ? "FB Login Worked" : "FB Login Failed");
6:
7: if (FB.IsLoggedIn)
8:
9: {
10:
11: Debug.Log("FB Login Worked");
12:
13: DealWithFBMenus(true);
14:
15: }
16:
17: else
18:
19: {
20:
21: Debug.Log("FB Login Failed");
22:
23: DealWithFBMenus(false);
24:
25: }
26:
27: }
28:
&
1: private void SetInt()
2:
3: {
4:
5: //its check wheather the FB init is done or not
6:
7: Debug.Log("FB Init Done");
8:
9: //Its check either FB logged in or not.this is the advance form of if-else statementif (FB.IsLoggedIn)
10:
11: if (FB.IsLoggedIn)
12:
13: {
14:
15: Debug.Log("FB LOGGED IN SUCCESS");
16:
17: DealWithFBMenus(true);
18:
19: }
20:
21: else
22:
23: {
24:
25: DealWithFBMenus(false);
26:
27: }
28:
29: }
Now Goto Unity Run it J.
Now We have to make an Image for FB Avatar and Text for FB username returning in FBHolder.cs
1: public GameObject FBUsername;
2:
3: public GameObject FBAvatart;
4:
Now Change the FB_Login() function to
1: public void FB_Login()
2:
3: {
4:
5: FB.Login("email", AutoCallBack);
6:
7: }
8:
This is due to FB recommend that ask user for less permission as possible at Init time and if you have other permissions then ask letter.
Now for additional work you should download Util.cs from
https://www.dropbox.com/s/o8qfjtjatzoshk3/Util.cs?dl=0
Now goto FBHolder.cs and add
FB.API(Util.GetPictureURL("me",200,200),Facebook.HttpMethod.GET,DealWithProfilePicture);
In DealWithFBMenues() after LoggedIn.
Now write a new function DealWithProfilePicture.
1: void DealWithProfilePicture(FBResult result)
2:
3: {
4:
5: if (result.Error!=null)
6:
7: {
8:
9: Debug.Log("Problem with getting picture from FaceBook");
10:
11: FB.API(Util.GetPictureURL("me", 200, 200), Facebook.HttpMethod.GET, DealWithProfilePicture);
12:
13: return;
14:
15: }
16:
17: Image UserAvatar = FBAvatart.GetComponent<Image>();
18:
19: UserAvatar.sprite = Sprite.Create(result.Texture, new Rect(0, 0, 200, 200), new Vector2(0, 0));
20:
21: }
22:
Now goto Unity and Assign the text and Image to related public variables in FBHolder.cs and Run J
Now for FB-API Queries you can Check:
https://developers.facebook.com/docs/games/unity/unity-tutorial
Now write a new function DealWithUserName.
1: void DealWithUserName(FBResult result)
2:
3: {
4:
5: if (result.Error != null)
6:
7: {
8:
9: Debug.Log("Problem with getting picture from FaceBook");
10:
11: FB.API(Util.GetPictureURL("me", 200, 200), Facebook.HttpMethod.GET, DealWithProfilePicture);
12:
13: return;
14:
15: }
16:
17: profile = Util.DeserializeJSONProfile(result.Text);
18:
19: Text UserMsg = FBUsername.GetComponent<Text>();
20:
21: UserMsg.text = "Hello ,"+profile["first_name"];
22:
23: }
Now write a new function For Sharing names it as ShareWithFriends().
1: public void ShareWithFriends()
2:
3: {
4:
5: FB.Feed(
6:
7: linkCaption:"I'm plying this awesome game",
8:
9: link:"http://www.google.com",
10:
11: linkName:"Check out this game",
12:
13: picture: "https://www.dropbox.com/s/f794nk1njk3x29i/photo.jpg.png?dl=0"
14:
15: );
16:
17: }
Now write a new function For Sharing names it as InviteWithFriends().
1: public void InviteFriends()
2:
3: {
4:
5: FB.AppRequest(
6:
7: message:"This game is awesome",
8:
9: title:"Invite your friends to join you"
10:
11: );
12:
13: }
Comments
Post a Comment