APP

[Unity] 페이스북로그인샘플, FaceBook Login Sample

폰타인 2020. 1. 30. 11:37

 

 

 

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class FBManager : MonoBehaviour
{
    public static FBManager instanace;
    public void Awake()
    {
        FBManager.instanace = this;
    }
    // Start is called before the first frame update
    void Start()
    {
 
    }
    public void SignIn(System.Action<bool> complete)
    {
        FB.LogInWithReadPermissions(new List<string>() { "public_profile""email""user_friends" },
            
            (result) =>
            {
                if(result != null)
                {
                    Debug.LogFormat("Error: {0}"result.Error);
                    Debug.LogFormat("Cancelled: {0}", result.Cancelled);
                    Debug.LogFormat("RawResult: {0}", result.RawResult);
                    Debug.LogFormat("result: {0}", result.ToString());
 
                    var isSuccess = string.IsNullOrEmpty(result.Error) && !result.Cancelled;
 
                    complete(isSuccess);
                }
                
            });
 
    }
    public void Init()
    {
 
        FB.Init(() =>
       {
           if (AccessToken.CurrentAccessToken != null)
           {
           }
       }
        , (isGameShown) => { });
 
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.Networking;
 
public class Test : MonoBehaviour
{
    public Button btnGpgs;
    public Button btnFb;
 
    public Text txtGpgsSignInResult;
    public Text txtFbSignInResult;
 
    public Text txtGpgs;//GPGS 로그인정보
    public Text txtFb; //Facebook 로그인정보
 
    public Image imgGpgs; //Gpgs 로그인정보(Google Play Game 썸네일아이콘)
    public Image imgFb; //Facebook 로그인정보
 
    public System.Action<bool> OnCompleteGpgsSignIn;
    public System.Action<bool> OnCompleteFbSignIn;
    // Start is called before the first frame update
 
    void Start()
    {
 
        GpgsSignIn();
        FaceBookSignIn();
    }
 
 
    private void FaceBookSignIn()
    {
        OnCompleteFbSignIn = (success) =>
        {
            Debug.Log("success: " + success);
 
            if (success)
            {
                this.GetFbUserProfile("me?field=name,id,birthday", (dicData) =>
                {
                    var name = dicData["name"];
                    var id = dicData["id"];
                    //birthday가 왜안되는지 key not found
 
                    this.txtFb.text = string.Format($"name: {name}\n id: {id}");
 
                });
 
 
 
                FB.API("/me/picture?redirect=false"HttpMethod.GET, (result) =>
                {
                    if (string.IsNullOrEmpty(result.Error) && !result.Cancelled)
                    {
                        IDictionary data = result.ResultDictionary["data"as IDictionary;
                        string photoURL = data["url"as string;
 
                        StartCoroutine(FetchProfilePic2(photoURL));
                    }
                });
            }
        };
 
        this.btnFb.onClick.AddListener(() =>
        {
            Debug.Log("Fb SignIn Button Pressed");
        });
    }
 
    private void GetFbUserProfile(string uri, System.Action<IDictionary<stringobject>> complete)
    {
        {
            foreach (var kv in data.ResultDictionary)
            {
                Debug.Log($"data.ResultDictionary:  {kv.Key}/{kv.Value.ToString()}");
            }
            complete(data.ResultDictionary);
        });
    }
    IEnumerator FetchProfilePic2(string url)
    {
        Debug.LogFormat("url: {0}", url);     
 
        UnityWebRequest webRequest = UnityWebRequestTexture.GetTexture(url);
 
        yield return webRequest.SendWebRequest();
 
        Debug.LogFormat("data length: {0}"webRequest.downloadHandler.data.Length);
        Texture tex = DownloadHandlerTexture.GetContent(webRequest);
 
       
 
 
        imgFb.sprite = Sprite.Create((Texture2D)tex, new Rect(00tex.width, tex.height), new Vector2(0.5f, 0.5f));
 
 
    }
 
 
    //IEnumerator FetchProfilePic(string url)
    //{
 
    //    using (UnityWebRequest webRequest = UnityWebRequest.Get(url))
    //    {
    //        // Request and wait for the desired page.
    //        yield return webRequest.SendWebRequest();
 
    //        Debug.LogFormat("data length: {0}", webRequest.downloadHandler.data.Length);
 
    //        Texture2D tex = new Texture2D(360, 360, TextureFormat.ETC2_RGB, false);
 
    //        tex.LoadRawTextureData(webRequest.downloadHandler.data);
    //        tex.Apply();
 
 
    //    }
    //}
 
    private void GpgsSignIn()
    {
        OnCompleteGpgsSignIn = (success) =>
        {
            this.txtGpgsSignInResult.text = success.ToString();
 
            if (success)
            {
                var id = Social.localUser.id; //아이디
                var userName = Social.localUser.userName; //유저이름
                var underAge = Social.localUser.underage; //연령               
 
                StartCoroutine(this.WaitForLoadImage(() =>
                {
                    var width = this.imgGpgs.rectTransform.rect.width;
                    var height = this.imgGpgs.rectTransform.rect.height;
                    var image = Social.localUser.image;
                    this.imgGpgs.sprite = Sprite.Create(image, new Rect(00, width, height), new Vector2(0.5f, 0.5f));
                    this.txtGpgs.text = string.Format("id: {0}\nuserName: {1}\n", id, userName);
                }));
            }
 
        };
 
        this.btnGpgs.onClick.AddListener(() =>
        {
            Debug.Log("SignIn Button Pressed");
        });
    }
 
    IEnumerator WaitForLoadImage(System.Action OnLoadImage)
    {
        Debug.Log("StartCoroutine");
 
        yield return ((PlayGamesLocalUser)Social.localUser).LoadImage();
        Debug.Log("yield return LoadImage");
 
        while (true)
        {
            if (Social.localUser.image != null)
            {
                Debug.Log($"image Loaded");
                break;
            }
            yield return null;
        }
 
        OnLoadImage();
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter