Search Unity

Sharing ScriptableObject data with a webserver

Discussion in 'Multiplayer' started by keego7237, Mar 23, 2023.

  1. keego7237

    keego7237

    Joined:
    Feb 18, 2023
    Posts:
    3
    I'm trying to build a slow paced persistent world strategy game playable either in browser via WebGL or natively on desktop. I don't need to synch/share GameObject states across users. My plan is to handle this like a web hosted app, a Unity frontend backed up by a C# REST API hosted in a cloud service. The server will handle gameplay logic and mostly just give the Unity client updates, but a subset of gameplay will also run on the client side to reduce server load.

    The REST API / gameplay logic could be pretty much be agnostic to the fact that the client was written with Unity. But, for ease of development I want to be able to configure the gameplay logic in Unity editor. Meaning creating ScriptableObjects that define how much damage a particular weapon type does etc.

    What is a good setup for this?
    Do I build ScriptableObjects, then have something in the build process export all of them as JSON files which get dumped somewhere my server can read?
    Can the REST API load some Unity dependencies just to read in the ScriptableObjects and ignore everything else related to rendering or running MonoBehaviors?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Use JsonUtility to serialize the contents of an SO or any other object to json, you can even do so at runtime. More easily however you can just create all the data you need in the database on your webserver (or firebase) and read that into unity via REST. During development you can have the database locally, or distribute a copy of it in the build as Sqlite file to reduce the number of REST calls.
     
  3. keego7237

    keego7237

    Joined:
    Feb 18, 2023
    Posts:
    3
    I like the idea of using a database directly. Is it possible for the Unity Editor to hook into a database so that I can essentially set a field on a GameObject/ScriptableObject to be a row in a database and read/write through the editor?
     
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    Sure, that's possible. Easiest route is through Sqlite for development and you can later export that Sqlite file to regular SQL and import it in another DB.
     
  5. keego7237

    keego7237

    Joined:
    Feb 18, 2023
    Posts:
    3
    Do you happen to have a link to a tutorial or documentation about how to do this? Everything I see online is about how to setup SQLite and access it from c#, but I don't see anything about how to couple it with editable fields on a Game/Scriptable Object in the unity editor. Do I have to write my own editor/inspector code to propagate fields from a C# class to columns in the db?
     
  6. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,990
    In its simplest form, you would have a List<MyTable> in the SO and in OnEnable load that SQL table's contents into that list, and in OnDisable (or on user button click) you write the List back into SQL. MyTable would be a struct containing fields for each column like Id, Name, and so on.

    But honestly, you will not want to actually edit that data in an SO because it makes for a clunky editing experience unless you invest a lot of time writing a spreadsheet-like Inspector, or possibly tweaking an existing one. There are spreadsheet editors out there on the store and some may even use SQL backends though.

    But it's usually a lot easier to do so with SQLite apps (SQLite Studio, DB Browser) that allow you to modify tables and data as if it were a spreadsheet, or export/import to/from a spreadsheet via CSV files. With a little scripting you can even automate the import/export process.