Unity3D Google Spreadsheet export/import library
Odin Inspector Asset recommended to usage with this Package (https://odininspector.com)
- 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
Menu: "UniGame/Google Spreadsheet/Google Spreadsheet Asset"
=============
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 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
Spreadsheet library support all base value types.
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
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;
}
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;
}
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;
}
for more complex import scenario we can use Pipeline Importer Asset
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
- 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
- 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
Add to your project manifiest by path [%UnityProject%]/Packages/manifiest.json new dependency:
{
"dependencies": {
"unigame.unityspreadsheets" : "https://github.com/UnioGame/UniGame.GoogleSpreadsheetsImporter.git",
}
}
{
"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"
}
}
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: