123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using GooglePlayGames;
- using GooglePlayGames.BasicApi;
- using UnityEngine.SocialPlatforms;
- using System.Threading;
- using UnityEngine.UI;
- using Firebase.Firestore;
- using Firebase.Extensions;
- using System;
- using UnityEngine.Networking;
- using System.Text.RegularExpressions;
- using TMPro;
- using UnityEngine.SceneManagement;
- public class gplayAuth : MonoBehaviour{
- public TMP_Text gplayText;
- public TMP_Text firebaseStatText;
- public string AuthCode;
- public static string userID;
- bool IsConnected;
- public TMP_InputField saveName, coins, item1, item2, item3 ;
- public TMP_Text saveStatText;
- public static string userNameCloud;
- public TMP_Text saveNameTxt , coinsTxt, item1Txt, item2Txt, item3Txt;
- #if UNITY_ANDROID
- void Start() {
- IsConnected =false;
- PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder().RequestServerAuthCode(false)
- .RequestEmail().RequestIdToken().EnableSavedGames().Build();
- PlayGamesPlatform.InitializeInstance(config);
- PlayGamesPlatform.DebugLogEnabled = true;
- PlayGamesPlatform.Activate();
- //GPGSLogin();
- }
-
- public void GPGSLogin(){
- PlayGamesPlatform.Instance.Authenticate((success) => {
- if(success == true){
- //logged into Google Play Games
- gplayText.text = "G-Play Connected";
- Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
- if(task.Result == Firebase.DependencyStatus.Available){
- //no dependency issue with firebase, continue to login
- ConnectToFirebase();
-
- }
- else{
- //error with firebase Dependecies plugin
- firebaseStatText.text = "Dependency Error";
- }
- }
- );
- }
- else{
- Debug.LogError("Gplay failed");
- }
- }
- );
- }
- public void SaveData(){
- //checking if connected to firebase
- if(IsConnected){
- FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
- string playerName = saveName.text;
- int playerCoin = int.Parse(coins.text);
- List<string> plyaerInventoryItems = new List<string>();
- plyaerInventoryItems.Add(item1.text);
- plyaerInventoryItems.Add(item2.text);
- plyaerInventoryItems.Add(item3.text);
- Dictionary<string, object> saveValues = new Dictionary<string, object>{
- {"playerName" , playerName},
- {"playerCoin" , playerCoin},
- {"playerInventory" , plyaerInventoryItems},
- };
- DocumentReference docRef = db.Collection("PlayerData").Document(userID);
- docRef.SetAsync(saveValues).ContinueWithOnMainThread(task => {
- if(task.IsCompleted){
- saveStatText.text = "Save Completed Firestore";
- }
- else{
- saveStatText.text = "Failed to save data to firestore";
- }
- });
- }else{
- //firebase_Not_Connected_error
- Debug.LogError("firebase Not connected to save");
- }
- }
- public void LoadData(){
- if(IsConnected){
- FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
- DocumentReference docRef = db.Collection("PlayerData").Document(userID);
- docRef.GetSnapshotAsync().ContinueWithOnMainThread(task => {
- DocumentSnapshot snapshot = task.Result;
- if(snapshot.Exists){
- //load data
- Dictionary<string,object> dic = snapshot.ToDictionary(ServerTimestampBehavior.Estimate);
- Debug.Log("Reading data");
- foreach(KeyValuePair<string,object> item in dic){
- Debug.Log(item.Key + " : " +item.Value.ToString());
- }
- saveNameTxt.text = snapshot.GetValue<string>("playerName");
- coinsTxt.text = snapshot.GetValue<int>("playerCoin").ToString();
- List<string> inventoryList = snapshot.GetValue<List<string>>("playerInventory");
- item1Txt.text = inventoryList[0];
- item2Txt.text = inventoryList[1];
- item3Txt.text = inventoryList[2];
- }else{
- //show error previous data doesnt exists to load
- Debug.Log("No previous data to load");
- }
- });
- }
- }
- public TMP_InputField registerEmail, registerPassword;
- public void RegisterUser(){
- Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- auth.CreateUserWithEmailAndPasswordAsync(registerEmail.text, registerPassword.text).ContinueWith(task => {
- if (task.IsCanceled) {
- Debug.LogError("CreateUserWithEmailAndPasswordAsync was canceled.");
- return;
- }
- if (task.IsFaulted) {
- Debug.LogError("CreateUserWithEmailAndPasswordAsync encountered an error: " + task.Exception);
- return;
- }
- // Firebase user has been created.
- Firebase.Auth.AuthResult result = task.Result;
- Debug.LogFormat("Firebase user created successfully: {0} ({1})",
-
- result.User.DisplayName, result.User.UserId);
- OnAuthSuccess(result.User);
- userNameCloud = registerEmail.text;
- });
- }
- public TMP_InputField loginEmail, loginPassword;
- public void LoginUser(){
- Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- auth.SignInWithEmailAndPasswordAsync(loginEmail.text, loginPassword.text).ContinueWith(task => {
- if (task.IsCanceled) {
- Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
- return;
- }
- if (task.IsFaulted) {
- Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
- return;
- }
- Firebase.Auth.AuthResult result = task.Result;
- Debug.LogFormat("User signed in successfully: {0} ({1})",
- result.User.DisplayName, result.User.UserId);
- OnAuthSuccess(result.User);
- userNameCloud = loginEmail.text;
-
- });
- }
- void ConnectToFirebase(){
- AuthCode=PlayGamesPlatform.Instance.GetServerAuthCode();
- Debug.Log("Gplay auth " + AuthCode);
- Firebase.Auth.FirebaseAuth FBAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- Firebase.Auth.Credential FBCred = Firebase.Auth.PlayGamesAuthProvider.GetCredential(AuthCode);
- FBAuth.SignInWithCredentialAsync(FBCred).ContinueWithOnMainThread(task => {
- if(task.IsCanceled){
- firebaseStatText.text = "sign in cancelled";
- }
- if(task.IsFaulted){
- firebaseStatText.text = "Error:"+task.Result;
- }
- Firebase.Auth.FirebaseUser user = FBAuth.CurrentUser;
- if(user != null){
- // userID = user.UserId;
- // firebaseStatText.text = "Signed in As :"+ user.DisplayName;
- // IsConnected = true;
- OnAuthSuccess(user);
- userNameCloud = user.DisplayName;
- }
- else{
- //error getting username
- }
- });
-
- // PlayGamesPlatform.Instance.RequestServerSideAccess(true , code => {
- // AuthCode = code;
- // Firebase.Auth.FirebaseAuth FBAuth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- // Firebase.Auth.Credential FBCred = Firebase.Auth.PlayGamesAuthProvider.GetCredential(AuthCode);
- // FBAuth.SignInWithCredentialAsync(FBCred).ContinueWithOnMainThread(task => {
- // if(task.IsCanceled){
- // firebaseStatText.text = "sign in cancelled";
- // }
- // if(task.IsFaulted){
- // firebaseStatText.text = "Error:"+task.Result;
- // }
- // Firebase.Auth.FirebaseUser user = FBAuth.CurrentUser;
- // if(user != null){
- // firebaseStatText.text = "Signed in As :"+ user.DisplayName;
- // }
- // else{
- // //error getting username
- // }
- // });
- // });
- }
- public void OnAuthSuccess(Firebase.Auth.FirebaseUser user){
- userID = user.UserId;
- firebaseStatText.text = "Signed in As :"+ user.DisplayName;
- IsConnected = true;
- //load game scene
- SceneManager.LoadScene("MenuScene");
- }
- #endif
- }
|