Skip to content

Instantly share code, notes, and snippets.

@5argon
Created May 2, 2018 15:13
Show Gist options
  • Save 5argon/b01c355cd6635a010df4575b185a5b16 to your computer and use it in GitHub Desktop.
Save 5argon/b01c355cd6635a010df4575b185a5b16 to your computer and use it in GitHub Desktop.
How to get Google Spreadsheet data to Unity. Put Google Api + Auth + Sheets .dll into your project. Then put .p12 service account file somewhere in Assets.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Sheets.v4;
using Google.Apis.Sheets.v4.Data;
using Google.Apis.Services;
using System.Security.Cryptography.X509Certificates;
using UnityEngine;
using UnityEditor;
class LevelSynchronizer
{
const string spreadsheetId = "1sakdfj0392029482_sajfalsdfhlsadkfsak";
const string sheetNameAndRange = "MySheet"; //You can also put a cell range here
const string p12PathFromAsset = "Plugins/example-23948239f.p12";
public static void SyncLevel()
{
String serviceAccountEmail = "example@myproject.iam.gserviceaccount.com";
var certificate = new X509Certificate2(Application.dataPath + Path.DirectorySeparatorChar + p12PathFromAsset, "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { SheetsService.Scope.SpreadsheetsReadonly }
/*
Without this scope, it will :
GoogleApiException: Google.Apis.Requests.RequestError
Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential.
lol..
*/
}.FromCertificate(certificate));
var service = new SheetsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
});
SpreadsheetsResource.ValuesResource.GetRequest request = service.Spreadsheets.Values.Get(spreadsheetId, sheetNameAndRange);
StringBuilder sb = new StringBuilder();
ValueRange response = request.Execute();
IList<IList<object>> values = response.Values;
if (values != null && values.Count > 0)
{
foreach (IList<object> row in values)
{
foreach(object cell in row)
{
sb.Append(cell.ToString() + " ");
}
//Concat the whole row
Debug.Log(sb.ToString());
sb.Clear();
}
}
else
{
Debug.Log("No data found.");
}
}
}
@agnelpb
Copy link

agnelpb commented Sep 14, 2022

This works for Editor.

If you are making a build which reads from Google Sheets and the X509Certificate2 is having trouble accessing the Resouce Folder, replace the P12 file extension as .bytes

Then use the code

TextAsset p12 = Resources.Load(P12FileNameWithoutExtension);
var certificate = new X509Certificate2(p12.bytes, Password, X509KeyStorageFlags.Exportable);

@ssmasrour
Copy link

I recently improved an automated Google Sheets importer plugin fo unity here: https://github.com/ssmasrour/Google-Sheets-to-Unity

It may help you to make it easy to coordinate with your data sheets [cheers]

@5argon
Copy link
Author

5argon commented Jan 21, 2024

I recently improved an automated Google Sheets importer plugin fo unity here: https://github.com/ssmasrour/Google-Sheets-to-Unity

It may help you to make it easy to coordinate with your data sheets [cheers]

Awesome! Looks great!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment