๐ Unity ํ๋ก์ ํธ ์์ฑ
1. ์๋ก์ด ํ๋ก์ ํธ ์์ฑํ [ FIle ] - [ Build Settings... ] ์์ Platform์ Android ์ ํ ํ Switch Platform
2. [ FIle ] - [ Build Settings... ] - [ Player Settings... ] - [ Other Settings ] - [ Identification ] ์ Package Name ์ค์
(Package Name์ ํ์์ "com.CompanyName.ProductName" ์ด๋ฉฐ, ์ซ์๋ก ์์ X, ์ํ๋ฒณ๊ณผ "_"๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.)
๐ Firebase ํ๋ก์ ํธ ์ถ๊ฐ
1. ์ฐ์ Firebase ์ฌ์ดํธ์ ์ ์ํ๋ค. (https://firebase.google.com/?hl=ko)
Firebase | Google’s Mobile and Web App Development Platform
๊ฐ๋ฐ์๊ฐ ์ฌ์ฉ์๊ฐ ์ข์ํ ๋งํ ์ฑ๊ณผ ๊ฒ์์ ๋น๋ํ๋๋ก ์ง์ํ๋ Google์ ๋ชจ๋ฐ์ผ ๋ฐ ์น ์ฑ ๊ฐ๋ฐ ํ๋ซํผ์ธ Firebase์ ๋ํด ์์๋ณด์ธ์.
firebase.google.com
2. [ ์์ํ๊ธฐ ] - [ ํ๋ก์ ํธ ์ถ๊ฐ ]
3. [ ํ๋ก์ ํธ ์ด๋ฆ ] ์
๋ ฅ
4. [ ์ด ํ๋ก์ ํธ์์ Google ์ ๋๋ฆฌํฑ์ค ์ฌ์ฉ ์ค์ ] ์ฒดํฌ - [ ๊ณ์ ]
5. ๊ณ์ ์ด ์์ ๊ฒฝ์ฐ Default Account for Firebase๋ฅผ ์ ํํ๋ค. (์ ๊ณ์ ์ ๋ง๋๋ ๊ฒ ์ถ์ฒ)
6. FIrebase์ Unity ์ฑ์ ์ถ๊ฐํ๋ค.
7. Unity ํ๋ก์ ํธ์ Package Name์ผ๋ก ์ฑ์ ๋ฑ๋กํ๋ค.
8. ๊ตฌ์ฑ ํ์ผ ๋ค์ด๋ก๋ ํ Unity ํ๋ก์ ํธ์ Assets ํด๋์ ๋ฃ๋๋ค.
9. google-services.json ํ์ผ์ "project_info" ๋ถ๋ถ์ "firebase_url" : "Firebase์ url"์ ์ถ๊ฐ๋ก ์
๋ ฅํด์ค๋ค.
10. Firebase์ SDK๋ฅผ ๋ค์ด๋ก๋ํ ๋ค ํธ๋ฆฌํ ์์น์ ์์ถ์ ํผ๋ค.
11. Unity Project ์ฐฝ์ [ Assets ] - [ ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ] - [ import Package ] - [ Custom Package... ]
12 . ์์ถ ํด์ ํ ํด๋์์ FirebaseDatabase.unitypackage ๋ฅผ ๊ฐ์ ธ์จ๋ค import ํ๋ค.
13. Unity๋ก ๋์์์ [ Enable ] ํด๋ฆญ (์ค๋ฅ๋ IOS ์ฑ ๋ฑ๋ก์ ์ ํด์ ๋ฐ์ํ๋๋ฐ ์ ํ ์๊ด X)
๐ ๋ฐ์ดํฐ ์ ์ฅ
1. ์ฐ์ [ ๋น๋ ] - [ Realtime Database ] - [ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง๋ค๊ธฐ ] ๋ฅผ ํตํด Realtime Database๋ฅผ ๋ง๋ ๋ค.
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์์น๋ฅผ ์ ํํ๋ค.
3. ํ
์ดํฐ๋ฒ ์ด์ค ๋ณด์ ๊ท์น์ ์์ฑํ ๋ค [ ์ฌ์ฉ ์ค์ ] ํด๋ฆญ
4. ๋ฐ์ดํฐ ์ ์ฅ Test๋ฅผ ์ํด Unity๋ก ๋์์ [ Hierarcy ] - [ ์ค๋ฅธ์ชฝ ๋ง์ฐ์ค ] - [ Create Empty ] ๋ฅผ ํตํด ๋น ์ค๋ธ์ ํธ ์์ฑ ํ
๋ค์๊ณผ ๊ฐ์ Script๋ฅผ Component๋ก ์ถ๊ฐํ๋ค.
๋ฐ์ดํฐ ์ ์ฅ Test๋ฅผ ์ํ FirebaseTest Script ์์ฑusing System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; public class FirebaseTest : MonoBehaviour { DatabaseReference dbReference; void Start() { dbReference = FirebaseDatabase.DefaultInstance.RootReference; dbReference.Child("users").Child("username").SetValueAsync("YS"); } }
5. Realtime Database์์ ๋ฐ์ดํฐ ์ ์ฅ์ด ์ ๋์๋์ง ํ์ธํ๋ค.
+ ๋ ๋ค๋ฅธ ๋ฐ์ดํฐ ์ ์ฅ ๋ฐฉ๋ฒ (https://firebase.google.com/docs/database/unity/save-data?hl=ko)
ํ์์ด ์ง์ ๋ C# ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํฐ๋ก ์ ์ฅํ๊ณ ์ถ์ ๊ฒฝ์ฐusing System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; public class Test : MonoBehaviour { DatabaseReference dbReference; void Start() { dbReference = FirebaseDatabase.DefaultInstance.RootReference; AddNewUser("1", "YS", "1234@naver.com"); } void AddNewUser(string userId, string name, string email) { User user = new User(name, email); string json = JsonUtility.ToJson(user); // ํ์์ด ์ง์ ๋ C# ๊ฐ์ฒด๋ฅผ ์ฌ์ฉ์ ๊ฐ์ฒด๋ฅผ ์์ JSON์ผ๋ก ๋ณํ dbReference.Child("users").Child(userId).SetRawJsonValueAsync(json); // ์ง์ ๋ ์์น์์ ํ์ ๋ ธ๋๋ฅผ ํฌํจํ์ฌ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฎ์ด์ด๋ค. dbReference.Child("users").Child(userId).Child("username").SetValueAsync("JY"); // ์ ์ฒด ๊ฐ์ฒด๋ฅผ ๋ค์ ์ฐ์ง ์๊ณ ํ์ ํญ๋ชฉ์ ์ ๋ฐ์ดํธ ํ๋ ๋ฐฉ๋ฒ } }โ
๐ ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
1. ํ์ฌ Realtime Database์ ์๋์ ๊ฐ์ ๋ฐ์ดํฐ๋ค์ด ์๋ค.
2. ๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํด Script๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ค.
๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ค๊ธฐ ์ํ FirebaseTest Script ์์ using System.Collections; using System.Collections.Generic; using UnityEngine; using Firebase; using Firebase.Database; public class Test : MonoBehaviour { DatabaseReference dbReference; void Start() { dbReference = FirebaseDatabase.DefaultInstance.RootReference; LoadUserInfo(); } void LoadUserInfo() { dbReference.Child("users").GetValueAsync().ContinueWith(task => { if (task.IsCanceled) Debug.Log("๋ก๋ ์ทจ์"); else if (task.IsFaulted) Debug.Log("๋ก๋ ์คํจ"); else { var dataSnapshot = task.Result; foreach (var data in dataSnapshot.Children) foreach (var child in data.Children) Debug.Log(data.Key + " : " + child.Key + " : " + child.Value); } }); } }โ
3. Console ์ฐฝ์ ํตํด ๋ฐ์ดํฐ๋ฅผ ์ ์์ ์ผ๋ก ๋ถ๋ฌ์๋์ง ํ์ธํ๋ค.
๐ ๋ฉ์๋ ์ฐธ๊ณ
'๐ Additional Study > ๐ Firebase' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Firebase์ ๋น๋๊ธฐ ์ฒ๋ฆฌ (0) | 2024.03.01 |
---|