当Unity Facebook SDK中没有互联网连接时,FB.Init失败

时间:2014-02-17 09:43:09

标签: unity3d facebook-unity-sdk

由于我的应用程序的性质,我需要尽快调用FB.Init。

FB.Init(onInitDelegate);

除了没有互联网连接的情况外,这种情况一直很好。在这种情况下,onInitDelegate将不会被调用,我留下了一个挂起的应用程序

我可以为init创建一个超时,但我宁愿有一个错误回调或try-catch替代方案。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

将FB Init添加到协程并让它在那里设置一个小等待,

作为一个例子:

using UnityEngine;
using System.Collections;

public class Example: MonoBehaviour {

    private bool attemptingConnection = false;
    private bool socaillyConnected = false;
    private float trySocialCallAgain = 0.0f;

    IEnumerator WaitAndInit() {
        attemptingConnection = true;
        // Make sure the level is fully loaded.
        while(Application.isLoadingLevel()) { }
        // give it just a second.
        yield return new WaitForSeconds(1);
        try {
          FB.Init(onInitDelegate);
          sociallyConnected = true;
        } catch {
          Debug.Log("Unable to connect to facebook");
          trySocialCallAgain = Time.time + 10;
        }
        attemptingConnection = true;
    }

    void Start() {
        //StartCoroutine(WaitAndInit());
    }

    void LateUpdate() {
        if(!(sociallyConnected) && (!attemptingConnection )) {
           if(Time.time > trySocialCallAgain) {
              StartCoroutine(WaitAndInit());
           }
        }
    }
}

这将尝试在后台建立连接,如果无法建立连接,它将等待10秒再试一次。

这将等到应用程序完全加载然后再等一下,以确保,不确定,为什么我添加了该部分,我想你可以把它拿出来。 在应用程序启动并尝试连接到Facebook之后,如果不是,它将抛出一个catch异常。

希望有所帮助。