Last active
May 3, 2022 05:20
-
-
Save fallenblood7080/4c55c103131a31adea1511d1250aabf0 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
/* | |
SDK - LootLockerSDK | |
Link - https://github.com/LootLocker/unity-sdk | |
*/ | |
using System; | |
using System.Collections; | |
using UnityEngine; | |
using TMPro; | |
using UnityEngine.UI; | |
using LootLocker.Requests; | |
public class LoginAndSignUpManager : MonoBehaviour | |
{ | |
#region Pages | |
public MainPageStuffs mainPage; | |
public LoginPageStuffs loginPage; | |
public SignUpStuffs signUpPage; | |
public GamePageStuffs gamePage; | |
#endregion | |
// Start is called before the first frame update | |
void Start() | |
{ | |
#region BtnInit | |
mainPage.MainPageLoginBtn.onClick.AddListener(OpenLoginPage); | |
mainPage.MainPageSignUpBtn.onClick.AddListener(OpenSignupPage); | |
signUpPage.SignUpBtn.onClick.AddListener(UserSignUp); | |
loginPage.LoginBtn.onClick.AddListener(LoginThroughBtn); | |
gamePage.SetNameBtn.onClick.AddListener(SetPlayerNickName); | |
gamePage.GetNameBtn.onClick.AddListener(GetPlayerName); | |
#endregion | |
#region CheckingGameSession | |
// To prevent showing login page everytime when player start the game | |
LootLockerSDKManager.CheckWhiteLabelSession(response => | |
{ | |
if (response) | |
{ | |
OpenGamePage(); | |
GetPlayerName(); | |
Debug.Log("session is valid, you can start a game session"); | |
} | |
else | |
{ | |
mainPage.MainPage.SetActive(true); | |
Debug.Log("session is NOT valid, we should show the login form"); | |
} | |
}); | |
#endregion | |
} | |
#region Signup and Login | |
void UserSignUp() | |
{ | |
string email = signUpPage.SignUpEmailInput.text; | |
string password = signUpPage.SignUpPasswordInput.text; | |
LootLockerSDKManager.WhiteLabelSignUp(email, password, (response) => | |
{ | |
if (!response.success) | |
{ | |
Debug.Log("error while creating user"); | |
return; | |
} | |
Debug.Log("user created successfully"); | |
UserLogin(email, password, true); | |
}); | |
} | |
void UserLogin(string email,string password , bool rememberMe) | |
{ | |
LootLockerSDKManager.WhiteLabelLogin(email, password, rememberMe, (response) => | |
{ | |
if (!response.success) | |
{ | |
Debug.Log("error while logging in"); | |
return; | |
} | |
LootLockerSDKManager.StartWhiteLabelSession((response) => | |
{ | |
if (!response.success) | |
{ | |
Debug.Log("error starting LootLocker session"); | |
return; | |
} | |
Debug.Log("session started successfully"); | |
OpenGamePage(); | |
}); | |
}); | |
} | |
void LoginThroughBtn() | |
{ | |
string email = loginPage.LoginEmailInput.text; | |
string password = loginPage.LoginPasswordInput.text; | |
bool remember = true; | |
UserLogin(email, password, remember); | |
} | |
#endregion | |
#region PlayerName | |
void SetPlayerName(string name) | |
{ | |
LootLockerSDKManager.SetPlayerName(name, (response) => | |
{ | |
if (response.success) | |
{ | |
Debug.Log("Successfully set player name"); | |
} | |
else | |
{ | |
Debug.Log("Error setting player name"); | |
} | |
}); | |
} | |
void SetPlayerNickName() | |
{ | |
SetPlayerName(gamePage.UsernameInput.text); | |
} | |
void GetPlayerName() | |
{ | |
string username = ""; | |
LootLockerSDKManager.GetPlayerName((response) => | |
{ | |
if (response.success) | |
{ | |
Debug.Log("Successfully retrieved player name: " + response.name); | |
username = response.name; | |
gamePage.Username.text = "Player Name: " + username; | |
} | |
else | |
{ | |
Debug.Log("Error getting player name"); | |
} | |
}); | |
} | |
#endregion | |
#region PageOpenerFuntions | |
#region OpenLoginPage | |
void OpenLoginPage() | |
{ | |
StartCoroutine(OnOpenLoginPage()); | |
} | |
IEnumerator OnOpenLoginPage() | |
{ | |
mainPage.Holder.SetTrigger("out"); | |
yield return new WaitForSeconds(.5f); | |
mainPage.MainPage.SetActive(false); | |
signUpPage.SignUpPage.SetActive(false); | |
loginPage.LoginPage.SetActive(true); | |
} | |
#endregion | |
#region OpenGamePage | |
void OpenGamePage() | |
{ | |
StartCoroutine(OnOpenGamePage()); | |
} | |
IEnumerator OnOpenGamePage() | |
{ | |
loginPage.holder.SetTrigger("out"); | |
yield return new WaitForSeconds(.9f); | |
mainPage.MainPage.SetActive(false); | |
loginPage.LoginPage.SetActive(false); | |
signUpPage.SignUpPage.SetActive(false); | |
gamePage.GamePage.SetActive(true); | |
} | |
#endregion | |
#region OpenSignUpPage | |
void OpenSignupPage() | |
{ | |
StartCoroutine(OpOpenSignupPage()); | |
} | |
IEnumerator OpOpenSignupPage() | |
{ | |
mainPage.Holder.SetTrigger("out"); | |
yield return new WaitForSeconds(.5f); | |
mainPage.MainPage.SetActive(false); | |
signUpPage.SignUpPage.SetActive(true); | |
} | |
#endregion | |
#endregion | |
} | |
#region Pages Class | |
[Serializable] | |
public class MainPageStuffs | |
{ | |
public GameObject MainPage; | |
public Button MainPageLoginBtn; | |
public Button MainPageSignUpBtn; | |
public Animator Holder; | |
} | |
[Serializable] | |
public class LoginPageStuffs | |
{ | |
public GameObject LoginPage; | |
public TMP_InputField LoginEmailInput; | |
public TMP_InputField LoginPasswordInput; | |
public Button LoginBtn; | |
public Animator holder; | |
} | |
[Serializable] | |
public class SignUpStuffs | |
{ | |
public GameObject SignUpPage; | |
public TMP_InputField SignUpEmailInput; | |
public TMP_InputField SignUpPasswordInput; | |
public Button SignUpBtn; | |
public Animator holder; | |
} | |
[Serializable] | |
public class GamePageStuffs | |
{ | |
public GameObject GamePage; | |
public TMP_Text Username; | |
public Button SetNameBtn; | |
public Button GetNameBtn; | |
public TMP_InputField UsernameInput; | |
} | |
#endregion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment