728x90
https://firebase.google.com/docs?authuser=0&hl=ko
https://firebase.google.com/docs/database/unity/start?authuser=0&hl=ko
using System;
using System.Collections;
using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;
using Firebase;
using Firebase.Database;
using UnityEngine.UI;
public class Test_DB : MonoBehaviour
{
private DatabaseReference m_DB;
[SerializeField]
private Button m_Button_NewUser;
[SerializeField] private Button m_Button_LevelUp;
[SerializeField] private Button m_Button_LevelDown;
[SerializeField] private Button m_Button_DeleteUser;
private User m_User;
private void Awake()
{
m_Button_NewUser.onClick.AddListener(OnNewUser);
m_Button_LevelUp.onClick.AddListener(OnLevelUp);
m_Button_LevelDown.onClick.AddListener(OnLevelDown);
m_Button_DeleteUser.onClick.AddListener(OnDeleteUser);
}
// Start is called before the first frame update
void Start()
{
m_DB = FirebaseDatabase.DefaultInstance.RootReference;
// 계정 정보 로드
m_DB.Child("users").Child("0").GetValueAsync().ContinueWith(task =>
{
if (task.IsCompleted)
{
if (task.Result.Value == null)
OnNewUser(); // 없으면 신규계정 생성
else
m_User = JsonUtility.FromJson<User>(task.Result.GetRawJsonValue());
}
});
}
private void OnApplicationQuit()
{
m_DB.OnDisconnect();
}
// Update is called once per frame
void Update()
{
}
private void OnNewUser()
{
Write_NewUser("0", "네이브", 1);
}
private void OnDeleteUser()
{
m_DB.Child("users").Child("0").RemoveValueAsync();
}
private void OnLevelUp()
{
m_User.SetLevel(m_User.Level + 1);
SetValue_Level("0", m_User.Level);
}
private void OnLevelDown()
{
m_User.SetLevel(m_User.Level - 1);
SetValue_Level("0", m_User.Level);
}
void Write_NewUser(string userId, string nickname, int level)
{
var user = new User(nickname, level);
var json = JsonUtility.ToJson(user);
m_DB.Child("users").Child(userId).SetRawJsonValueAsync(json).ContinueWith(task =>
{
if (task.IsCompleted)
m_User = new User(nickname, level);
});
}
void SetValue_Level(string userId, int level)
{
m_DB.Child("users").Child(userId).Child("Level").SetValueAsync(level);
}
}
public class User
{
public string Nickname;
public int Level;
public User(string nickname, int level)
{
this.Nickname = nickname;
this.Level = level;
}
public void SetLevel(int level)
{
Level = level;
}
}
https://firebase.google.com/docs/crashlytics/get-started?authuser=2&platform=android
728x90
'프로그래밍 > Unity' 카테고리의 다른 글
유니티 콜백 팝업 예제 (0) | 2022.07.18 |
---|---|
어드레서블 (Addressable) (0) | 2022.07.14 |
UGUI Advanced (Dynamic) ScrollView (0) | 2022.06.18 |
유니티쨩 URP 를 써보자 (0) | 2022.03.21 |
Unity - Obfuscator 메뉴얼 (0) | 2021.12.30 |
댓글