https://firebase.google.com/?hl=ko
Firebase | Google’s Mobile and Web App Development Platform
개발자가 사용자가 좋아할 만한 앱과 게임을 빌드하도록 지원하는 Google의 모바일 및 웹 앱 개발 플랫폼인 Firebase에 대해 알아보세요.
firebase.google.com
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UserInfo
{
public string userId;
public int score;
//생성자
public UserInfo(string userId, int score)
{
this.userId = userId;
this.score = score;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameInfo
{
public List<UserInfo> userinfos = new List<UserInfo>();
}
using Firebase.Database;
using Firebase.Extensions;
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class GameMain : MonoBehaviour
{
DatabaseReference mDatabaseRef;
void Start()
{
//Debug.Log($"Firebase.FirebaseApp.DefaultInstance: {Firebase.FirebaseApp.DefaultInstance}");
// Get the root reference location of the database.
mDatabaseRef = FirebaseDatabase.DefaultInstance.RootReference;
// List<UserInfo> userInfos = new List<UserInfo>();
//userInfos.Add(new UserInfo("hong",100));
//userInfos.Add(new UserInfo("hong2", 200));
//userInfos.Add(new UserInfo("hong3", 300));
//GameInfo gameInfo = new GameInfo(); //저장 객체 만들기
//gameInfo.userinfos = userInfos;
//string json = JsonConvert.SerializeObject(gameInfo); //직렬화
//mDatabaseRef.Child("gameInfo").SetRawJsonValueAsync(json);
//가져오기
FirebaseDatabase.DefaultInstance
.GetReference("gameInfo")
.GetValueAsync().ContinueWithOnMainThread(task => {
if (task.IsFaulted)
{
// Handle the error...
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
// Do something with snapshot...
string jsonGameInfo = snapshot.GetRawJsonValue();
var gameInfo = JsonConvert.DeserializeObject<GameInfo>(jsonGameInfo);
Debug.Log($"gameInfo : {gameInfo}");
//score로 정렬
//gameInfo.userinfos.Sort((x, y) => {
// if (x.score == y.score) return 0;
// else if (x.score > y.score) return -1;
// else if (x.score < y.score) return 1;
// else return x.score.CompareTo(y.score);
//});
gameInfo.userinfos.OrderBy(x => x.score);
foreach (var userInfo in gameInfo.userinfos)
{
Debug.Log($"=> {userInfo.userId}, {userInfo.score}");
}
UserInfo findinfo = gameInfo.userinfos.Find(x => x.userId == "jjang");
if (findinfo !=null)
{
///있으면 수정
findinfo.score = 1500;
}
else
{
gameInfo.userinfos.Add(new UserInfo("jjang", 1300));
Debug.Log("유저 추가");
}
//SetRawJsonValueAsync
string json = JsonConvert.SerializeObject(gameInfo);//직렬화
mDatabaseRef.Child("gameInfo").SetRawJsonValueAsync(json);//저장
}
});
}
}
'게임프로젝트 > 쿠키런 모작' 카테고리의 다른 글
풀매니저 대신 타일맵 제너레이터 (0) | 2024.04.08 |
---|---|
베스트 스코어 저장하기 (0) | 2024.04.08 |
R&D 타일맵 랜덤 무한 생성(5) 오브젝트 풀링 오류 수정 끝! (0) | 2024.03.30 |
R&D 타일맵 랜덤 무한 생성(4) 오브젝트 풀링 수정중 (0) | 2024.03.30 |
R&D 타일맵 랜덤 무한 생성(3) 오브젝트 풀링 수정중 (1) | 2024.03.30 |