Skip to content

Instantly share code, notes, and snippets.

@Cdddo
Last active September 7, 2022 22:12
Show Gist options
  • Save Cdddo/c0528109a570b78f519e81fcfdb95061 to your computer and use it in GitHub Desktop.
Save Cdddo/c0528109a570b78f519e81fcfdb95061 to your computer and use it in GitHub Desktop.
Unity to Google Forms
// https://github.com/luzan/Unity-Google-Spreadsheet
// Save data from Unity to Google Spreadsheet
// No Authentication, plugins, SDK required
///<summary>
/// Here we’ll be creating a Google Form that will save the responses to the Google Spreadsheet an 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.
/// So, our first step is to create a Google Form. 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.
/// A few things on the scripts are achieved 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)
/// 2. Press CTRL + F to find, and type “form action” from here you’ll get BASE_URL.
/// 3. Now we need to get entry fields, on same page search for “input type=”text” “, and copy the data inside attribute name (Tip: starts with entry.NUMBER). Remember type can vary for every field according to the type you select during form creation. eg. entry.1673653496
///</summary>
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class UnityToGoogleForms: MonoBehaviour
{
[SerializeField] private InputField _inputUsername;
[SerializeField] private InputField _inputEmail;
[SerializeField] private InputField _inputComment;
public bool SendFromEditor = true;
private string _name;
private string _email;
private string _comment;
// This url must be taken from the Google Form.
private string FORM_URL = "https://docs.google.com/forms/d/e/1FAIpQLSeAC0CYd1m9hvphv28eSv-ot4lZpszLDUmQa_B76vAEjZ-6Ow/formResponse";
// This is called by a submit button configured in Unity
public void Send()
{
if (!SendFromEditor && Application.isEditor) return;
_name = _inputUsername.text;
_email = _inputEmail.text;
_comment = _inputComment.text;
StartCoroutine(Post(_name, _email, _comment));
}
// This is a web request handler so it must be able to wait for the web to respond (i.e. coroutine)
private IEnumerator Post(string name, string email, string comment)
{
WWWForm form = new WWWForm();
//These are the form fields, the entry values should be inspected from the form page
form.AddField("entry.1673653496", name);
form.AddField("entry.1422402232", email);
form.AddField("entry.1022174842", comment);
UnityWebRequest www = UnityWebRequest.Post(FORM_URL, form);
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Debug.Log(www.error);
}
else
{
Debug.Log("Form upload complete!");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment