Class CsvColumns
Synchronizes one or more CSV columns with localization data.
Namespace: UnityEditor .Localization.Plugins.CSV.Columns
Assembly: Unity.Localization.Editor.dll
Syntax
[Serializable]
public abstract class CsvColumns
Examples
This shows how synchronize custom IMetadata over multiple CSV columns. Note: To use CsvWriter and CsvReader you will need to add an Assembly Reference to Unity.Localization.ThirdParty.Editor.
[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
Name | Description |
---|---|
Read |
Called when reading a CSV file column header row.
This call should be used to determine which column indexes to use during Read |
Read |
Called when the CSV file has been imported. This can be used to perform any required cleanup. |
Read |
Called when reading each row of the CSV file. Unity recommends to store the indexes of the rows you are
interested in during Read |
Write |
Called when writing the header row of the CSV file.
Each column that will be written to during Write |
Write |
Called after the CSV file has been exported. This can be used to perform any required cleanup. |
Write |
Called when writing each row of the CSV file. This is called in the same order as Write |