Facebook.init()函数仍然导致null异常

时间:2014-12-12 22:21:38

标签: c# android facebook unity3d

背景 我正在尝试将facebook整合到一个统一的android项目中,我似乎无法使其工作,我已经查看了fb页面并分配了其他地方,但似乎无法找到我自己的东西做错了。

问题: 尝试FB.Login时,我得到引用异常:未加载Facebook对象。你有没有打电话给FB.init?

InitializeFB.cs代码

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Facebook.MiniJSON;
using System;

public class ConncetToFaceBook : MonoBehaviour {



    // Connect to facebook
    void Awake () {
        // Required
        DontDestroyOnLoad(gameObject);

        // Initialize FB SDK
        enabled = false;
        FB.Init(onInitComplete, OnHideUnity);

        //Display id
        Debug.Log (FB.UserId);

        //Login to facebook
        FB.Login("email,publish_actions", LoginCallback);
    }


    /* Helper Methods */
    private void onInitComplete ()
    {   
        enabled = true; // "enabled" is a property inherited from MonoBehaviour
        if (FB.IsLoggedIn) 
        {
            //Some Code            
        }
    }

    private void OnHideUnity(bool isGameShown)
    {

        //some code
    }

    void LoginCallback(FBResult result)                                                        
    {                                                                                          
        if (FB.IsLoggedIn)                                                                     
        {                                                                                      
            OnLoggedIn();                                                                      
        }                                                                                      
    }                                                                                          

    void OnLoggedIn()                                                                          
    {                                                                                          
        Debug.Log("Logged in. ID: " + FB.UserId);                                            
    } 
}

FB.cs.init()的代码

public static void Init(
        InitDelegate onInitComplete,
        string appId = "{My app ID}", //I did put my own here. Plus I use " instead of ' because ' give me a error. 
        bool cookie = true,
        bool logging = true,
        bool status = true,
        bool xfbml = true,
        bool frictionlessRequests = true,
        HideUnityDelegate onHideUnity = null,
        string authResponse = null)

2 个答案:

答案 0 :(得分:2)

FB.Init()是异步方法 - 它不会让程序等到它完成。您的FB.Login()过早被调用,您需要在FB.Init()准备就绪后调用它 - 在onInitComplete()方法内。

我的设置:

    void FBConnect(){
        if(!FB.IsInitialized){
            Debug.Log("Initializing FB");
            FB.Init(FBInitCallback, null,null);
        } else {
            Debug.Log("No need for FB init");
            FBInitCallback();       
        }
    }

    private void FBInitCallback(){
        Debug.Log("FB init OK");
        if(!FB.IsLoggedIn){
            FB.Login("email,user_friends", FBLoginCallback);
        } else {        
            //GetHisFBDataNow();
            Debug.Log("Everything is known about this guy");
        }
    }

    private void FBLoginCallback(FBResult result){
        if (result.Error != null){
            Debug.Log("FB Error Response:\n" + result.Error);
        } else if (!FB.IsLoggedIn)      {
            Debug.Log("FB Login cancelled by Player");
        } else {
            //GetHisFBDataNow();
            Debug.Log("Now also everything is known about this guy");
        }

    }

答案 1 :(得分:0)

嘿idk如果这会有所帮助,但你能尝试将fb.log放入init完成吗?