Last active
September 28, 2020 13:25
-
-
Save melikeeroglu/c37bd86adb6ec55df9d0042d0522c772 to your computer and use it in GitHub Desktop.
Unity account kit
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 HuaweiMobileServices.Id; | |
using HuaweiMobileServices.Utils; | |
using UnityEngine; | |
using UnityEngine.UI; | |
using HmsPlugin; | |
using UnityEngine.SceneManagement; | |
public class AccountSignIn : MonoBehaviour | |
{ | |
private const string NOT_LOGGED_IN = "No user logged in"; | |
private const string LOGGED_IN = "{0} is logged in"; | |
private const string LOGIN_ERROR = "Error or cancelled login"; | |
public Text loggedInUser; | |
private AccountManager accountManager; | |
// Start is called before the first frame update | |
void Start() | |
{ | |
loggedInUser = GameObject.Find("LoggedUserText").GetComponent<Text>(); | |
loggedInUser.text = NOT_LOGGED_IN; | |
accountManager = AccountManager.GetInstance(); | |
accountManager.OnSignInSuccess = OnLoginSuccess; | |
accountManager.OnSignInFailed = OnLoginFailure; | |
} | |
public void LogIn() | |
{ | |
print("Login"); | |
accountManager.SignIn(); | |
} | |
public void LogOut() | |
{ | |
print("Logout"); | |
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); | |
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"); | |
if (unityActivity != null) | |
{ | |
AndroidJavaClass toastClass = new AndroidJavaClass("android.widget.Toast"); | |
unityActivity.Call("runOnUiThread", new AndroidJavaRunnable(() => | |
{ | |
AndroidJavaObject toastObject = toastClass.CallStatic<AndroidJavaObject>("makeText", unityActivity, "asd", 0); | |
toastObject.Call("show"); | |
})); | |
} | |
accountManager.SignOut(); | |
loggedInUser.text = NOT_LOGGED_IN; | |
Debug.Log("Logout"); | |
} | |
public void OnLoginSuccess(AuthHuaweiId authHuaweiId) | |
{ | |
print(authHuaweiId); | |
loggedInUser.text = string.Format(LOGGED_IN, authHuaweiId.DisplayName); | |
} | |
public void OnLoginFailure(HMSException error) | |
{ | |
print(error); | |
loggedInUser.text = LOGIN_ERROR; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment