docs.unity3d.com
    Show / Hide Table of Contents

    Class FormatterBase

    Base class that implements common IFormatter functionality.

    Inheritance
    Object
    FormatterBase
    ChooseFormatter
    ConditionalFormatter
    DefaultFormatter
    IsMatchFormatter
    ListFormatter
    PluralLocalizationFormatter
    SubStringFormatter
    TemplateFormatter
    TimeFormatter
    XElementFormatter
    Namespace: UnityEngine.Localization.SmartFormat.Core.Extensions
    Syntax
    public abstract class FormatterBase : object, IFormatter, ISerializationCallbackReceiver
    Examples

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

    [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;
    }
    }

    Properties

    DefaultNames

    Default names to use when Names is null.

    Declaration
    public abstract string[] DefaultNames { get; }
    Property Value
    Type Description
    String[]

    Names

    An extension can be explicitly called by using any of its names. Any extensions with "" names will be called implicitly (when no named formatter is specified). For example, "{0:default:N2}" or "{0:d:N2}" will explicitly call the "default" extension. "{0:N2}" will implicitly call the "default" extension (and other extensions, too).

    Declaration
    public string[] Names { get; set; }
    Property Value
    Type Description
    String[]
    Implements
    IFormatter.Names

    Methods

    TryEvaluateFormat(IFormattingInfo)

    Writes the current value to the output, using the specified format. IF this extension cannot write the value, returns false, otherwise true.

    Declaration
    public abstract bool TryEvaluateFormat(IFormattingInfo formattingInfo)
    Parameters
    Type Name Description
    IFormattingInfo formattingInfo
    Returns
    Type Description
    Boolean
    Implements
    IFormatter.TryEvaluateFormat(IFormattingInfo)
    In This Article
    • Properties
      • DefaultNames
      • Names
    • Methods
      • TryEvaluateFormat(IFormattingInfo)
    Back to top
    Copyright © 2023 Unity Technologies — Terms of use
    • Legal
    • Privacy Policy
    • Cookies
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)
    "Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
    Generated by DocFX on 18 October 2023