docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class CsvColumns

    Synchronizes one or more CSV columns with localization data.

    Inheritance
    object
    CsvColumns
    KeyIdColumns
    LocaleColumns
    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
    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(SharedTableEntry, CsvReader). Use reader.GetFieldIndex("Column Header", isTryGet: true) to check for a column with a matching name.

    ReadEnd(StringTableCollection)

    Called when the CSV file has been imported. This can be used to perform any required cleanup.

    ReadRow(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.

    WriteBegin(StringTableCollection, CsvWriter)

    Called when writing the header row of the CSV file. Each column that will be written to during WriteRow(SharedTableEntry, IList<StringTableEntry>, CsvWriter) should have its column header written here using writer.WriteField("Header Title").

    WriteEnd(StringTableCollection)

    Called after the CSV file has been exported. This can be used to perform any required cleanup.

    WriteRow(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).

    In This Article
    Back to top
    Copyright © 2024 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)