我正在我的游戏上运行Firebase,并且在终止游戏任务并重新打开它时,它将以某种方式重置值。我还注意到,在某些Android设备上,它甚至都无法连接到Firebase数据库。 它在编辑器中可以正常工作,所以我猜我的代码没有“真正的”问题。 但是无论如何,这是我的Firebase脚本。
using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using Firebase;
using Firebase.Auth;
using Firebase.Database;
using Firebase.Unity.Editor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class DatabaseYAuth : MonoBehaviour
{
int guestnumber = 0;
Firebase.Auth.FirebaseUser newUser;
string aux = "";
string mailconvert = "";
DatabaseReference reference;
public GameObject registerform;
public Text regemail;
public Text regpasswd;
public Text logemail;
public Text logpasswd;
private string mail;
private string passwd;
Firebase.Auth.FirebaseAuth auth;
public GameObject regerror;
public GameObject passworderror;
public GameObject loginerror;
public GameObject loginerror2;
private Boolean logged = false;
// Use this for initialization
void Start()
{
}
private void Awake()
{
StartCoroutine(LoadData());
}
// Update is called once per frame
void Update()
{
}
public IEnumerator LoadData(){
yield return new WaitForSeconds(1f * Time.deltaTime);
auth = FirebaseAuth.DefaultInstance;
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl(MY FIREBASE URL);
reference = FirebaseDatabase.DefaultInstance.RootReference;
mail = PlayerPrefs.GetString("email");
passwd = PlayerPrefs.GetString("passwd");
if (mail == null || passwd == null)
{
registerform.SetActive(true);
}
else
{
auth.SignInWithEmailAndPasswordAsync(mail, passwd).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
registerform.SetActive(true);
return;
}
newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
for (int i = 0; i < mail.Length; i++)
{
aux = mail[i].ToString();
if (aux == ".")
{
break;
}
mailconvert = mailconvert + aux;
StartCoroutine(RetrieveData());
}
FirebaseDatabase.DefaultInstance.GetReference(mailconvert).ValueChanged += HandleValueChanged;
StartCoroutine(SaveData());
});
}
}
public IEnumerator SaveData(){
while (true)
{
if (logged)
{
yield return new WaitForSeconds(2f);
reference.Child(mailconvert).Child("Money").SetValueAsync(cashController.quant);
reference.Child(mailconvert).Child("MouseLvl").SetValueAsync(Levels.mouselvl);
reference.Child(mailconvert).Child("ADNLvl").SetValueAsync(Levels.adnlvl);
reference.Child(mailconvert).Child("MicroLvl").SetValueAsync(Levels.microscopelvl);
reference.Child(mailconvert).Child("PillLvl").SetValueAsync(Levels.pilllvl);
reference.Child(mailconvert).Child("BacteriaLvl").SetValueAsync(Levels.bacterialvl);
reference.Child(mailconvert).Child("CalcLvl").SetValueAsync(Levels.calculatorlvl);
reference.Child(mailconvert).Child("AtomsLvl").SetValueAsync(Levels.atomslvl);
reference.Child(mailconvert).Child("ZoomLvl").SetValueAsync(Levels.zoomlvl);
reference.Child(mailconvert).Child("TelescopeLvl").SetValueAsync(Levels.telescopelvl);
reference.Child(mailconvert).Child("HandLvl").SetValueAsync(Levels.handlvl);
reference.Child(mailconvert).Child("GlassesLvl").SetValueAsync(Levels.glasseslvl);
reference.Child(mailconvert).Child("HealthLvl").SetValueAsync(Levels.healthlvl);
reference.Child(mailconvert).Child("WheelLvl").SetValueAsync(Levels.wheellvl);
reference.Child(mailconvert).Child("MaskLvl").SetValueAsync(Levels.masklvl);
reference.Child(mailconvert).Child("WaterLvl").SetValueAsync(Levels.waterlvl);
reference.Child(mailconvert).Child("MagnetLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("FireLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("ParachuteLvl").SetValueAsync(Levels.parachutelvl);
reference.Child(mailconvert).Child("BikeLvl").SetValueAsync(Levels.byciclelvl);
reference.Child(mailconvert).Child("CarLvl").SetValueAsync(Levels.carlvl);
reference.Child(mailconvert).Child("ShipLvl").SetValueAsync(Levels.shiplvl);
reference.Child(mailconvert).Child("PlaneLvl").SetValueAsync(Levels.planelvl);
reference.Child(mailconvert).Child("Boy?").SetValueAsync(RetrieveChara.b);
reference.Child(mailconvert).Child("Girl?").SetValueAsync(RetrieveChara.g);
reference.Child(mailconvert).Child("multiplier").SetValueAsync(cashClicker.multiplier);
reference.Child(mailconvert).Child("Mouse Reward").SetValueAsync(RewardsPerLevel.showmopanel);
reference.Child(mailconvert).Child("Adn Reward").SetValueAsync(RewardsPerLevel.showapanel);
reference.Child(mailconvert).Child("Microscope Reward").SetValueAsync(RewardsPerLevel.showmpanel);
reference.Child(mailconvert).Child("Pills Reward").SetValueAsync(RewardsPerLevel.showppanel);
reference.Child(mailconvert).Child("Bacteria Reward").SetValueAsync(RewardsPerLevel.showbpanel);
reference.Child(mailconvert).Child("Calculator Reward").SetValueAsync(RewardsPerLevel.showcpanel);
reference.Child(mailconvert).Child("Atoms Reward").SetValueAsync(RewardsPerLevel.showatpanel);
reference.Child(mailconvert).Child("Zoom Reward").SetValueAsync(RewardsPerLevel.showzpanel);
reference.Child(mailconvert).Child("Telescope Reward").SetValueAsync(RewardsPerLevel.showtpanel);
}
}
}
public IEnumerator RetrieveData()
{
while (true)
{
yield return new WaitForSeconds(.001f * Time.deltaTime);
FirebaseDatabase.DefaultInstance.GetReference(mailconvert).ValueChanged += HandleValueChanged;
logged = true;
break;
}
}
public void Register()
{
registerform.SetActive(false);
mail = regemail.text;
passwd = regpasswd.text;
PlayerPrefs.SetString("email", mail);
PlayerPrefs.SetString("passwd", passwd);
auth.CreateUserWithEmailAndPasswordAsync(mail, passwd).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
passworderror.SetActive(true);
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
regerror.SetActive(true);
return;
}
for (int i = 0; i < mail.Length; i++)
{
aux = mail[i].ToString();
if (aux == ".")
{
break;
}
mailconvert = mailconvert + aux;
}
// Firebase user has been created.
FirebaseUser newUser = task.Result;
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
reference.Child(mailconvert).Child("Money").SetValueAsync(cashController.quant);
reference.Child(mailconvert).Child("MouseLvl").SetValueAsync(Levels.mouselvl);
reference.Child(mailconvert).Child("ADNLvl").SetValueAsync(Levels.adnlvl);
reference.Child(mailconvert).Child("MicroLvl").SetValueAsync(Levels.microscopelvl);
reference.Child(mailconvert).Child("PillLvl").SetValueAsync(Levels.pilllvl);
reference.Child(mailconvert).Child("BacteriaLvl").SetValueAsync(Levels.bacterialvl);
reference.Child(mailconvert).Child("CalcLvl").SetValueAsync(Levels.calculatorlvl);
reference.Child(mailconvert).Child("AtomsLvl").SetValueAsync(Levels.atomslvl);
reference.Child(mailconvert).Child("ZoomLvl").SetValueAsync(Levels.zoomlvl);
reference.Child(mailconvert).Child("TelescopeLvl").SetValueAsync(Levels.telescopelvl);
reference.Child(mailconvert).Child("HandLvl").SetValueAsync(Levels.handlvl);
reference.Child(mailconvert).Child("GlassesLvl").SetValueAsync(Levels.glasseslvl);
reference.Child(mailconvert).Child("HealthLvl").SetValueAsync(Levels.healthlvl);
reference.Child(mailconvert).Child("WheelLvl").SetValueAsync(Levels.wheellvl);
reference.Child(mailconvert).Child("MaskLvl").SetValueAsync(Levels.masklvl);
reference.Child(mailconvert).Child("WaterLvl").SetValueAsync(Levels.waterlvl);
reference.Child(mailconvert).Child("MagnetLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("FireLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("ParachuteLvl").SetValueAsync(Levels.parachutelvl);
reference.Child(mailconvert).Child("BikeLvl").SetValueAsync(Levels.byciclelvl);
reference.Child(mailconvert).Child("CarLvl").SetValueAsync(Levels.carlvl);
reference.Child(mailconvert).Child("ShipLvl").SetValueAsync(Levels.shiplvl);
reference.Child(mailconvert).Child("PlaneLvl").SetValueAsync(Levels.planelvl);
reference.Child(mailconvert).Child("Boy?").SetValueAsync(RetrieveChara.b);
reference.Child(mailconvert).Child("Girl?").SetValueAsync(RetrieveChara.g);
reference.Child(mailconvert).Child("multiplier").SetValueAsync(cashClicker.multiplier);
reference.Child(mailconvert).Child("Mouse Reward").SetValueAsync(RewardsPerLevel.showmopanel);
reference.Child(mailconvert).Child("Adn Reward").SetValueAsync(RewardsPerLevel.showapanel);
reference.Child(mailconvert).Child("Microscope Reward").SetValueAsync(RewardsPerLevel.showmpanel);
reference.Child(mailconvert).Child("Pills Reward").SetValueAsync(RewardsPerLevel.showppanel);
reference.Child(mailconvert).Child("Bacteria Reward").SetValueAsync(RewardsPerLevel.showbpanel);
reference.Child(mailconvert).Child("Calculator Reward").SetValueAsync(RewardsPerLevel.showcpanel);
reference.Child(mailconvert).Child("Atoms Reward").SetValueAsync(RewardsPerLevel.showatpanel);
reference.Child(mailconvert).Child("Zoom Reward").SetValueAsync(RewardsPerLevel.showzpanel);
reference.Child(mailconvert).Child("Telescope Reward").SetValueAsync(RewardsPerLevel.showtpanel);
StartCoroutine(SaveData());
loginerror.SetActive(false);
loginerror2.SetActive(false);
regerror.SetActive(false);
passworderror.SetActive(false);
});
}
public void LogIn()
{
mail = logemail.text;
passwd = logpasswd.text;
PlayerPrefs.SetString("email", mail);
PlayerPrefs.SetString("passwd", passwd);
auth.SignInWithEmailAndPasswordAsync(mail, passwd).ContinueWith(task => {
if (task.IsCanceled)
{
Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
loginerror.SetActive(true);
return;
}
if (task.IsFaulted)
{
Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
loginerror2.SetActive(true);
return;
}
newUser = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
for (int i = 0; i < mail.Length; i++)
{
aux = mail[i].ToString();
if (aux == ".")
{
break;
}
mailconvert = mailconvert + aux;
}
FirebaseDatabase.DefaultInstance.GetReference(mailconvert).ValueChanged += HandleValueChanged;
cashClicker.multiplier = StoreController.adnm + StoreController.atomsm + StoreController.bacteriam + StoreController.calculatorm
+ StoreController.microscopem + StoreController.mousem + StoreController.pillm + StoreController.telescopem
+ StoreController.zoomm;
registerform.SetActive(false);
loginerror.SetActive(false);
loginerror2.SetActive(false);
regerror.SetActive(false);
passworderror.SetActive(false);
});
}
void HandleValueChanged(object sender, ValueChangedEventArgs args)
{
if (args.DatabaseError != null)
{
Debug.LogError(args.DatabaseError.Message);
return;
}
cashController.quant = float.Parse(args.Snapshot.Child("Money").Value.ToString());
Levels.mouselvl = float.Parse(args.Snapshot.Child("MouseLvl").Value.ToString());
Levels.adnlvl = float.Parse(args.Snapshot.Child("ADNLvl").Value.ToString());
Levels.microscopelvl = float.Parse(args.Snapshot.Child("MicroLvl").Value.ToString());
Levels.pilllvl = float.Parse(args.Snapshot.Child("PillLvl").Value.ToString());
Levels.bacterialvl = float.Parse(args.Snapshot.Child("BacteriaLvl").Value.ToString());
Levels.calculatorlvl = float.Parse(args.Snapshot.Child("CalcLvl").Value.ToString());
Levels.atomslvl = float.Parse(args.Snapshot.Child("AtomsLvl").Value.ToString());
Levels.zoomlvl = float.Parse(args.Snapshot.Child("ZoomLvl").Value.ToString());
Levels.telescopelvl = float.Parse(args.Snapshot.Child("TelescopeLvl").Value.ToString());
Levels.handlvl = float.Parse(args.Snapshot.Child("HandLvl").Value.ToString());
Levels.glasseslvl = float.Parse(args.Snapshot.Child("GlassesLvl").Value.ToString());
Levels.healthlvl = float.Parse(args.Snapshot.Child("HealthLvl").Value.ToString());
Levels.wheellvl = float.Parse(args.Snapshot.Child("WheelLvl").Value.ToString());
Levels.masklvl = float.Parse(args.Snapshot.Child("MaskLvl").Value.ToString());
Levels.waterlvl = float.Parse(args.Snapshot.Child("WaterLvl").Value.ToString());
Levels.magnetlvl = float.Parse(args.Snapshot.Child("MagnetLvl").Value.ToString());
Levels.firelvl = float.Parse(args.Snapshot.Child("FireLvl").Value.ToString());
Levels.parachutelvl = float.Parse(args.Snapshot.Child("ParachuteLvl").Value.ToString());
Levels.byciclelvl = float.Parse(args.Snapshot.Child("BikeLvl").Value.ToString());
Levels.carlvl = float.Parse(args.Snapshot.Child("CarLvl").Value.ToString());
Levels.shiplvl = float.Parse(args.Snapshot.Child("ShipLvl").Value.ToString());
Levels.planelvl = float.Parse(args.Snapshot.Child("PlaneLvl").Value.ToString());
RetrieveChara.b = bool.Parse(args.Snapshot.Child("Boy?").Value.ToString());
RetrieveChara.g = bool.Parse(args.Snapshot.Child("Girl?").Value.ToString());
cashClicker.multiplier = float.Parse(args.Snapshot.Child("multiplier").Value.ToString());
RewardsPerLevel.showmopanel = bool.Parse(args.Snapshot.Child("Mouse Reward").Value.ToString());
RewardsPerLevel.showapanel = bool.Parse(args.Snapshot.Child("Adn Reward").Value.ToString());
RewardsPerLevel.showmpanel = bool.Parse(args.Snapshot.Child("Microscope Reward").Value.ToString());
RewardsPerLevel.showppanel = bool.Parse(args.Snapshot.Child("Pills Reward").Value.ToString());
RewardsPerLevel.showbpanel = bool.Parse(args.Snapshot.Child("Bacteria Reward").Value.ToString());
RewardsPerLevel.showcpanel = bool.Parse(args.Snapshot.Child("Calculator Reward").Value.ToString());
RewardsPerLevel.showatpanel = bool.Parse(args.Snapshot.Child("Atoms Reward").Value.ToString());
RewardsPerLevel.showzpanel = bool.Parse(args.Snapshot.Child("Zoom Reward").Value.ToString());
RewardsPerLevel.showtpanel = bool.Parse(args.Snapshot.Child("Telescope Reward").Value.ToString());
}
public void SkipRegister(){
registerform.SetActive(false);
guestnumber++;
mail = "guest" + guestnumber + "@customuser.com";
string characs = "abcdefghijklmnopqrstuvwxyz1234567890|$%&/!";
for (int i = 0; i < 12; i++)
{
int numr = Random.Range(0, characs.Length);
passwd += characs[numr];
}
Debug.Log(passwd);
PlayerPrefs.SetString("email", mail);
PlayerPrefs.SetString("passwd", passwd);
auth.CreateUserWithEmailAndPasswordAsync(mail, passwd).ContinueWith(task =>
{
if (task.IsCanceled)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
return;
}
if (task.IsFaulted)
{
Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
SkipRegister();
return;
}
for (int i = 0; i < mail.Length; i++)
{
aux = mail[i].ToString();
if (aux == ".")
{
break;
}
mailconvert = mailconvert + aux;
}
// Firebase user has been created.
Firebase.Auth.FirebaseUser newUser = task.Result;
Debug.LogFormat("Firebase user created successfully: {0} ({1})",
newUser.DisplayName, newUser.UserId);
reference.Child(mailconvert).Child("Money").SetValueAsync(cashController.quant);
reference.Child(mailconvert).Child("MouseLvl").SetValueAsync(Levels.mouselvl);
reference.Child(mailconvert).Child("ADNLvl").SetValueAsync(Levels.adnlvl);
reference.Child(mailconvert).Child("MicroLvl").SetValueAsync(Levels.microscopelvl);
reference.Child(mailconvert).Child("PillLvl").SetValueAsync(Levels.pilllvl);
reference.Child(mailconvert).Child("BacteriaLvl").SetValueAsync(Levels.bacterialvl);
reference.Child(mailconvert).Child("CalcLvl").SetValueAsync(Levels.calculatorlvl);
reference.Child(mailconvert).Child("AtomsLvl").SetValueAsync(Levels.atomslvl);
reference.Child(mailconvert).Child("ZoomLvl").SetValueAsync(Levels.zoomlvl);
reference.Child(mailconvert).Child("TelescopeLvl").SetValueAsync(Levels.telescopelvl);
reference.Child(mailconvert).Child("HandLvl").SetValueAsync(Levels.handlvl);
reference.Child(mailconvert).Child("GlassesLvl").SetValueAsync(Levels.glasseslvl);
reference.Child(mailconvert).Child("HealthLvl").SetValueAsync(Levels.healthlvl);
reference.Child(mailconvert).Child("WheelLvl").SetValueAsync(Levels.wheellvl);
reference.Child(mailconvert).Child("MaskLvl").SetValueAsync(Levels.masklvl);
reference.Child(mailconvert).Child("WaterLvl").SetValueAsync(Levels.waterlvl);
reference.Child(mailconvert).Child("MagnetLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("FireLvl").SetValueAsync(Levels.firelvl);
reference.Child(mailconvert).Child("ParachuteLvl").SetValueAsync(Levels.parachutelvl);
reference.Child(mailconvert).Child("BikeLvl").SetValueAsync(Levels.byciclelvl);
reference.Child(mailconvert).Child("CarLvl").SetValueAsync(Levels.carlvl);
reference.Child(mailconvert).Child("ShipLvl").SetValueAsync(Levels.shiplvl);
reference.Child(mailconvert).Child("PlaneLvl").SetValueAsync(Levels.planelvl);
reference.Child(mailconvert).Child("Boy?").SetValueAsync(RetrieveChara.b);
reference.Child(mailconvert).Child("Girl?").SetValueAsync(RetrieveChara.g);
reference.Child(mailconvert).Child("multiplier").SetValueAsync(cashClicker.multiplier);
reference.Child(mailconvert).Child("Mouse Reward").SetValueAsync(RewardsPerLevel.showmopanel);
reference.Child(mailconvert).Child("Adn Reward").SetValueAsync(RewardsPerLevel.showapanel);
reference.Child(mailconvert).Child("Microscope Reward").SetValueAsync(RewardsPerLevel.showmpanel);
reference.Child(mailconvert).Child("Pills Reward").SetValueAsync(RewardsPerLevel.showppanel);
reference.Child(mailconvert).Child("Bacteria Reward").SetValueAsync(RewardsPerLevel.showbpanel);
reference.Child(mailconvert).Child("Calculator Reward").SetValueAsync(RewardsPerLevel.showcpanel);
reference.Child(mailconvert).Child("Atoms Reward").SetValueAsync(RewardsPerLevel.showatpanel);
reference.Child(mailconvert).Child("Zoom Reward").SetValueAsync(RewardsPerLevel.showzpanel);
reference.Child(mailconvert).Child("Telescope Reward").SetValueAsync(RewardsPerLevel.showtpanel);
StartCoroutine(SaveData());
loginerror.SetActive(false);
loginerror2.SetActive(false);
regerror.SetActive(false);
passworderror.SetActive(false);
});
}
}
谢谢。