当我启动游戏并点击使用Facebook登录时,我能够成功登录我的帐户,我的个人资料图片以及我的名字。 但是,当我点击播放并加载下一个场景时,我的个人资料图片没有更新,我的名字也没有。就好像我没有在第二场比赛中登录facebook。 我在场景二中的FBprofile和Name对象都被正确引用。 所以,我的问题是如何修改我的代码,以便它自动检测到我在场景2打开时登录到facebook。
using System.Collections;
using System.Collections.Generic;
using Facebook.Unity;
using UnityEngine.UI;
using UnityEngine;
public class FacebookScript : MonoBehaviour
{
public GameObject facebookButton;
public GameObject facebookUsername;
public GameObject facebookAvitar;
public GameObject facebookFrame;
void Awake()
{
FB.Init (SetInit, OnHideUnity);
DealWtihFBMenus (FB.IsLoggedIn);
}
void SetInit()
{
if (FB.IsLoggedIn)
{
Debug.Log ("Logged in");
}
else
{
Debug.Log ("FB not logged in.");
}
DealWtihFBMenus (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
{
Debug.Log ("FB Not logged in(AuthCallback)");
}
DealWtihFBMenus (FB.IsLoggedIn);
}
void DealWtihFBMenus(bool isLoggedIn)
{
if (isLoggedIn)
{
facebookButton.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
{
facebookButton.SetActive (true);
}
}
void DisplayUsername(IResult result)
{
facebookUsername.SetActive (true);
Text UserName = facebookUsername.GetComponent<Text> ();
if (result.Error == null)
{
UserName.text = "" + result.ResultDictionary ["first_name"];
}
}
void DisplayProfilePic(IGraphResult result)
{
if (result.Texture != null)
{
facebookAvitar.SetActive (true);
facebookFrame.SetActive (true);
Image ProfilePic = facebookAvitar.GetComponent<Image> ();
ProfilePic.sprite = Sprite.Create (result.Texture, new Rect (0, 0, 128, 128), new Vector2 ());
}
}
}