UniGame.GoogleSpreadsheetsImporter
UniGame.GoogleSpreadsheetsImporter copied to clipboard
google spreadsheet v.4 support library for unity3d
Unity3D Google Spreadsheets v.4 Support
Unity3D Google Spreadsheet export/import library
Odin Inspector Asset recommended to usage with this Package (https://odininspector.com)
Features
- Export Serializable Project Data into Google Spreadsheet
- Support Custom Import/Export data providers
- Support nested synched spreadsheed fields
- Support export/import JSON to serializable classed
- Export/import into Addressables AssetReferences
- Export/import into Unity Scriptable Objects
- Export/import base Unity Assets types
Table of Content
- Data Definitions
- Nested Spreadsheet tables
- Connect to Google Spreadsheet
- Supported types
Create Importer asset
Menu: "UniGame/Google Spreadsheet/Google Spreadsheet Asset"
Data Definitions
Import and Export Attributes
=============
SpreadsheetTargetAttribute
Allow to mark serializable class as Synched with Google Spreadsheet.
SpreadsheetTargetAttribute(string sheetName = "",string keyField = "",bool syncAllFields = true)
Parameters:
- sheetName. Name of target Table into Google Spreadsheet. If sheetName empty, then class Type name will be used as SheetName
- keyField. Primary key field name for sync between spreadsheet and serialized class
- syncAllFields. If TRUE, then try to sync all class fields data. If FALSE - synchronized only fields marked with attribute [SpreadSheetFieldAttribute]
=============
SpreadSheetFieldAttribute
All fields marked with this attribute will be synchronized with target Spreadsheet data
public SpreadSheetFieldAttribute(string sheetField = "", bool isKey = false)
Parameters:
- sheetField. Name of target Spreadsheet column
- isKey. If TRUE, then target field will be used as Primary Key field.
[SpreadsheetTarget("DemoTable")]
public class DemoSO : ScriptableObject{
[SpreadSheetField("KeyField",true)]
public string key;
[SpreadSheetField("ValueField",true)]
[SerializableField]
private string value;
}
=============
Default Sheet Id
[SpreadSheetField("DemoTable",syncAllFields: true)]
public class DemoSO : ScriptableObject{
public string id; // Field with name Id | _id | ID will be used as Primary key by Default
private string value; // syncAllFields value active. Import/Export try to find Sheet column with name "Value"
}
=============
Nested Spreadsheet tables
Nested fields support
[SpreadSheetField("DemoTable",syncAllFields: true)]
public class DemoSO : ScriptableObject{
public string id; // Field with name Id | _id | ID will be used as Primary key by Default
[SpreadSheetField("ResourcesTable",syncAllFields: true)]
private int cost; // syncAllFields value active. Import/Export try to find column with name "Cost" from sheet "ResourcesTable"
}
Nested Table
Supported Types
Spreadsheet library support all base value types.
Custom Type support
To add support your custom type you need to realize new converter that extends base interface ITypeConverter. As a shortcut you can implement BaseTypeConverter class and mark with [Serializable] attribute.
public interface ITypeConverter
{
bool CanConvert(Type fromType, Type toType);
TypeConverterResult TryConvert(object source, Type target);
}
[Serializable]
public class StringToVectorTypeConverter : BaseTypeConverter
{
....
}
Now we can add it into converters list
Unity Assets Support
your can make reference to unity asset by type filtering and specifying name of asset
[SpreadSheetField("DemoTable",syncAllFields: true)]
public class DemoSO : ScriptableObject{
public string id; // Field with name Id | _id | ID will be used as Primary key by Default
private Sprite iconAsset;
}
Auto Sync Scriptable Assets on Import
Unity Addressables References support
Same usage as regular unity asset type with only one exception. The type of target field must be AssetReference or inherited from AssetReference
[SpreadSheetField("DemoTable",syncAllFields: true)]
public class DemoSO : ScriptableObject{
public string id; // Field with name Id | _id | ID will be used as Primary key by Default
private AssetReference iconReference;
}
JSON support
For more complex scenarios JSON serialization can be used
[SpreadSheetField("DemoTable",syncAllFields: true)]
public class DemoSO : ScriptableObject{
public string id; // Field with name Id | _id | ID will be used as Primary key by Default
[SpreadSheetField("ItemsTable",syncAllFields: true)]
private ItemData defnition; // sync item data from json value value
}
[Serializable]
public class ItemData
{
public string id;
public int position;
}
Pipeline Importer
for more complex import scenario we can use Pipeline Importer Asset
How To Create
Create Menu: "UniGame/Google/Importers/PipelineImporter"
Each step of import pipeline take data from previous one. For custom import step your should implement one of:
- SerializableSpreadsheetImporter: pure serializable c# class
- BaseSpreadsheetImporter: ScriptableObject asset
For example:
- Create Units Prefabs by Spreadsheet Data
- Apply Unit settings from sheets data to assets
- Mark Assets as an Addressables an move them into target group
Connect to Google Spreadsheet
Editor Window
Create Google Api Credentials
- HOWTO create api credentials https://developers.google.com/sheets/api/quickstart/dotnet
IMPORTANT
When you create an Desktop API KEY:
- Setup path to credential .json file and press "Connect Spreadsheet"
- Under target Google Profile allow application access to spreadsheets
Spreadsheet Id's
- Now you can specify your spreadsheets
- Id's of your sheet can be found right from web page url
- Copy your table id and paste into importer window field
Google API V4 .NET References
-
https://developers.google.com/sheets/api/quickstart/dotnet
-
https://googleapis.dev/dotnet/Google.Apis.Sheets.v4/latest/api/Google.Apis.Sheets.v4.html
UPM Installation
Add to your project manifiest by path [%UnityProject%]/Packages/manifiest.json new dependency:
{
"dependencies": {
"unigame.unityspreadsheets" : "https://github.com/UnioGame/UniGame.GoogleSpreadsheetsImporter.git",
}
}
Official Newtonsoft.Json Unity Package
{
"name": "com.unity.nuget.newtonsoft-json",
"displayName": "Newtonsoft Json",
"version": "2.0.0",
"unity": "2018.4",
"description": "Newtonsoft Json for use in Unity projects and Unity packages. Currently synced to version 12.0.2.\n\nThis package is used for advanced json serialization and deserialization. Most Unity users will be better suited using the existing json tools built into Unity.\nTo avoid assembly clashes, please use this package if you intend to use Newtonsoft Json.",
"type": "library",
"repository": {
"type": "git",
"url": "[email protected]:unity/com.unity.nuget.newtonsoft-json.git",
"revision": "74ca86c283a2f63ba5b687451a0842ba924da907"
}
}
Co-Processors
Selected co-processor execute after main import processor (after parsing and applying every row).
Custom co-processor
[Serializable]
public class MyCustomCoProcessor : ICoProcessorHandle
{
// some properties
public void Apply(SheetValueInfo valueInfo, DataRow row)
{
// some code
}
}
Example
For example, Nested Table Co-Processor applies nested google-table where filter is parsing pattern: