docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Property TableProvider

    TableProvider

    Called when attempting to load a table, can be used to override the default table loading through Addressables in order to provide a custom table.

    Declaration
    public ITableProvider TableProvider { get; set; }
    Property Value
    Type Description
    ITableProvider
    Examples

    This example demonstrates how to use the ITableProvider to provide a custom String Table without using the Addressables system. This approach is particularly useful when you want to allow users to add third-party content, such as modding. The localization data could be loaded from an external file and then converted into a table at runtime.

    [Serializable]
    public class CustomTableProvider : ITableProvider
    {
        public string customTableCollectionName = "My Custom Table";
    
        public AsyncOperationHandle<TTable> ProvideTableAsync<TTable>(string tableCollectionName, Locale locale) where TTable : LocalizationTable
        {
            Debug.Log($"Requested {locale.LocaleName} {typeof(TTable).Name} with the name `{tableCollectionName}`.");
    
            // Provide a custom string table only with the name "My Custom Table".
            if (typeof(TTable) == typeof(StringTable) && tableCollectionName == customTableCollectionName)
            {
                // Create the table and its shared table data.
                var table = ScriptableObject.CreateInstance<StringTable>();
                table.SharedData = ScriptableObject.CreateInstance<SharedTableData>();
                table.SharedData.TableCollectionName = customTableCollectionName;
                table.LocaleIdentifier = locale.Identifier;
    
                // Add some values
                table.AddEntry("My Entry 1", "My localized value 1");
                table.AddEntry("My Entry 2", "My localized value 2");
    
                return Addressables.ResourceManager.CreateCompletedOperation(table as TTable, null);
            }
    
            // Fallback to default table loading.
            return default;
        }
    }
    public static class AssignCustomTableProviderExample
    {
        [MenuItem("Localization Samples/Assign Custom table provider")]
        public static void AssignTableProvider()
        {
            // Create an instance of the table provider.
            var provider = new CustomTableProvider();
    
            // A provider can be assigned to each database or the same provider can be shared between both.
            var settings = LocalizationEditorSettings.ActiveLocalizationSettings;
            settings.GetStringDatabase().TableProvider = provider;
            settings.GetAssetDatabase().TableProvider = provider;
    
            // Set dirty so the changes are saved.
            EditorUtility.SetDirty(settings);
        }
    }
    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)