Class CsvColumns
Synchronizes one or more CSV columns with localization data.
Namespace: UnityEditor.Localization.Plugins.CSV.Columns
Syntax
[Serializable]
public abstract class CsvColumns
Examples
This shows how synchronize custom IMetadata over multiple CSV columns.
[Serializable]
[DisplayName("Custom Data")]
[Metadata(AllowedTypes = MetadataType.StringTableEntry)]
public class MyMetadata : IMetadata
{
public string someValue;
public int someOtherValue;
}
public class CsvCustomColumnsExample : CsvColumns
{
[SerializeField] LocaleIdentifier m_LocaleIdentifier;
int m_SomeValueIndex, m_SomeOtherValueIndex, m_CollectionTableIndex;
StringTable m_ImportTable;
string SomeValueFieldName => m_LocaleIdentifier + " Some Value";
string SomeOtherValueFieldName => m_LocaleIdentifier + " Some Other Value";
public override void ReadBegin(StringTableCollection collection, CsvReader reader)
{
m_ImportTable = collection.GetTable(m_LocaleIdentifier) as StringTable;
if (m_ImportTable != null)
{
m_SomeValueIndex = reader.GetFieldIndex(SomeValueFieldName, isTryGet: true);
m_SomeOtherValueIndex = reader.GetFieldIndex(SomeOtherValueFieldName, isTryGet: true);
}
}
public override void ReadRow(SharedTableData.SharedTableEntry keyEntry, CsvReader reader)
{
if (m_ImportTable == null)
return;
// Get the entry or create one
StringTableEntry entry = m_ImportTable.GetEntry(keyEntry.Id) ?? m_ImportTable.AddEntry(keyEntry.Id, string.Empty);
// Get the metadata or add one
var metadata = entry.GetMetadata<MyMetadata>();
if (metadata == null)
{
metadata = new MyMetadata();
entry.AddMetadata(metadata);
}
if (m_SomeValueIndex != -1)
{
metadata.someValue = reader.GetField(m_SomeValueIndex);
}
if (m_SomeOtherValueIndex != -1)
{
metadata.someOtherValue = reader.GetField<int>(m_SomeOtherValueIndex);
}
}
public override void WriteBegin(StringTableCollection collection, CsvWriter writer)
{
// Does the collection contain a string table for our Locale Id?
var tables = collection.StringTables;
m_CollectionTableIndex = -1;
for (int i = 0; i < tables.Count; ++i)
{
if (tables[i].LocaleIdentifier == m_LocaleIdentifier)
{
m_CollectionTableIndex = i;
}
}
if (m_CollectionTableIndex != -1)
{
writer.WriteField(SomeValueFieldName);
writer.WriteField(SomeOtherValueFieldName);
}
}
public override void WriteRow(SharedTableData.SharedTableEntry keyEntry, IList<StringTableEntry> tableEntries, CsvWriter writer)
{
if (m_CollectionTableIndex != -1 && tableEntries[m_CollectionTableIndex] != null)
{
var entry = tableEntries[m_CollectionTableIndex];
var metadata = entry.GetMetadata<MyMetadata>();
if (metadata != null)
{
writer.WriteField(metadata.someValue, true);
writer.WriteField(metadata.someOtherValue);
return;
}
}
// Write empty entries
writer.WriteField(string.Empty);
writer.WriteField(0);
}
}
Methods
ReadBegin(StringTableCollection, CsvReader)
Called when reading a CSV file column header row.
This call should be used to determine which column indexes to use during ReadRow(SharedTableData.SharedTableEntry, CsvReader).
Use reader.GetFieldIndex("Column Header", isTryGet: true)
to check for a column with a matching name.
Declaration
public abstract void ReadBegin(StringTableCollection collection, CsvReader csvReader)
Parameters
Type | Name | Description |
---|---|---|
StringTableCollection | collection | The target collection the CSV file is being imported into. |
CsvReader | csvReader | Provides acces to the CSV file. |
ReadEnd(StringTableCollection)
Called when the CSV file has been imported. This can be used to perform any required cleanup.
Declaration
public virtual void ReadEnd(StringTableCollection collection)
Parameters
Type | Name | Description |
---|---|---|
StringTableCollection | collection | The target collection the CSV file was imported into. |
ReadRow(SharedTableData.SharedTableEntry, CsvReader)
Called when reading each row of the CSV file. Unity recommends to store the indexes of the rows you are
interested in during ReadBegin(StringTableCollection, CsvReader) and then use them to access the row using reader.GetField
.
Declaration
public abstract void ReadRow(SharedTableData.SharedTableEntry keyEntry, CsvReader reader)
Parameters
Type | Name | Description |
---|---|---|
SharedTableData.SharedTableEntry | keyEntry | The current entry being read. |
CsvReader | reader | Provides read access to the CSV file. |
WriteBegin(StringTableCollection, CsvWriter)
Called when writing the header row of the CSV file.
Each column that will be written to during WriteRow(SharedTableData.SharedTableEntry, IList<StringTableEntry>, CsvWriter) should have
its column header written here using writer.WriteField("Header Title")
.
Declaration
public abstract void WriteBegin(StringTableCollection collection, CsvWriter csvWriter)
Parameters
Type | Name | Description |
---|---|---|
StringTableCollection | collection | The collection being exported. |
CsvWriter | csvWriter | Provides access to write to a CSV file. |
WriteEnd(StringTableCollection)
Called after the CSV file has been exported. This can be used to perform any required cleanup.
Declaration
public virtual void WriteEnd(StringTableCollection collection)
Parameters
Type | Name | Description |
---|---|---|
StringTableCollection | collection | The target collection the CSV file was exported from. |
WriteRow(SharedTableData.SharedTableEntry, IList<StringTableEntry>, CsvWriter)
Called when writing each row of the CSV file. This is called in the same order as WriteBegin(StringTableCollection, CsvWriter) and expects each
column that was written to be populated each time. If a column will not always contain a value, you can use writer.WriteField(string.Empty)
.
Declaration
public abstract void WriteRow(SharedTableData.SharedTableEntry keyEntry, IList<StringTableEntry> tableEntries, CsvWriter writer)
Parameters
Type | Name | Description |
---|---|---|
SharedTableData.SharedTableEntry | keyEntry | The shared table data for the current entry that is being written to the row. |
IList<StringTableEntry> | tableEntries | The locale specific table entries for this row. |
CsvWriter | writer | Provides write acces to the CSV file. |