Created
January 11, 2022 07:49
-
-
Save gmYusuf/a4d73b23acde80155ceb6a838e5d2ded to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using FirebaseUnityWrapper.Auth; | |
using FirebaseUnityWrapper.Database; | |
using FirebaseUnityWrapper.Database.Event; | |
using FirebaseUnityWrapper.Storage; | |
using FirebaseUnityWrapper.Storage.StorageTask; | |
using GMSTasks; | |
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.IO; | |
using UnityEngine; | |
public class FirebaseTest : MonoBehaviour | |
{ | |
public Action<DataSnapshot> valueListener { get; set; } | |
// Start is called before the first frame update | |
void Start() | |
{ | |
} | |
//Firebase Database | |
public void WriteToDatabase() | |
{ | |
// Write a message to the database | |
FirebaseDatabase database = FirebaseDatabase.Instance; | |
DatabaseReference myRef = database.ReferenceForPath("message"); | |
var mes = "Hello, World!"; | |
myRef.SetValue(mes); | |
} | |
//Firebase Database | |
public void ReadFromDatabase() | |
{ | |
// Write a message to the database | |
FirebaseDatabase database = FirebaseDatabase.Instance; | |
DatabaseReference myRef = database.ReferenceForPath("message"); | |
myRef.AddValueEventListener(valueListener); | |
/* myEvent.AddValueEventListener((result) => | |
{ | |
Debug.Log("[HMSP:] FirebaseDatabase result " + result.GetValue()); | |
Debug.Log("[HMSP:] FirebaseDatabase result " + result.Key); | |
}); | |
Debug.Log("[HMSP:] FirebaseDatabase result " );*/ | |
} | |
//Firebase Storage | |
public void UploadFiles() | |
{ | |
byte[] data = { 1, 1, 1, 1 };//File.ReadAllBytes(Application.persistentDataPath + "/" + fileName); | |
FirebaseStorage storage = FirebaseStorage.Instance; | |
StorageReference storageRef = storage.Reference; | |
// Create a reference to "mountains.jpg" | |
StorageReference mountainsRef = storageRef.Child("test1.jpg"); | |
ITask<UploadTask.TaskSnapshot> uploadTask = mountainsRef.PutBytes(data); | |
uploadTask.AddOnSuccessListener((result) => | |
{ | |
Debug.Log("[HMSP:] FirebaseStorage result " ); | |
Debug.Log("[HMSP:] FirebaseStorage result " + result.gMetadata.Name); | |
}).AddOnFailureListener((exception) => | |
{ | |
Debug.Log("[HMS:] FirebaseStorage fail: " + exception.Message + "[HMS:]" + exception.InnerException + "[HMS:]" + exception.HResult + exception.ToString()); | |
}); | |
} | |
//Test | |
public void DownloadFilesInMemory() | |
{ | |
FirebaseStorage storage = FirebaseStorage.Instance; | |
// Create a storage reference from our app | |
StorageReference storageRef = storage.Reference; | |
StorageReference islandRef = storageRef.Child("image/gold.jpg"); | |
long ONE_MEGABYTE = 1024 * 1024; | |
ITask<byte[]> authTask = islandRef.GetBytes(ONE_MEGABYTE);//.AddOnSuccessListener(OnSuccessListener).AddOnFailureListener(OnFailureListener); | |
authTask.AddOnSuccessListener((result) => | |
{ | |
Debug.Log("[HMSP:] FirebaseStorage result " + result.Length); | |
}).AddOnFailureListener((exception) => | |
{ | |
Debug.Log("[HMS:] FirebaseStorage fail: " + exception.Message + "[HMS:]" + exception.InnerException + "[HMS:]" + exception.HResult + exception.ToString()); | |
}); | |
} | |
void OnSuccessListener(byte[] result) | |
{ | |
Debug.Log("[HMSP:] StorageReference OnSuccessListener " + result.Length); | |
} | |
void OnFailureListener(Exception exception) | |
{ | |
Debug.Log("[HMSP:] StorageReference OnFailureListener " + exception.Message); | |
} | |
//Test | |
public void CreateUserWithEmailAndPassword() | |
{ | |
FirebaseAuth firebaseAuth; | |
firebaseAuth = FirebaseAuth.Instance; | |
ITask<AuthResult> authTask = firebaseAuth.CreateUserWithEmailAndPassword("yusufaltungolaaxx@hotmail.com", "m123416"); | |
authTask.AddOnSuccessListener((result) => | |
{ | |
Debug.Log("[HMSP:] FirebaseAuth archiveSummary " + result.User.Anonymous); | |
}).AddOnFailureListener((exception) => | |
{ | |
Debug.Log("[HMS:] FirebaseAuth fail: " + exception.Message + "[HMS:]" + exception.InnerException + "[HMS:]" + exception.HResult + exception.ToString()); | |
}); | |
} | |
public void SignInAnonymously() | |
{ | |
FirebaseAuth firebaseAuth; | |
firebaseAuth = FirebaseAuth.Instance; | |
ITask<AuthResult> authTask = firebaseAuth.SignInAnonymously(); | |
authTask.AddOnSuccessListener((result) => | |
{ | |
Debug.Log("[HMSP:] FirebaseAuth archiveSummary " + result.User.Anonymous); | |
}).AddOnFailureListener((exception) => | |
{ | |
Debug.Log("[HMS:] FirebaseAuth fail: " + exception.Message + "[HMS:]" + exception.InnerException + "[HMS:]" + exception.HResult + exception.ToString()); | |
}); | |
} | |
// Update is called once per frame | |
void Update() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment