docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Creating a custom formatter

    You can create a custom Formatter by inheriting from the FormatterBase class.

    To use a custom Formatter, add it to the Formatters list in the LocalizationSettings.

    Byte formatter example

    This example shows how to create a formatter to format an integer that represents bytes.

    using UnityEngine.Localization;
    using UnityEngine.Localization.SmartFormat.Core.Extensions;
    
    [DisplayName("Base 2 Byte Formatter")]
    public class ByteFormatter : FormatterBase
    {
        public override string[] DefaultNames => new string[] { "byte" };
    
        public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
        {
            if (formattingInfo.CurrentValue is long bytes)
            {
                // We are performing a Base 2 conversion here. 1024 bytes = 1 KB
                if (bytes < 512)
                {
                    formattingInfo.Write($"{bytes} B");
                    return true;
                }
    
                if (bytes < 512 * 1024)
                {
                    var kb = bytes / 1024.0f;
                    formattingInfo.Write($"{kb.ToString("0.00")} KB");
                    return true;
                }
    
                bytes /= 1024;
                if (bytes < 512 * 1024)
                {
                    var mb = bytes / 1024.0f;
                    formattingInfo.Write($"{mb.ToString("0.00")} MB");
                    return true;
                }
    
                bytes /= 1024;
                var gb = bytes / 1024.0f;
                formattingInfo.Write($"{gb.ToString("0.00")} GB");
                return true;
            }
    
            return false;
        }
    }
    
    Example Smart String Arguments Result
    The file size is {0:byte()} 100 The file size is 100 B
    1000 The file size is 0.98 KB
    1234 The file size is 1.21 KB
    10000000 The file size is 9.5 MB
    2000000000 The file size is 1.9 GB

    Color formatter example

    This example creates a formatter to turn a Color into an HTML string for use in rich text.

    using UnityEngine;
    using UnityEngine.Localization;
    using UnityEngine.Localization.SmartFormat.Core.Extensions;
    
    [DisplayName("Color Formatter")]
    public class ColorFormatter : FormatterBase
    {
        public override string[] DefaultNames => new string[] { "color" };
    
        public override bool TryEvaluateFormat(IFormattingInfo formattingInfo)
        {
            if (formattingInfo.CurrentValue is Color color)
            {
                formattingInfo.Write(ColorUtility.ToHtmlStringRGB(color));
                return true;
            }
            return false;
        }
    }
    
    Example Smart String Arguments Result
    This is <color=#{0:color()}>red</color> Color.red This is red
    In This Article
    Back to top
    Copyright © 2025 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)