-
Notifications
You must be signed in to change notification settings - Fork 2
/
FBscript.cs
103 lines (81 loc) · 1.96 KB
/
FBscript.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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");
}
DealWithFBMenu (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");
}
DealWithFBMenu (FB.IsLoggedIn);
}
}
void DealWithFBMenu(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 ());
}
}
}