Last active
July 10, 2018 02:39
-
-
Save yuw-unknown/49f5ab17e16ffe46b477e1adf6299591 to your computer and use it in GitHub Desktop.
FirebaseのRealtimeDatabaseをUnityからTwitter認証を用いてデータ取得するSample vol2.
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 System; | |
using System.Linq; | |
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using TwitterKit.Unity; | |
using Firebase; | |
using Firebase.Database; | |
using Firebase.Unity.Editor; | |
public class PUFirebaseTwitterLogin : MonoBehaviour { | |
private string _AccessToken; | |
private string _Secret; | |
private string _UserName; | |
// Firebase | |
private DatabaseReference _FirebaseDB; | |
private Firebase.Auth.FirebaseUser _FirebaseUser; | |
void Start () { | |
Twitter.Init(); | |
this.TwitterAuth(); | |
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("Firebase Realtime Database URL"); | |
_FirebaseDB = FirebaseDatabase.DefaultInstance.GetReference("UnitySample/Ranking"); | |
} | |
public void FirebaseLogin() { | |
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; | |
Firebase.Auth.Credential credential = Firebase.Auth.TwitterAuthProvider.GetCredential(_AccessToken, _Secret); | |
auth.SignInWithCredentialAsync(credential).ContinueWith(task => { | |
if (task.IsCanceled) { | |
Debug.LogError("[Info ] : SignInWithCredentialAsync canceled."); | |
return; | |
} | |
if (task.IsFaulted) { | |
Debug.LogError("[Error ] : SignInWithCredentialAsync fatal. an error: " + task.Exception); | |
return; | |
} | |
_FirebaseUser = task.Result; | |
this.WriteNewScore(100); | |
}); | |
} | |
public void TwitterAuth() { | |
Debug.Log("[Info] : start login"); | |
TwitterSession session = Twitter.Session; | |
if (session == null) { | |
Twitter.LogIn(TwitterLoginComplete, TwitterLoginFailure); | |
} else { | |
TwitterLoginComplete(session); | |
} | |
} | |
public void TwitterLoginComplete(TwitterSession session) { | |
Debug.Log("[Info] : Login success. " + session.authToken); | |
_AccessToken = session.authToken.token; | |
_Secret = session.authToken.secret; | |
_UserName = session.userName; | |
this.FirebaseLogin(); | |
} | |
public void TwitterLoginFailure(ApiError error) { | |
Debug.Log("[Error ] : Login faild code =" + error.code + " msg =" + error.message); | |
} | |
void GetRankingData() { | |
_FirebaseDB.OrderByChild("score").LimitToLast(1).GetValueAsync().ContinueWith(task => { | |
if (task.IsFaulted) { | |
Debug.Log("[ERROR] get ranking sort"); | |
} else if (task.IsCompleted) { | |
Debug.Log("[INFO] get ranking success."); | |
DataSnapshot snapshot = task.Result; | |
IEnumerator<DataSnapshot> en = snapshot.Children.GetEnumerator(); | |
int rank = 0; | |
string json = snapshot.GetRawJsonValue(); | |
while (en.MoveNext()) { | |
DataSnapshot data = en.Current; | |
string name = (string)data.Child("name").Value; | |
int score = (int)(long)data.Child("score").Value; | |
Debug.Log("name : " + name + " score : " + score); | |
} | |
} | |
}); | |
} | |
private void WriteNewScore(int score) { | |
string key = FirebaseDatabase.DefaultInstance.GetReference("UnitySample").Child("Ranking").Push().Key; | |
Debug.Log("key is : " + key); | |
Dictionary<string, object> itemMap = new Dictionary<string, object>(); | |
itemMap.Add("name", _FirebaseUser.DisplayName); | |
itemMap.Add("id", _UserName); | |
itemMap.Add("score", score); | |
itemMap.Add("updatedate", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")); | |
Dictionary<string, object> map = new Dictionary<string, object>(); | |
map.Add(_FirebaseUser.UserId, itemMap); | |
_FirebaseDB.UpdateChildrenAsync(map); | |
this.GetRankingData(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment