Firebase Saving and Loading
You can Save data to the Firebase Realtime Database using Save Game Pro Firebase integration.
You first need to authenticate the User, like creating an account by Email and Password:
public void CreateAccount ()
{
if ( string.IsNullOrEmpty ( emailInputField.text ) )
{
Debug.LogError ( "Email is Empty!" );
return;
}
try
{
// Disable all controls.
SetInteractableAll ( false );
// Create user with the email and password.
FirebaseAuth.DefaultInstance.CreateUserWithEmailAndPasswordAsync ( emailInputField.text, passwordInputField.text ).ContinueWith ( (task ) =>
{
if ( task.IsFaulted )
{
Debug.LogError ( "Account Creation Failed, here is why:" );
Debug.LogException ( task.Exception );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
else if ( task.IsCanceled )
{
Debug.LogError ( "Account Creation Cancelled" );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
else if ( task.IsCompleted )
{
Debug.Log ( "Account Created Successfully" );
// Disable account controls and enable all other controls.
SetInteractableAccount ( false );
// Update the Database reference with the user id.
UpdateReference ();
}
} );
}
catch ( System.Exception ex )
{
Debug.LogException ( ex );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
}
So, you can sign in the user next time you want to authenticate the user:
public void SignIn ()
{
// A simple email validation.
if ( string.IsNullOrEmpty ( emailInputField.text ) )
{
Debug.LogError ( "Email is Empty!" );
return;
}
try
{
// Disable all controls.
SetInteractableAll ( false );
// Sign in user with the email and password.
FirebaseAuth.DefaultInstance.SignInWithEmailAndPasswordAsync ( emailInputField.text, passwordInputField.text ).ContinueWith ( (task ) =>
{
if ( task.IsFaulted )
{
Debug.LogError ( "Sign In Failed, here is why:" );
Debug.LogException ( task.Exception );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
else if ( task.IsCanceled )
{
Debug.LogError ( "Sign In Cancelled" );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
else if ( task.IsCompleted )
{
Debug.Log ( "Signed In Successfully" );
// Disable account controls and enable all other controls.
SetInteractableAccount ( false );
// Update the Database reference with the user id.
UpdateReference ();
}
} );
}
catch ( System.Exception ex )
{
Debug.LogException ( ex );
// Enable account controls and disable all other controls.
SetInteractableAccount ( true );
}
}
Now, you need to get a database reference to save the user data in the database, You can use the User ID to get a unique database reference path, for example, you can use users/User ID Here/saves database reference path:
string userId = FirebaseAuth.DefaultInstance.CurrentUser.UserId;
string path = string.Format ( "users/{0}/saves" );
DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference ( path );
Now, you can use Save Game Pro Firebase API to save and load data:
IEnumerator DoSave ()
{
Debug.Log ( "Saving..." );
// The "reference" is the database reference that we was created in the previous step
SaveGameFirebase firebase = new SaveGameFirebase ( reference );
// Send the request and wait for completion.
yield return StartCoroutine ( firebase.Save ( "helloWorld", "Hello World" ) );
// Check if request is succeed or failed.
if ( firebase.CurrentTask.IsFaulted )
{
Debug.LogError ( "Save Failed" );
Debug.LogException ( firebase.CurrentTask.Exception );
}
else
{
Debug.Log ( "Save Successful" );
}
}
And load it back:
IEnumerator DoLoad ()
{
Debug.Log ( "Loading..." );
// The "reference" is the database reference that we was created in the previous step
SaveGameFirebase firebase = new SaveGameFirebase ( reference );
// Send the request and wait for the completion.
yield return StartCoroutine ( firebase.Download ( "helloWorld" ) );
// Check if request is succeed or failed.
if ( firebase.CurrentTask.IsFaulted )
{
Debug.LogError ( "Load Failed" );
Debug.LogException ( firebase.CurrentTask.Exception );
}
else
{
// Check if data exists or not.
if ( firebase.DownloadTask.Result.Exists )
{
Debug.Log ( "Load Successful" );
// Load the data.
string helloWorld = firebase.Load<string> ();
}
else
{
Debug.LogError ( "Load Failed" );
Debug.LogError ( "The given identifier is not exists" );
}
}
}
Note: The Firebase SDK only works on Mobile platforms, so you should run the script in the Android build and not in the Editor.