MATTHEW VENTURES
  • Home
  • Resume
  • Side Quests
  • Other
    • Professional Timeline
    • Recommendations
    • Tutorials
    • Research >
      • Glyph
      • NASA
    • Getting Into Game Dev

Downloading Google Sheets

11/5/2019

11 Comments

 
Introduction / Motivation
Sometimes it is helpful to think of games in terms of code and data. In this model, "data" contains the art, music, information, etc of a game and "code" contains the instructions of how to use it. When we design game architectures, we usually aim to separate the two when possible. This allows us to perform actions such as "patching" which means downloading remote data without the need to recompile our game. This is tremendously helpful when you have small changes you want to make and would prefer to avoid uploading the entire game again to the App Store or PlayStore (also useful if someone buys your game on a physical disk!)
​Using Google Sheets we can store and edit our data to make live changes to our games! This tutorial covers how to download CSV's from Google Sheets and then parse them for data!

Overview
In this tutorial we will breakdown an approach to downloading data from Google Sheets in a CSV format. This will allow you to update your game by simply changing a few values online! This approach will make use of three classes...
  • TermData - this class is a small structure which we will fill with the data we download
  • CSVDownloader - this class manages the download part
  • Loader - this class initiates the download  and manages the processing of data afterwards
I will be using this Google Sheet as my example. Its role is to contain translations for different terms. The far right column is the English and each row is a term translated differently for each column.

Note that the first cell is "3=English" instead of "English" this "3=" is a system I am using to denote the version of the document. When I make an update, I increment the version (for example the next version would be  "4="). 
Picture
TermData
CSVDownloader
Loader
11 Comments
Matthew Ventures
11/10/2019 02:44:08 pm

Q: When would I want to use this approach rather than a traditional database?
A: This approach is best for when you want to easily edit the sheet with multiple people. We can always just bake the CSV and download it from a hosting site but GSheets make live edits and cross collaboration easy. *Especially for non-tech-savvy contributors such as volunteer translators.*

Reply
Sarah
1/11/2020 07:15:45 pm

Thank you =)

Reply
Josh M.
4/2/2020 03:51:05 am

Trying to implement this into a project, but it keeps throwing a nullreference exception whenever I try to call for DownloadData. Suggestions to a fix?

Reply
Matthew Ventures
4/2/2020 03:57:15 am

Hey, thanks for reaching out. It's difficult to say without looking at the logs. Could you send me as many screenshots as possible to matthewXVentures@gmail.com? I will try my best to help

Reply
Ashish
4/26/2020 06:04:49 pm

his was my local .csv script which i wrote and can u help how to link downloaded .csv file to my script and how to get data of particular row from its ID FROM .DOWNLOADED CSV FILE

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;

public class storm : MonoBehaviour
{
// Start is called before the first frame update
public TextMesh t1, t2, t3, t4;
public GameObject other, this1;


public void OnMouseDown()
{
this1.SetActive(true);
other.SetActive(false);
ReadCSVFile(6);
}

// Update is called once per frame

void ReadCSVFile(int j)
{


StreamReader streamReader = new StreamReader(new MemoryStream((Resources.Load<TextAsset>("Water")).bytes));



bool endOfFile = false;
int i = 0;
while (!endOfFile)
{
string data_String = streamReader.ReadLine();
if (data_String == null)
{
endOfFile = true;
break;
}
var data_values = data_String.Split(',');
if (i == j)
{
t1.text = data_values[0].ToString();
t2.text = data_values[3].ToString();
t3.text = data_values[4].ToString();
t4.text = data_values[6].ToString();

Debug.Log(data_values[0].ToString() + " " + data_values[1].ToString() + " " + data_values[2].ToString() + " " + data_values[3].ToString() + " " + data_values[4].ToString() + " " + data_values[5].ToString() + " " + data_values[6].ToString() + " " + data_values[7].ToString() + " " + data_values[8].ToString() + " " + data_values[9].ToString());
// Debug.Log(data_values[1].ToString() + " " + data_values[1].ToString());
}
i++;

}




}



}




Reply
Matthew
4/26/2020 06:36:38 pm

I responded over email that there are several alternative tutorials for a local db approach: https://www.google.com/search?client=firefox-b-1-d&q=use+csv+in+unity

Reply
ima_wizzard
9/2/2020 07:09:05 am

Hello, I got some errors when adding the code.

CSVDownloader:
line 21: webRequest.isNetworkError (deprecated)

Loader:
Line 57: Manager does not exist; TermData() not found;
Line 71: Utils.IsIOS() has no definition.

Can you help me please?

Reply
Matthew Ventures
9/2/2020 11:02:22 am

Hello, these lines of code refer to other code in my specific codebase. If you would like me to write the code for you for this to hook into an existing game please email me at matthewXventures@gmail.com to discuss rates.

Reply
D
8/15/2021 10:49:03 am

Hey Matthew, thanks for the tutorial! The video description made it seem like you were sharing your classes--is this still the case? The link-looking text doesn't seem to link anywhere. If not, thanks anyways!

Reply
Matthew Ventures
8/15/2021 12:25:56 pm

You can find all my game dev tutorials are listed here: https://www.mrventures.net/gamedevlearn.html

Reply
sad link
8/25/2021 10:28:59 am

ad

Reply



Leave a Reply.

  • Home
  • Resume
  • Side Quests
  • Other
    • Professional Timeline
    • Recommendations
    • Tutorials
    • Research >
      • Glyph
      • NASA
    • Getting Into Game Dev