Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

CSV to Unity/WebGl.. possible?

Discussion in 'Getting Started' started by Tawnwen, Nov 11, 2019.

  1. Tawnwen

    Tawnwen

    Joined:
    Oct 30, 2019
    Posts:
    11
    Hello everyone.

    Me and my team are building a Unity application that should be available on the web. I wrote a whole run-through of that here (not necessary to read), but wanted to make a separate thread for this specific question.

    p.s We're all new to Unity and haven't gotten any actual "development" done. For that reason, we just need some general insight.

    I'm fully aware that Unity can read local CSV files through a script with some simple file IO and parsing, but how would a Unity-WebGl application read a CSV file that was queried from a database and is available on the webpage? Once the web application requests and receives a CSV data file in their http-response body, how could we then pass this data to the Unity-WebGl app at run-time for our scripts to use?

    (We need the CSV data to modify our game objects a bit... just scale them and make sure they're positioned correctly before the user starts "playing". On desktop this would be easy if we had a folder with all CSV files handy, but alas, this needs to be on the web and there are too many CSV files to store in a file).

    Any references you guys could link me to? I've searched and thus far come up with nothing. I know web stuff isn't standard use case for Unity, but still.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    8,283
    I'm not sure I see the difficulty. Fetch your CSV data with UnityWebRequest, and parse it with ordinary C# string parsing.
     
    Tawnwen likes this.
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,922
    I might suggest to just hard code some values first, and get it working, then add the CSV portion. Or have a text input field to enter the values. Break the problem down into smaller pieces. And you don't want to connect directly to a database, it's not scalable and is not secure (each user requires a separate db connection, which is slow). You would want to use a proper web API service instead, building on what JoeStrout mentioned. Have your web service in turn connect over a single connection to your database.
     
  4. Tawnwen

    Tawnwen

    Joined:
    Oct 30, 2019
    Posts:
    11
    @JeffDUnity3D What I'm understanding is, have our web app fetch the CSV data via a secure API and bring it to the client, then have our WebGl Unity application grab it using UnityWebRequest?

    That was our original plan, apart from not knowing about UWR of course.
     
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,922
    Actually either approach should work, fetching the CSV files directly, or using a database. How often does the data change? Directly getting the CSV as Joe mentioned would probably be best at any rate.
     
  6. Tawnwen

    Tawnwen

    Joined:
    Oct 30, 2019
    Posts:
    11
    Hm, updates shouldn't be too frequent (maybe an occasional change of parts) but they'd be important.

    The only way I understand how to do this (and excuse me, I've yet to take a databases course) is to send a UnityWebRequest to the SQL server, the server grabs the data, SQL server sends it back, UnityWebRequest receives the data, and parses it for use. By "fetching the CSV files directly", did you mean manually downloading the CSV files and then essentially "dragging and dropping" into the app?
     
  7. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    5,922
    No, the web request would retrieve the files directly like when you browse to a web site and retrieve the text and images back in your browser. But instead of showing the file content in a browser, the content of the files is stored in variables in your program that are then applied to your 3D objects. This is all done in the code that you write and happens within milliseconds. But again, don't spend too much time on this part. Simply hard-code (enter) the values directly into your variables for testing.
     
    JoeStrout likes this.