我是来制作一个带有firebase auth的匿名登录游戏,以及一个带有firebase实时数据库的数据库,除了firebase auth之外,一切都很顺利。问题是我不知道为什么? unity 不会连接到 Firebase 身份验证。
我已经像always database和auth一样安装了软件包
这里是我的登录脚本:
//Firebase variables
[Header("Firebase")]
public DependencyStatus dependencyStatus;
public FirebaseAuth auth;
public FirebaseUser User;
public DatabaseReference DBreference;
public static FirebaseManager instance;
void Awake()
{
if (instance != null)
{
Destroy(gameObject);
}
else
{
DontDestroyOnLoad(gameObject);
instance = this;
}
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task =>
{
dependencyStatus = task.Result;
if (dependencyStatus == DependencyStatus.Available)
{
InitializeFirebase();
}
else
{
Debug.LogError(
"Could not resolve all Firebase dependencies: " + dependency status);
}
});
}
public void InitializeFirebase()
{
auth = FirebaseAuth.DefaultInstance;
DBreference = FirebaseDatabase.DefaultInstance.RootReference;
auth.StateChanged += AuthStateChanged;
AuthStateChanged(this, null);
}
void AuthStateChanged(object sender, System.EventArgs eventArgs)
{
//This checks if the user (your local user) is the same as the one from the auth
if (auth.CurrentUser != User)
{
bool signedIn = User != auth.CurrentUser && auth.CurrentUser != null;
User = auth.CurrentUser;
if (signedIn)
{
Debug.Log("Signed in " + User.UserId + " " + User.DisplayName);
}
else
{
StartCoroutine(SignAnonymously());
}
}
}
//it does not directly log the user out but invalidates the auth
void OnDestroy()
{
auth.StateChanged -= AuthStateChanged;
auth = null;
}
public IEnumerator SignAnonymously()
{
var loginTask = auth.SignInAnonymouslyAsync();
yield return new WaitUntil(predicate: () => loginTask.IsCompleted);
if (loginTask.Exception != null)
{
//If there are errors handle them
Debug.LogWarning(message: $"Failed to register task with {loginTask.Exception}");
}
else
{
User = loginTask.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})", User.DisplayName, User.UserId);
}
}
continuewith
更改为 ContinueWithOnMainThread(task..
注意:我使用的是 unity ver 2020.3.1f1 LTS
我做错了吗?
答案 0 :(得分:1)
尝试用这个异步方法替换你的协程方法 import pyautogui, time
true=True
false=False
running = false
def execute():
time.sleep(2)
running = !running
while true:
if running == true:
pyautogui.press("w")
:
SignAnonymously
我认为问题可能是在您的 public async void SignAnonymously()
{
await 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;
}
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
});
}
末尾缺少 ContinueWith
,也许您可以在协程中使用它,但我从未这样做过,请告诉我它有效^^