简体   繁体   English

Unity 登陆 anynomous android

[英]Unity sign in anynomous on android

I am trying to sign in anynomous on my android device in a game I have developed.我正在尝试在我开发的游戏中的 android 设备上登录 anynomous。 When I run the game in the Unity Editor on my Macbook, everything runs smoothly and the database is update.当我在我的 Macbook 上的 Unity 编辑器中运行游戏时,一切运行顺利并且数据库已更新。

But when I compile and run it on my Samsung device, nothing happens, it just freezes.但是当我在我的三星设备上编译和运行它时,什么也没有发生,它只是死机了。 Does anyone have a clue why?有谁知道为什么? I tried adding the sha1 key to the fingeprints of my project on Firebase, but no luck.我尝试将 sha1 密钥添加到我在 Firebase 上的项目的指纹中,但没有成功。

Here is the code:这是代码:

private void anonymousSignIn()
{
    auth = Firebase.Auth.FirebaseAuth
        .DefaultInstance;
    auth.SignInAnonymouslyAsync().ContinueWith(task => {
        if (task.IsCanceled)
        {
            Debug.LogError("SignInAnonymouslyAsync was canceled.");
            return;
        }
        if (task.IsFaulted)
        {
            Debug.LogError("SignInAnonymouslyAsync encountered an error: " + task.Exception);
            return;
        }

        user = task.Result;
        Debug.LogFormat("User signed in successfully: {0} ({1})",
            user.DisplayName, user.UserId);
        print("User signed in");
        removeUser();
    });
    print("Auth done");
}

private void prepareDatabase()
{
    FirebaseApp.DefaultInstance
        .SetEditorDatabaseUrl("mydatabaseadress/");
    DatabaseReference reference =
        FirebaseDatabase.DefaultInstance.RootReference;
    string userId = "Erik";
    User user = new User(userId, 24);
    string json = JsonUtility.ToJson(user);
    reference.Child("highscore").Child(userId).SetRawJsonValueAsync(json);
}

I'd need a stack trace at the point of the freeze or the Android logcat output to see what exactly is happening *I'd recommend this Unity plugin to help with parsing LogCat).我需要在冻结点的堆栈跟踪或 Android logcat output 来查看到底发生了什么*我推荐这个 Unity 插件来帮助解析 LogCat)。 Without knowing how all of this hooks together, it's hard to tell what "freeze" may mean in the context of your game or which call may be causing your problem.如果不知道所有这些是如何联系在一起的,就很难说出“冻结”在您的游戏上下文中可能意味着什么,或者哪个调用可能会导致您的问题。 I do see a few possible issues:我确实看到了一些可能的问题:

1) SetEditorDatabaseUrl is not necessary, and I never use it in my own projects. 1) SetEditorDatabaseUrl不是必需的,我从来没有在自己的项目中使用它。 I assume that "mydatabaseadress/" is a temporary value for this stack overflow question, but it's also an unnecessary potential bug surface if you only have one database in your project.我假设"mydatabaseadress/"是这个堆栈溢出问题的临时值,但如果您的项目中只有一个数据库,它也是一个不必要的潜在错误面。

2) removeUser may be doing some work in Unity. 2) removeUser可能在 Unity 中做一些工作。 For that reason, you should replace ContinueWith with ContinueWithOnMainThread (see this ).因此,您应该将ContinueWith替换为ContinueWithOnMainThread (请参阅)。 That will also make sure that you get all of the necessary logging.这也将确保您获得所有必要的日志记录。

3) auth.CurrentUser will persist between runs of your game. 3) auth.CurrentUser将在您的游戏运行之间持续存在。 Each call to auth.SignInAnonymouslyAsync will create a new user - even if one's already been registered.每次调用auth.SignInAnonymouslyAsync都会创建一个新用户- 即使已经注册了一个用户。 You should change your auth code to something like:您应该将您的授权码更改为:

auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
if (auth.CurrentUser == null) {
    auth.SignInAnonymouslyAsync().ContinueWithOnMainThread(task => {

And remember to do any important work (such as calling removeUser() ) in an else .并记住在else中做任何重要的工作(例如调用removeUser() )。

4) In your prepareDatabase call, you have a hardcoded string userId = "Erik" . 4) 在您的prepareDatabase调用中,您有一个硬编码string userId = "Erik" If you have a database rule like:如果您有这样的数据库规则:

{
  "rules": {
    "highscore": {
      "$uid": {
        ".write": "$uid === auth.uid"
      }
    }
  }
}

Then it will always be rejected.那么它总是会被拒绝。

5) Similarly, if you're not waiting for auth.SignInAnonymouslyAsync to complete before calling prepareDatabase , the default "auth != null" may automatically fail. 5) 同样,如果您在调用prepareDatabase之前没有等待auth.SignInAnonymouslyAsync完成,默认的"auth != null"可能会自动失败。

I hope that helps!希望对您有所帮助!

--Patrick ——帕特里克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Apple 登录 - 使用 Firebase 实现 Android - Sign in With Apple - Android Implementation with Firebase Twitter 在 Android 上使用 FirebaseUI 登录 - Twitter sign-in with FirebaseUI on Android 用户没有退出 firebase android - User is not getting sign out firebase android 如何在 Flutter Android 上编写 Firebase 用 Apple 登录的功能 - How to write Firebase Functions for Sign in with Apple on Flutter Android Apple 登录出现错误“Invalid_client”firebase android - Apple Sign in getting error "Invalid_client" firebase android Google Firebase 注销并忘记 Android 应用中的用户 - Google Firebase sign out and forget user in Android app 基于Unity的离线登录 Android Apps - Offline Login for Unity-Based Android Apps unity Firebase Facebook auth on Android - Unity Firebase Facebook auth on Android Flutter:使用 google_sign_in 时出现 PlatformException(sign_in_failed,com.google.android.gms.common.api.ApiException:10:,null) - Flutter : PlatformException(sign_in_failed, com.google.android.gms.common.api.ApiException: 10: , null) when using google_sign_in 从 Android/iOS Unity 应用程序向家庭 PC 发送消息并返回消息 - Sending messages to home PC and back from Android/iOS Unity app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM