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



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

FB.Init(onInitDelegate);

除了没有互联网连接的情况外,这在任何时候都很有效。在这种情况下,不会调用onInitDegate,我只剩下一个挂起的应用程序

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

将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异常。

希望有所帮助。

最新更新