페이스북 앱을 만들었을경우 invitable_friends 를 가져 올수 있는데
유저의 해당 앱에 초대할 친구 목록이다 즉 가입이 안된 친구들 목록이다
나의 친구중
//해당앱에 초대된 유저
FB.API ("/me/friends?fields=id,name,picture", HttpMethod.GET, FriendListCallBack);
//해당앱에 초대가 안된 유저
FB.API ("/me/invitable_friends?fields=id,name,picture", HttpMethod.GET, InvitableFriendListCallBack);
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;
using Facebook.MiniJSON;
public class FBHolder : MonoBehaviour {
public GameObject DialogLoggedIn;
public GameObject DialogLoggedOut;
public GameObject DialogUserName;
public GameObject DialogPrifilePic;
public GameObject FriendListGm;
public GameObject Invitable_FriendListGM;
private Dictionary<string,string> friendListDic;
private Dictionary<string,string> invitable_friendListDic;
void Awake(){
FB.Init (SetInit, OnHideUnity);
}
private void SetInit(){
//Screen.SetResolution( Screen.width, Screen.width * 16 / 9, true );
Debug.Log ("FB init donw");
if (FB.IsLoggedIn) {
Debug.Log ("Fb Logged In");
} else {
Debug.Log ("Fb Not Logged In");
}
//DealWidthFBMenu (FB.IsLoggedIn);
}
private 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 Logged In AuthCallBack");
} else {
Debug.Log ("Fb Not Logged In AuthCallBack");
}
DealWidthFBMenu (FB.IsLoggedIn);
}
}
void DealWidthFBMenu(bool isLoggedIn){
if (isLoggedIn) {
DialogLoggedIn.SetActive (true);
DialogLoggedOut.SetActive (false);
FB.API ("/me?fields=first_name", HttpMethod.GET, DialogUserNameCallBack);
FB.API ("/me/picture?type=square&height=128&width=128", HttpMethod.GET, DialogUserProfileImgCallBack);
FB.API ("/me/friends?fields=id,name,picture", HttpMethod.GET, FriendListCallBack);
FB.API ("/me/invitable_friends?fields=id,name,picture", HttpMethod.GET, InvitableFriendListCallBack);
} else {
DialogLoggedIn.SetActive (false);
DialogLoggedOut.SetActive (true);
}
}
void DialogUserNameCallBack(IResult result){
Text UsetName = DialogUserName.GetComponent<Text> ();
if (result.Error == null) {
UsetName.text = "Hi there, " + result.ResultDictionary ["first_name"];
} else {
Debug.Log (result.Error);
}
}
void DialogUserProfileImgCallBack(IGraphResult result){
if (result.Texture != null) {
Image ProfilePic = DialogPrifilePic.GetComponent<Image> ();
ProfilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
} else {
Debug.Log (result.Error);
}
}
//게임등록을 안한 유저
void InvitableFriendListCallBack(IResult result){
if (result.Error != null) {
Debug.Log ("Error : " + result.Error);
} else {
invitable_friendListDic = FriendListParsing.Parsing (result.RawResult);
SetText ( Invitable_FriendListGM , invitable_friendListDic);
}
}
//게임등록을 한 유저
void FriendListCallBack(IResult result){
if (result.Error != null) {
Debug.Log ("Error : " + result.Error);
} else {
friendListDic = FriendListParsing.Parsing (result.RawResult);
SetText (FriendListGm, friendListDic);
}
}
//리스ㅌ트를 텍스트로 뿌려준다
void SetText(GameObject gm, Dictionary<string,string> dic){
Text textField = gm.GetComponent<Text> ();
foreach (KeyValuePair<string,string> pair in dic) {
textField.text += string.Format ("{0}", pair.Key, pair.Value)+"\n";
}
}
}
파싱용 스크립트
using Facebook.MiniJSON;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;
public class FriendListParsing {
//친구 리스트 파싱
static public Dictionary<string,string> Parsing(string text){
Debug.Log ("parsing " + text);
//이곳에 json이 담겨져 있ㅏ
Dictionary<string, object> dict = (Dictionary<string,object>)Json.Deserialize (text);
object friendsH;
List<object> friends = new List<object> ();
string friendName;
Dictionary<string,string> friend = new Dictionary<string,string> ();
if (dict.TryGetValue ("data", out friendsH)) {
friends = (List<object>)dict ["data"];
if (friends.Count > 0) {
for (int i = 0; i< friends.Count; i++) {
Dictionary<string,object> friendDict = ((Dictionary<string,object>)(friends [i]));
friend [(string)friendDict ["name"]] = (string)friendDict ["name"];
}
}
}
return friend;
}
}
http://blog.naver.com/brane7/220712830773
[출처] Facebook - 로그인 - 친구리스트 불러오기 - 5|작성자 LET
'Program > Unity' 카테고리의 다른 글
google sdk (0) | 2017.02.23 |
---|---|
[Unity3D] Application.LoadLevel(string) is obsolete 마이그레이션 (0) | 2017.02.20 |