我使用Unity 5.5.2f1 Pro和Facebook的SDK 7.9.4 v回调函数问题的统一
我有登录后(在以前的场景管理)发出的API请求的脚本FB要求朋友,姓名和电子邮件,并将该信息作为POST发送到php网站。
代码:
[Serializable]
public struct FBData {
public string first_name;
public string email;
public string friends;
public string id;}
public class UserManagement : MonoBehaviour {
string urlSaveUserData="some php website";
public Text testTxt;
FBData parsedData;
// Use this for initialization
void Start() {
//Check if it's the first time the user is opening the app.
if (UserInfo.FIRST_TIME) {
//update text (only used for testing, should be removed in production.)
testTxt.text = "Your user id is: " + UserInfo.ID;
//Perform FB.API call to get User Data.
getUserData();
//Save in SQL table. (won't get here if line in getUserData() is active)
StartCoroutine ("saveUserData");
} else {
//do something else.
}
注:由于这是为iOS的我有我使用在屏幕上的文字显示信息,以测试它的设备上(认为它是一个糟糕实现的打印语句)。
问题:在FB.API的回调函数中,我在文本GameObject(又名testTxt)中写入保存在Custom UserInfo clss中的响应的解析信息。它显示正确,但代码卡在那里。它不会继续下一个功能。但是,如果我删除/注释该行,并不显示任何文本字段中。这些代码会继续执行POST函数,但是API调用的信息不会被传递,即我的自定义类是空的(导致我相信回调函数根本不会被调用)。
public void getUserData(){
string query = "me?fields=first_name,email,friends";
FB.API (query, HttpMethod.GET, Apicallback, new Dictionary<string, string>());
}
private void Apicallback(IGraphResult result){
//Parse Graph response into a specific class created for this result.
parsedData = JsonUtility.FromJson<FBData>(result.RawResult);
//Pass each field into UserInfo class.
UserInfo.EMAIL = parsedData.email;
UserInfo.FRIENDS = parsedData.friends;
UserInfo.NAME = parsedData.first_name;
UserInfo.FACEBOOKID = parsedData.id;
/*problem area, if I comment line below, then previous information is apparently not stored. If left as is then testTxt displays correct information but code gets stuck there. */
testTxt.text = "This is the info from USerInfoInside the APICallback: " + UserInfo.EMAIL + UserInfo.FRIENDS + UserInfo.FACEBOOKID;
}
下面的功能是发送信息到PHP的网站,是有说明的目的:
代码:
public IEnumerator saveUserData() {
//get user info (this information is EMPTY if line in getUserData() is commented.
parsedData.id = UserInfo.FACEBOOKID;
parsedData.friends = UserInfo.FRIENDS;
parsedData.first_name = UserInfo.NAME;
parsedData.email = UserInfo.EMAIL;
//translate data into json
string JsonBodyData = JsonUtility.ToJson (parsedData);
//Custom web request (POST method doesnt seem to work very well, documentation example sends empty form)
var w = new UnityWebRequest(urlSaveUserData, "POST");
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(JsonBodyData);
w.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
w.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
w.SetRequestHeader("Content-Type", "application/json");
yield return w.Send();
//work with received data...}
我在这里停留的任何帮助表示赞赏。谢谢!
奇怪,编辑器中的行为如何? –
你有记录功能吗?你可以发布xcode日志吗? –
我不明白的一件事就是为什么你在接收到响应后,从'Start'而不是'Apicallback'函数调用'StartCoroutine(“saveUserData”)''。 –