Unity Facebook SDK构建出现故障 - Unity 5.1,FB SDK 6.2.2

时间:2015-07-16 21:49:10

标签: c# facebook web unity3d build

我在使用Facebook登录/共享POC时遇到了一些问题。当我在编辑器中运行它时,一切运行正常。我可以登录,检索个人资料名称和图片并分享到墙上。然而,当我为Web Player创建构建时,没有任何功能。单击登录不会执行任何操作,并且所有UI元素都会在启动时显示,即使在代码中我已将许多UI元素设置为非活动状态,直到单击登录按钮为止。真的很感激一些帮助,我不知道为什么它被错误地构建。还有其他人经历过这个吗?

链接:https://locomoku.com/projects/facebook/

public GameObject UIFBIsLoggedIn;
public GameObject UIFBNotLoggedIn;
public GameObject UIFBAvatar;
public GameObject UIFBUserName;

private Dictionary<string, string> profile = null;


void Awake()
{
    FB.Init(SetInit, OnHideUnity);
}

private void SetInit()
{
    Debug.Log("FB Init Done");

    if(FB.IsLoggedIn)
    {
        Debug.Log("FB Logged in");
        DealWithFBMenus(true);
    } 
    else
    {
        DealWithFBMenus(false);
    }
}

private void OnHideUnity(bool isGameShown)
{
    if(!isGameShown)
    {
        Time.timeScale = 0;
    } 
    else
    {
        Time.timeScale = 1;
    }
}

public void FBLogin()
{
    FB.Login("public_profile, user_friends", AuthCallback);
}

void AuthCallback(FBResult result)
{
    if(FB.IsLoggedIn)
    {
        Debug.Log("FB Login Worked");
        DealWithFBMenus(true);
    } 
    else
    {
        Debug.Log("FB Login Fail");
        DealWithFBMenus(false);
    }
}

void DealWithFBMenus(bool isLoggedIn)
{
    if(isLoggedIn)
    {
        UIFBIsLoggedIn.SetActive(true);
        UIFBNotLoggedIn.SetActive(false);

        // Get Profile Picture Code
        FB.API(Util.GetPictureURL("me",128,128),Facebook.HttpMethod.GET,DealWithProfilePicture);

        // Get User Name Code
        FB.API("/me?fields=id,first_name", Facebook.HttpMethod.GET, DealWithUserName);

    } 
    else
    {
        UIFBIsLoggedIn.SetActive(false);
        UIFBNotLoggedIn.SetActive(true);
    }
}

void DealWithProfilePicture(FBResult result)
{
    if (result.Error == null)
    {          
        Image img = UIFBAvatar.GetComponent<Image>();
        img.sprite = Sprite.Create(result.Texture, new Rect(0,0, 128, 128), new Vector2());         
    }
    else{
        Debug.Log("Error with Profile Picture");
    }
}

void DealWithUserName(FBResult result)
{
    if (result.Error == null)
    {          
        profile = Util.DeserializeJSONProfile(result.Text);    
        Text userMsg = UIFBUserName.GetComponent<Text>();
        userMsg.text = "Hello, " + profile["first_name"];
    }
    else{
        Debug.Log("Error with Profile Name");
    }

}

public void ShareWithFriends()
{
    FB.Feed(
        linkCaption: "Test Captions",
        picture: "https://locomoku.com/projects/facebook/shot.jpg",
        linkName: "Test Link Name",
        link: "http://locomoku.com"
        );
}

Util.cs代码 -

public static string GetPictureURL(string facebookID, int? width = null, int? height = null, string type = null)
{
    string url = string.Format("/{0}/picture", facebookID);
    string query = width != null ? "&width=" + width.ToString() : "";
    query += height != null ? "&height=" + height.ToString() : "";
    query += type != null ? "&type=" + type : "";
    if (query != "") url += ("?g" + query);
    return url;
}

public static void FriendPictureCallback(FBResult result)
{
    if (result.Error != null)
    {
        Debug.LogError(result.Error);
        return;
    }

    //GameStateManager.FriendTexture = result.Texture;
}

public static Dictionary<string, string> RandomFriend(List<object> friends)
{
    var fd = ((Dictionary<string, object>)(friends[Random.Range(0, friends.Count - 1)]));
    var friend = new Dictionary<string, string>();
    friend["id"] = (string)fd["id"];
    friend["first_name"] = (string)fd["first_name"];
    return friend;
}

public static Dictionary<string, string> DeserializeJSONProfile(string response)
{
    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object nameH;
    var profile = new Dictionary<string, string>();
    if (responseObject.TryGetValue("first_name", out nameH))
    {
        profile["first_name"] = (string)nameH;
    }
    return profile;
}

public static List<object> DeserializeScores(string response) 
{

    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object scoresh;
    var scores = new List<object>();
    if (responseObject.TryGetValue ("data", out scoresh)) 
    {
        scores = (List<object>) scoresh;
    }

    return scores;
}

public static List<object> DeserializeJSONFriends(string response)
{
    var responseObject = Json.Deserialize(response) as Dictionary<string, object>;
    object friendsH;
    var friends = new List<object>();
    if (responseObject.TryGetValue("friends", out friendsH))
    {
        friends = (List<object>)(((Dictionary<string, object>)friendsH)["data"]);
    }
    return friends;
}



public static void DrawActualSizeTexture (Vector2 pos, Texture texture, float scale = 1.0f)
{
    Rect rect = new Rect (pos.x, pos.y, texture.width * scale , texture.height * scale);
    GUI.DrawTexture(rect, texture);
}
public static void DrawSimpleText (Vector2 pos, GUIStyle style, string text)
{
    Rect rect = new Rect (pos.x, pos.y, Screen.width, Screen.height);
    GUI.Label (rect, text, style);
}

0 个答案:

没有答案