Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Central component solution

Discussion in 'Getting Started' started by CptBertorelli, Apr 15, 2020.

  1. CptBertorelli

    CptBertorelli

    Joined:
    Apr 9, 2020
    Posts:
    20
    Hello,

    I would like to somewhere store data to be available across all instances of my game (eg. leaderboard, weekly competition between players). The amount of data is very small and its structure is not complicated (I don't need any relational dbms).

    What would you recommend as a cheap and efficient solution to this problem?

    Thank you in advance.
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Does the data have to be stored server-side?
    If not, have a look at ScriptableObjects.

    Otherwise, if you're familiar with writing web services, you could write & host your own REST API and have your Unity app communicate with it via HTTP requests.
    You could use either the built-in .NET HttpClient or Unity's UnityWebRequest objects, though UnityWebRequest is deprecated and planning to be replaced with something else in the future.
     
  3. CptBertorelli

    CptBertorelli

    Joined:
    Apr 9, 2020
    Posts:
    20
    Thanks.

    The data needs to be available by all game instances. So it needs to be stored on the server/cloud.

    My question was not related to network algorithms. I rather asked about cheap solutions where I could store the data. AWS or Azure seem to be quite expensive for that.
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Firebase has a pretty generous free-tier plan. Maybe worth checking out?
     
  5. CptBertorelli

    CptBertorelli

    Joined:
    Apr 9, 2020
    Posts:
    20
    Thank you, looks promising!
     
  6. CptBertorelli

    CptBertorelli

    Joined:
    Apr 9, 2020
    Posts:
    20
    I made a research on that matter.

    Firebase offers two kind of solutions: Cloud Firestore and Realtime Database. The second one offers dedicated SDK for Unity. I followed its steps. However after integrating my application with SDK, big problems emerged. After launching my app (ctrl+p in Unity) I must wait 15-20seconds more. Afterwards I can observe additional lags during next ~15 seconds. I did it on fresh new project and it looked the same way.

    So I changed my approach to use REST API instead of dedicated SDK. It works quickly and nicely. However I cannot find a good way to secure this approach. How to implement authentication? I do not have a separate server, all instances of my mobile app will connect directly to Firebase database.

    Thank you in advance for help
     
  7. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    After researching how to do this, it seems fairly complex to explain over text, so I made a little step-by-step graphic tutorial.
    The authentication method I'm using is email/password, but the same concepts should apply with other authentication methods.
    I'm also using Postman to send the HTTP requests, but the results should be the same no matter what HTTP client you use.
    fb-tut-1.jpg fb-tut-2.jpg fb-tut-3.jpg fb-tut-4.jpg

    References:
    https://firebase.google.com/docs/database/rest/structure-data?authuser=0
    https://firebase.google.com/docs/database/security/quickstart?authuser=0#user
    https://firebase.google.com/docs/reference/rest/database#section-api-usage
    https://firebase.google.com/docs/database/rest/auth#authenticate_with_an_id_token
    https://firebase.google.com/docs/reference/rest/auth
     
  8. CptBertorelli

    CptBertorelli

    Joined:
    Apr 9, 2020
    Posts:
    20
    Thank you so much for such a detailed tutorial. I followed all steps and everything worked like a charm. I searched previously for a solution under https://firebase.google.com/docs/auth but it turns out that I should visit 'References' tab instead of Guide.

    By the way, the bug with Firebase SDK for Unity I mentioned earlier is already raised but sadly still not resolved:
    https://github.com/firebase/quickstart-unity/issues/639. Its quite fresh (2 weeks) though.
     
  9. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Glad I could help.