How to save data to Google Spreadsheet from Unity 3D (No SDK – No Plugins)

Learn how to save data to Google Spreadsheet from Unity 3D to save user and game data without using any plugin or SDK. With help of this tutorial you can create survey apps, newsletter subscription and many other ideas on your Unity App without having to worry for databases. Store all the information on Google Sheet from Unity.

Difficulty Level : Beginner

Total Time for Project: 15 minutes

Extra Skills:

  • Browser Inspect Element or View Source code and Find (Ctrl + F) 
  • Google Forms

Github: https://github.com/luzan/Unity-Google-Spreadsheet
StackOverflow: https://stackoverflow.com/q/44200938/1939163

How to save data to Google Spreadsheet from Unity 3D

Basic Workflow:

Here we’ll be creating a Google Form that will save the responses to the Google Spreadsheet using few tricks we will replicate that form into our Unity App, so that the data to the Google Form will now be feed from Unity App.

Firstly, we’ll have to create a Google Form. After that, Login to Google Forms with Google Account and create a Google form. New Google Form will have fields and entries that are similar to what you are trying to keep on unity app.

After creation of Form, start a Unity Project and create similar form on the 2D Canvas by adding Input Fields. This is my form in Unity.

Save data to Google Spreadsheet from Unity 3D
Save data to Google Spreadsheet from Unity 3D

Now, add the script to the Canvas Component.

C# – Save Data to Google Sheet

Follow this C# Code to save data to Google Spreadsheet from Unity 3D app.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SendToGoogle : MonoBehaviour {

    public GameObject username;
    public GameObject email;
    public GameObject phone;

    private string Name;
    private string Email;
    private string Phone;

    [SerializeField]
    private string BASE_URL = "https://docs.google.com/forms/d/e/1FAIpQLSeAC0CYd1m9hvphv28eSv-ot4lZpszLDUmQa_B76vAEjZ-6Ow/formResponse";

    IEnumerator Post(string name, string email, string phone) {
        WWWForm form = new WWWForm();
        form.AddField("entry.1673653496", name);
        form.AddField("entry.1422402232", email);
        form.AddField("entry.1022174842", phone);
        byte[] rawData = form.data;
        WWW www = new WWW(BASE_URL, rawData);
        yield return www;
    }
    public void Send() {
        Name = username.GetComponent<InputField>().text;
        Email = email.GetComponent<InputField>().text;
        Phone = phone.GetComponent<InputField>().text;
        StartCoroutine(Post(Name, Email, Phone));

    }
}

Up to this step, every component of the UI should be connected to the script, and you are ready to go.

How to find Input’s name from Google Forms

To embed Google Forms on Unity, you’ll have to get input entries from Source Code of the Google Forms, to get that follow these steps.

  1. Right click Google Form Page and click View page source (CTRL + U on Windows and cmd-alt-u on Mac)
  2. Press CTRL + F (cmd-f on Mac) to find, and type “FB_PUBLIC_LOAD_DATA_” .
    You’ll see something like this
    embed google forms
  3. Firstly, copy the first part of the script.
    var FB_PUBLIC_LOAD_DATA_ = [null,[null,[[1157917829,"Question 1",null,0,[[942198238,null,0]
    ]
    ]
    ,[1168406727,"Question 2",null,0,[[194659197,null,0]
    ]
    ]
    ]
  4. Here are two entries I got form the form
    // all entries with IDs
    Question 1: 942198238
    Question 2: 194659197

    Simple trick is to take the string, likewise copying the number that comes after it from within square brackets.

  5. Now, set the entries field or form’s
    name property as entry.{entry_id}, eg:

    form.AddField("entry.942198238", name);

Now, App is ready to run. Watch video for more detailed information.

2 thoughts on “How to save data to Google Spreadsheet from Unity 3D (No SDK – No Plugins)”

  1. hi, SO I have been using this frrom very long time and works like a charm but yesterday i was doing a project and when i created form i was not ablw to find the “entry id” for any fiels in form source code, i thibk google have removed that option, is there any solution ?

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: