2017-11-19 11 views
0

私はここで、このコードを持っている:私はGetProfilePic関数を呼び出すとき、それは「成功」のメッセージが印刷されたにもかかわらず、nullを返し、それでも何とかUnityとFacebookは:nullの代わりに、実際の結果が返されたガイド

private Texture profilePic; 

public Texture GetProfilePic() 
{ 
    FB.API("me/picture?width=100&height=100", HttpMethod.GET, ProfilePicCallback); 

    return profilePic; 
} 

private void ProfilePicCallback(IGraphResult result) 
{ 
    if (result.Error != null || !FB.IsLoggedIn) 
    { 
     Debug.LogError(result.Error); 
    } 
    else 
    { 
     Debug.Log("FB: Successfully retrieved profile picture!"); 
     profilePic = result.Texture; 
    } 
} 

をコンソールで私はFacebookのIDなどを正しく設定しているので、それはできません。ここで何が起きていますか?これをどのように修正できますか?

+0

どの行で参照例外が発生していますか? – ZayedUpal

+0

ここでは、_asynchronous_要求を正しく処理していません。 JSでは、これはhttps://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-callの複製となるでしょう - Unityではおそらくそれほど違いはありませんが、それで、適切にそれを処理する方法をいくつか研究してください。 – CBroe

答えて

0

私はこれに対する解決策を見つけました。私は、非同期要求を正しく処理していなかったことに気付きました。

私の新しいコードは、今ではJavaScriptで行われている方法に類似の約束のデザインパターン、(!ないUnityScript)

を使用して、私はそれを正しく実装するには、ここのコードを使用:https://github.com/Real-Serious-Games/C-Sharp-Promise

これが今の私です新しいコード:

public IPromise<Texture> GetProfilePic() 
{ 
    var promise = new Promise<Texture>(); 

    FB.API("me/picture?width=100&height=100", HttpMethod.GET, (IGraphResult result) => 
    { 
     if (result.Error != null || !FB.IsLoggedIn) 
     { 
      promise.Reject(new System.Exception(result.Error)); 
     } 
     else 
     { 
      promise.Resolve(result.Texture); 
     } 
    }); 

    return promise; 
} 

次に、この関数はこのように呼び出されます。

GetProfilePic() 
    .Catch(exception => 
    { 
     Debug.LogException(exception); 
    }) 
    .Done(texture => 
    { 
     Debug.Log("FB: Successfully retrieved profile picture!"); 

     // Notice that with this method, the texture is now pushed to 
     // where it's needed. Just change this line here depending on 
     // what you need to do. 
     UIManager.Instance.UpdateProfilePic(texture); 
    }); 

これは誰かを助けることを願っています!

関連する問題