我在Unity 3D上获得facebook名称时出错,这个功能明显在2周前工作,
FB.Init(this.InitCallback,this.OnHideUnity);
private void InitCallback ()
{
if (FB.IsInitialized) {
if (FB.IsLoggedIn) {
FB.LogInWithReadPermissions(new List<string>() { "public_profile","email" }, this.AuthCallback);
} else {
}
} else {
Debug.Log("Failed to Initialize the Facebook SDK");
}
}
private void AuthCallback (ILoginResult result) {
if (FB.IsLoggedIn) {
FB.API("/me?fields=name", HttpMethod.GET, this.LoginCallback2);
} else {
//Debug.Log("User cancelled login");
}
}
void LoginCallback2(IResult result)
{
if (result.Error != null) {
Debug.Log(result.Error);
}
else{
fbid = result.ResultDictionary ["id"].ToString();
}
}
我在LoginCallback2函数上遇到错误,调试如下:
UnityEngine.Debug:Log(Object)
connect:LoginCallback2(IResult) (at Assets/connect.cs:983)
Facebook.Unity.<Start>d__9:MoveNext()
UnityEngine.SetupCoroutine:InvokeMoveNext(IEnumerator, IntPtr)
答案 0 :(得分:0)
简短的回答是,FB尚未初始化,或者用户未登录。下面我分享了自己的代码,用于检索用户的全名及其FBID。在调用此代码部分之前,您需要检查以确保以下内容为真:
FB.IsInitialized == true
和
FB.IsLoggedIn == true
auth = FirebaseAuth.getInstance();
db = FirebaseDatabase.getInstance();
ref = db.getReference();
FirebaseUser user = auth.getCurrentUser();
uid = user.getUid();//getuser id
run = (EditText) findViewById(R.id.Run);
rpw = (EditText) findViewById(R.id.Rpw);
rage = (EditText) findViewById(R.id.Rage);
//rbm = (RadioButton) findViewById(R.id.rbmale);
//rbfm = (RadioButton) findViewById(R.id.rbfemale);
re = (EditText) findViewById(R.id.Re);
rpn = (EditText) findViewById(R.id.Rpn);
ra = (EditText) findViewById(R.id.Ra);
rpc = (EditText) findViewById(R.id.Rpc);
//rbstd = (RadioButton) findViewById(R.id.rbstd);
//rbtt= (RadioButton) findViewById(R.id.rbtt);
final Button br = (Button) findViewById(R.id.Rbt);
br.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
usnm = run.getText().toString().trim();
pswd = rpw.getText().toString().trim();
email = re.getText().toString().trim();
ref.child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Object a = dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (TextUtils.isEmpty(usnm)){
Toast.makeText(Register.this, "Username cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (usnm.length()<8){
Toast.makeText(Register.this, "Username cannot less then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (usnm.length()>8){
Toast.makeText(Register.this, "Username cannot greater then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (ref.child(uid).child("username").equals(usnm)){
Toast.makeText(Register.this, "Username had been taken already! Please try another one.", Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(pswd)){
Toast.makeText(Register.this, "Password cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (pswd.length()<8){
Toast.makeText(Register.this, "Password cannot less then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (pswd.length()>8){
Toast.makeText(Register.this, "Password cannot greater then 8 characters", Toast.LENGTH_SHORT).show();
return;
}else if (TextUtils.isEmpty(email)){
Toast.makeText(Register.this, "Email cannot be empty", Toast.LENGTH_SHORT).show();
return;
}else if (ref.child(uid).child("email").equals(email)){
Toast.makeText(Register.this, "Email had been taken already! Please try another one.", Toast.LENGTH_SHORT).show();
return;
}
register(usnm, pswd, email);
}//end of onclick
});