Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections;
- using Facebook.Unity;
- using System.Collections.Generic;
- using System;
- using System.Threading.Tasks;
- public class Controller : MonoBehaviour {
- public Text txtDebug;
- //face
- void Awake(){
- FB.Init(OnInitComplete, OnHideUnity);
- }
- void OnInitComplete()
- {
- DebugLog ("Init Face Done!");
- if (FB.IsLoggedIn) {
- strToken= AccessToken.CurrentAccessToken.TokenString;
- DebugLog ("Face Logged in!"+"; token: "+strToken);
- } else
- FBLogin ();
- }
- void OnHideUnity(bool isGameShown)
- {
- if (!isGameShown) {
- Time.timeScale = 0;
- } else {
- Time.timeScale =1;
- }
- }
- void FBLogin(){
- FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, LoginHandleResult);
- }
- string strToken;
- void LoginHandleResult(IResult result){
- DebugLog (result.ToString ());
- if (FB.IsLoggedIn) {
- strToken= AccessToken.CurrentAccessToken.TokenString;
- DebugLog ("Login handle result!"+"; token: "+strToken);
- }
- }
- //face
- //firebase
- Firebase.Auth.FirebaseAuth auth;
- Firebase.Auth.FirebaseUser user;
- Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
- // When the app starts, check to make sure that we have
- // the required dependencies to use Firebase, and if not,
- // add them if possible.
- void Start() {
- dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
- if (dependencyStatus != Firebase.DependencyStatus.Available) {
- Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
- dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
- if (dependencyStatus == Firebase.DependencyStatus.Available) {
- InitializeFirebase();
- } else {
- // This should never happen if we're only using Firebase Analytics.
- // It does not rely on any external dependencies.
- Debug.LogError(
- "Could not resolve all Firebase dependencies: " + dependencyStatus);
- }
- });
- } else {
- InitializeFirebase();
- }
- }
- // Handle initialization of the necessary firebase modules:
- void InitializeFirebase() {
- DebugLog("Setting up Firebase Auth");
- auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
- auth.StateChanged += AuthStateChanged;
- }
- // Exit if escape (or back, on mobile) is pressed.
- void Update() {
- if (Input.GetKeyDown(KeyCode.Escape)) {
- Application.Quit();
- }
- }
- void OnDestroy() {
- auth.StateChanged -= AuthStateChanged;
- auth = null;
- }
- // Output text to the debug log text field, as well as the console.
- public void DebugLog(string s) {
- Debug.Log(s);
- txtDebug.text += s + "\n";
- }
- // Track state changes of the auth object.
- void AuthStateChanged(object sender, System.EventArgs eventArgs) {
- if (auth.CurrentUser != user) {
- if (user == null && auth.CurrentUser != null) {
- DebugLog("Signed in " + auth.CurrentUser.DisplayName);
- } else if (user != null && auth.CurrentUser == null) {
- DebugLog("Signed out " + user.DisplayName);
- }
- user = auth.CurrentUser;
- //====================
- //=====ERROR HERE=====
- //====================
- Debug.Log (User.Token ());
- }
- }
- // This is functionally equivalent to the Signin() function. However, it
- // illustrates the use of Credentials, which can be aquired from many
- // different sources of authentication.
- public void SigninWithCredential() {
- DebugLog("Face login click!"+";token "+ strToken);
- // DisableUI();
- // Firebase.Auth.Credential cred = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
- // auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSigninResult);
- Firebase.Auth.Credential cred = Firebase.Auth.FacebookAuthProvider.GetCredential(strToken);
- auth.SignInWithCredentialAsync (cred).ContinueWith (HandleSigninResult);
- }
- void HandleSigninResult(Task<Firebase.Auth.FirebaseUser> authTask) {
- if (authTask.IsCanceled) {
- DebugLog("SignIn canceled.");
- } else if (authTask.IsFaulted) {
- DebugLog("Login encountered an error.");
- DebugLog(authTask.Exception.ToString());
- } else if (authTask.IsCompleted) {
- DebugLog("Login completed.");
- DebugLog("Signing out.");
- DebugLog ("token"+user.RefreshToken+"; userid"+user.UserId+"; username"+user.DisplayName);
- user.TokenAsync (false).ContinueWith(HandleTokenFirebase);
- auth.SignOut();
- }
- }
- void HandleTokenFirebase(Task<string> task){
- DebugLog("result: "+task.Result+"; Lenght "+task.Result.Length);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement