Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.47 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3. using System.Collections;
  4. using Facebook.Unity;
  5. using System.Collections.Generic;
  6. using System;
  7. using System.Threading.Tasks;
  8.  
  9. public class Controller : MonoBehaviour {
  10.  
  11. public Text txtDebug;
  12.  
  13. //face
  14. void Awake(){
  15. FB.Init(OnInitComplete, OnHideUnity);
  16. }
  17.  
  18. void OnInitComplete()
  19. {
  20. DebugLog ("Init Face Done!");
  21. if (FB.IsLoggedIn) {
  22. strToken= AccessToken.CurrentAccessToken.TokenString;
  23. DebugLog ("Face Logged in!"+"; token: "+strToken);
  24. } else
  25. FBLogin ();
  26. }
  27.  
  28. void OnHideUnity(bool isGameShown)
  29. {
  30. if (!isGameShown) {
  31. Time.timeScale = 0;
  32. } else {
  33. Time.timeScale =1;
  34. }
  35. }
  36.  
  37. void FBLogin(){
  38. FB.LogInWithReadPermissions(new List<string>() { "public_profile", "email", "user_friends" }, LoginHandleResult);
  39. }
  40.  
  41. string strToken;
  42. void LoginHandleResult(IResult result){
  43. DebugLog (result.ToString ());
  44. if (FB.IsLoggedIn) {
  45. strToken= AccessToken.CurrentAccessToken.TokenString;
  46. DebugLog ("Login handle result!"+"; token: "+strToken);
  47. }
  48. }
  49. //face
  50.  
  51. //firebase
  52.  
  53. Firebase.Auth.FirebaseAuth auth;
  54. Firebase.Auth.FirebaseUser user;
  55.  
  56. Firebase.DependencyStatus dependencyStatus = Firebase.DependencyStatus.UnavailableOther;
  57.  
  58. // When the app starts, check to make sure that we have
  59. // the required dependencies to use Firebase, and if not,
  60. // add them if possible.
  61. void Start() {
  62. dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
  63. if (dependencyStatus != Firebase.DependencyStatus.Available) {
  64. Firebase.FirebaseApp.FixDependenciesAsync().ContinueWith(task => {
  65. dependencyStatus = Firebase.FirebaseApp.CheckDependencies();
  66. if (dependencyStatus == Firebase.DependencyStatus.Available) {
  67. InitializeFirebase();
  68. } else {
  69. // This should never happen if we're only using Firebase Analytics.
  70. // It does not rely on any external dependencies.
  71. Debug.LogError(
  72. "Could not resolve all Firebase dependencies: " + dependencyStatus);
  73. }
  74. });
  75. } else {
  76. InitializeFirebase();
  77. }
  78. }
  79.  
  80. // Handle initialization of the necessary firebase modules:
  81. void InitializeFirebase() {
  82. DebugLog("Setting up Firebase Auth");
  83. auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
  84. auth.StateChanged += AuthStateChanged;
  85. }
  86.  
  87. // Exit if escape (or back, on mobile) is pressed.
  88. void Update() {
  89. if (Input.GetKeyDown(KeyCode.Escape)) {
  90. Application.Quit();
  91. }
  92. }
  93.  
  94. void OnDestroy() {
  95. auth.StateChanged -= AuthStateChanged;
  96. auth = null;
  97. }
  98.  
  99. // Output text to the debug log text field, as well as the console.
  100. public void DebugLog(string s) {
  101. Debug.Log(s);
  102. txtDebug.text += s + "\n";
  103. }
  104.  
  105. // Track state changes of the auth object.
  106. void AuthStateChanged(object sender, System.EventArgs eventArgs) {
  107. if (auth.CurrentUser != user) {
  108. if (user == null && auth.CurrentUser != null) {
  109. DebugLog("Signed in " + auth.CurrentUser.DisplayName);
  110. } else if (user != null && auth.CurrentUser == null) {
  111. DebugLog("Signed out " + user.DisplayName);
  112. }
  113. user = auth.CurrentUser;
  114.  
  115. //====================
  116. //=====ERROR HERE=====
  117. //====================
  118. Debug.Log (User.Token ());
  119. }
  120. }
  121.  
  122. // This is functionally equivalent to the Signin() function. However, it
  123. // illustrates the use of Credentials, which can be aquired from many
  124. // different sources of authentication.
  125. public void SigninWithCredential() {
  126. DebugLog("Face login click!"+";token "+ strToken);
  127. // DisableUI();
  128. // Firebase.Auth.Credential cred = Firebase.Auth.EmailAuthProvider.GetCredential(email, password);
  129. // auth.SignInWithCredentialAsync(cred).ContinueWith(HandleSigninResult);
  130.  
  131. Firebase.Auth.Credential cred = Firebase.Auth.FacebookAuthProvider.GetCredential(strToken);
  132. auth.SignInWithCredentialAsync (cred).ContinueWith (HandleSigninResult);
  133. }
  134.  
  135. void HandleSigninResult(Task<Firebase.Auth.FirebaseUser> authTask) {
  136. if (authTask.IsCanceled) {
  137. DebugLog("SignIn canceled.");
  138. } else if (authTask.IsFaulted) {
  139. DebugLog("Login encountered an error.");
  140. DebugLog(authTask.Exception.ToString());
  141. } else if (authTask.IsCompleted) {
  142. DebugLog("Login completed.");
  143. DebugLog("Signing out.");
  144. DebugLog ("token"+user.RefreshToken+"; userid"+user.UserId+"; username"+user.DisplayName);
  145. user.TokenAsync (false).ContinueWith(HandleTokenFirebase);
  146. auth.SignOut();
  147. }
  148. }
  149.  
  150. void HandleTokenFirebase(Task<string> task){
  151. DebugLog("result: "+task.Result+"; Lenght "+task.Result.Length);
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement