Firebase可在Unity模拟器中使用,但不能在设备上使用

时间:2019-06-14 16:35:55

标签: c# firebase unity3d firebase-authentication

我正在尝试在我的统一游戏中使用Firebase。当我在统一环境中播放场景时它可以工作,但是当我以.apk进行构建时,在我的Android设备上不起作用。

public class FirebaseAuth : MonoBehaviour
{
    public InputField EmailAddress, Password;
    public Text errorLog;
    public Text usernameText;

    Firebase.Auth.FirebaseAuth auth;
    public FirebaseUser user;

    playerInfo playerInfo;
    userInfo uInfo;
    FirebaseDB fireDB;
    DatabaseReference reference;

    private void Awake()
    {
        DontDestroyOnLoad(this);

        if (FindObjectsOfType(GetType()).Length > 1)
        {
            Destroy(gameObject);
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
        user = auth.CurrentUser;
        fireDB = FindObjectOfType<FirebaseDB> ();
        playerInfo = FindObjectOfType<playerInfo> ();

        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl ("https://jornada-no-parque.firebaseio.com/");
        FirebaseApp.DefaultInstance.SetEditorP12FileName("YOUR-FIREBASE-APP-P12.p12");
        FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("SERVICE-ACCOUNT-ID@YOUR-FIREBASE-APP.iam.gserviceaccount.com");
        FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");

        DatabaseReference reference = FirebaseDatabase.DefaultInstance.RootReference;
    }

    // Update is called once per frame
    public void createNewUser()
    {

        auth.CreateUserWithEmailAndPasswordAsync (EmailAddress.text, Password.text).ContinueWith (task => {
            if (task.IsCanceled) 
            {
                Debug.LogError ("CreateUserWithEmailAndPasswordAsync was canceled.");
                return;
            }
            if (task.IsFaulted) 
            {
                Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
                return;
            }

            //vai ser criado
            user = task.Result;

            Debug.LogFormat ("usuário Firebase criado com sucesso: {0}({1})", 
                user.DisplayName, user.UserId);
        });

        playerInfo.TriggerWriteData();
    }

    public void loginCredentials()
    {
        auth.SignInWithEmailAndPasswordAsync(EmailAddress.text, Password.text).ContinueWith(task => {
            if (task.IsCanceled) 
            {
                errorLog.text = "SignIn was canceled.";
                return;
            }
            if (task.IsFaulted) 
            {
                errorLog.text = "SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception;
                return;
            }

            //vai ser criado
            user = task.Result;

            Debug.LogFormat ("usuário acessou com sucesso: {0}({1})", 
                user.DisplayName, user.UserId);
        });

        errorLog.text = "login processing";

        SceneManager.LoadScene("01.preWorld");
    }

    void Update()
    {
        if(user == null)
        {
            errorLog.text = "no user found";
        }
        else
        {
            errorLog.text = "Bem vindo, " + user.UserId;
        }
    }
}

我认为代码正在运行,也许需要一些权限。当新用户尝试统一内部时,一切正常,但是当新用户尝试在设备上进行相同操作时,它将无法正常工作。

0 个答案:

没有答案