Skip to content

Latest commit

 

History

History
267 lines (180 loc) · 8.53 KB

IntegrationWithGoogleSheet.md

File metadata and controls

267 lines (180 loc) · 8.53 KB

日本語

What You Need for Google Sheets

If you haven't set up settings yet, please complete it from Initial Setup

Two Types Prepared

There are two types, so please use the one that fits your needs.

  • Simple - A simple one with only Key + language-specific text
  • Custom - A versatile one that can be customized

Simple

Although you can't change the structure of the spreadsheet, you can use it immediately without generating any scripts.

Generating the Spreadsheet

Generate CuvImporter

Right-click on the Project and select CuvImporter from "CMSuniVortex > create CuvImporter".

Select CMSuniVortex.GoogleSheet.GoogleSheetCuvClient as client

Enter Required Information in CuvImporter

explanation e.g.
Build Path Path to generate assets Assets/Generated/
Languages Specify the language, even if not in use, you must select at least one. English
Sheet Url Specify the URL of the spreadsheet https://docs.google.com/spreadsheets/d/sheetID/
Sheet Names Name of the tab at the bottom of the spreadsheet Animals, SeaCreatures
Json Key Path Path where the service account is saved Assets/GoogleSheetTest/light-operator-x-x-x.json

Import

After entering, please click "Import". If it is output without errors, it is complete.

Output

Specify how to reference. This time, we specified GoogleSheetCuvOutput which is a direct reference.

Acquisition and Display

The easiest way is to use CuvComponent. Save the following code as TestText.cs, place it under Assets, attach it to Text, and enter the necessary information.

using CMSuniVortex.Compornents;
using CMSuniVortex.GoogleSheet;
using UnityEngine;
using UnityEngine.UI;

public sealed class TestText : CuvComponent<GoogleSheetCuvReference>
{
    [SerializeField] Text _text;
        
    protected override void OnChangeLanguage(GoogleSheetCuvReference reference, string key)
    {
        if (reference.GetList().TryGetByKey(key, out var model))
        {
            _text.text = model.Text;
        }
    }
}

Custom

You can freely change anything other than the first Key of the spreadsheet. Script generation is required.

Generating the Spreadsheet

Generate CuvImporter

Right-click on the Project and select CuvImporter from "CMSuniVortex > create CuvImporter".

Click on the "Script Generator" button of the generated CuvImporter

Script Generation

Please enter the necessary information and generate.

explanation e.g.
Full Class Name Specify the class name. You can also specify the namespace. namespace.ClassName
Build Path Specify the directory path to generate the code Assets/Models/

Enter Required Information in CuvImporter

Select the generated Client.

Enter the information.

explanation e.g.
Build Path Path to generate assets Assets/Generated/
Languages Specify the language, even if not in use, you must select at least one. English
Sheet Url Specify the URL of the spreadsheet https://docs.google.com/spreadsheets/d/sheetID/
Json Key Path Path where the service account is saved Assets/GoogleSheetTest/light-operator-x-x-x.json

Import

Click the "Import" button to import. After import, if it is output without errors, it is successful.

Output

Specify how to reference. This time, we specified the 'CustomGoogleSheetCuvOutput' which is a direct reference. Click "Output" to output.

Acquisition and Display

Refer to Simple.

Always Set Key

Please be sure to set the first 'Key' of the sheet. Also, make sure that this key is not duplicated.

Custom Method

The 'Text' column of the sheet can be retrieved with the model's Text = GetString("Text");.

public sealed class Meta : CustomGoogleSheetModel
{
    public ElementType Element;
    public string Text; // <--
    public bool Boolean;
    public int Number;
    public Sprite Image;
    public string Date;
    
    public enum ElementType { Title, Narration, Character1, Character2, Character3, TextOnly }

    protected override void OnDeserialize()
    {
        Element = GetEnum<ElementType>("Element");
        Text = GetString("Text"); // <--
        Boolean = GetBool("Boolean");
        Number = GetInt("Number");
        Date = GetDate("Date");
        
        LoadSprite("Image", sprite => Image = sprite);
    }
}

If you choose the Addressable-compatible CuvClient, you can use AssetReference.

using System;
using CMSuniVortex.GoogleSheet;
using UnityEngine.AddressableAssets;

[Serializable]
public sealed class MetaAddressable : CustomGoogleSheetModel
{
    public AssetReferenceSprite Sprite;
    public AssetReferenceTexture2D Texture;

    protected override void OnDeserialize()
    {
        LoadSpriteReference("Image", asset => Sprite = asset);
        LoadTextureReference("Image2", asset => Texture = asset);
    }
}

Addition

We will explain how to add. First, we want to try adding a Float to the English sheet.

For Japanese, it's easier to import from the English sheet, except for areas that require translation. We're displaying cells E to J of English using the IMPORTRANGE function.

// Sheet url, Sheet name + cells
=IMPORTRANGE("https://docs.google.com/spreadsheets/d/13XEuxW89jT4ICb2guBcgcgPrCmY_oGxDQgiWNOth7ww/", "English!E:J")

We add Float to the generated model and add deserialization processing.

public sealed class Meta : CustomGoogleSheetModel
{
    public ElementType Element;
    public string Text;
    public bool Boolean;
    public int Number;
    public Sprite Image;
    public string Date;
    public float Float; // <--
    
    public enum ElementType { Title, Narration, Character1, Character2, Character3, TextOnly }

    protected override void OnDeserialize()
    {
        Element = GetEnum<ElementType>("Element");
        Text = GetString("Text");
        Boolean = GetBool("Boolean");
        Number = GetInt("Number");
        Date = GetDate("Date");
        Float = GetFloat("Float"); // <--
        
        LoadSprite("Image", sprite => Image = sprite);
    }
}

After adding, we import.

If it's added without any errors, like shown below, then it's complete. By removing or adding in this manner, try creating your own original sheet.

Google API Libraries

We are using the official libraries for data retrieval.

https://www.nuget.org/profiles/google-apis-packages

  • Google.Apis
  • Google.Apis.Core
  • Google.Apis.Auth
  • Google.Apis.Drive.v3
  • Google.Apis.Sheets.v4