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

本文关键字:异常 init 函数 Facebook | 更新日期: 2023-09-27 18:34:13

背景:我正在尝试为一个 unity-android 项目集成 facebook,但我似乎无法让它工作,我已经查看了 fb 页面和其他地方,但似乎找不到我做错了什么。

问题:尝试FB时。登录我得到参考异常:Facebook对象未加载。你打电话给FB.init了吗?

初始化FB的代码

.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)

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

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");
        }
    }

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