Step-by-Step Guide: in-app collect user emails to submit to Google Form in C# Unity

neon lights in envelope shape

To request the user to fill in their email and send this information to a Google Sheets form, you can use a Google Form linked to a Google Sheet, then submit the email data via an HTTP POST request from your application. Here’s a step-by-step guide to implement this in a C# application:

Step 1: Set Up a Google Form Linked to Google Sheets

  1. Create a Google Form:
  • Go to Google Forms.
  • Create a new form and add a field for the user’s email.

2. Link the Google Form to a Google Sheet:

  • Click on Responses in the Google Form editor, which is circled red here:
  • Click on the Google Sheets icon to link the form responses to a Google Sheet.
  • A new Google Sheet will be created where the form responses will be stored.

3. Get the Form’s Entry ID for the Email Field:

Now, click on the 3 dots button as circled green in this image:

Then, select Get pre-filled link. A new page will pop up. Right-click on the form page, select Inspect, and look for the form field’s entry ID (e.g., entry.1234567890) for the email field. This ID will be used in your HTTP request to submit data to the form.

Step 2: Create the Unity Script

  1. Create a new C# script in Unity (e.g., GoogleFormSubmitter.cs).
  2. Set up the script to use Unity’s InputField and UnityWebRequest to send the email to Google Forms.

Here’s the full script (Click to download the codes):

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Collections;

public class GoogleFormSubmitter : MonoBehaviour
{
    // Attach this to an InputField in the Inspector
    public InputField emailInputField;

    // Replace with your Google Form's POST URL
    private string googleFormUrl = "https://docs.google.com/forms/d/e/your-form-id/formResponse";

    // Replace with your Google Form's entry ID for the email field
    private string emailFieldEntryID = "entry.1234567890";

    public void SubmitEmail()
    {
        if (string.IsNullOrEmpty(emailInputField.text))
        {
            Debug.LogError("Email field is empty.");
            return;
        }

        StartCoroutine(PostEmailToGoogleForm(emailInputField.text));
    }

    private IEnumerator PostEmailToGoogleForm(string email)
    {
        WWWForm form = new WWWForm();
        form.AddField(emailFieldEntryID, email);

        UnityWebRequest request = UnityWebRequest.Post(googleFormUrl, form);

        yield return request.SendWebRequest();

        if (request.result == UnityWebRequest.Result.Success)
        {
            Debug.Log("Email submitted successfully.");
        }
        else
        {
            Debug.LogError("Error submitting email: " + request.error);
        }
    }
}

Step 3: Set Up the Unity UI

  1. Create an Input Field in your Unity scene:
  • Go to GameObject > UI > Legacy > Input Field to add a new input field.
  • Set the Placeholder text to something like “Enter your email”.
  1. Add a Button for submission:
  • Go to GameObject > UI > Button.
  • Set the button text to “Submit”.
  1. Attach the Script:
  • Drag the GoogleFormSubmitter script onto any GameObject (e.g., an empty GameObject called FormManager).
  • Assign the emailInputField variable in the Inspector to your input field.
  1. Link the Button to the Script:
  • Select your button and go to the Inspector.
  • In the On Click () section, add the GoogleFormSubmitter object.
  • Select GoogleFormSubmitter.SubmitEmail() as the function.

Step 4: Test the Form Submission

  1. Run the Scene.
  2. Enter an email in the input field and click the submit button.
  3. Check your Google Sheet to see if the email was added correctly.

Important Notes

  • Ensure the Google Form and the Google Sheet permissions are set to allow access without signing in.
  • This approach relies on Google Forms being open for submissions from anyone with the link.

Discover more from Science Comics

Subscribe to get the latest posts sent to your email.

Leave a Reply

error: Content is protected !!