Skip to content

Formattable Text System

Xeltica edited this page Mar 12, 2018 · 6 revisions

Formattable Text System(hereinafter called "FTS") is a function group to format the text in game such as NPC messages.

FTS includes 2 following elements.

Formatting Command

You can use commands starts with $ (dollar sign) to format.

Generally commands has the following syntax.

Syntax: $cmd=arg;

We will show commands to below.

Color

Syntax: $c=#rrggbbaa; $c=#rrggbb; $c=#rgba; $c=#rgb; $c=color_code;

You can specify a color-code of 8 or 4 digits of RGBA, a color-code of 6 or 3 digits of RGB or a HTML color name.

Bold

Syntax: $b;

No parameters.

Italic

Syntax: $i;

No parameters.

Size

Syntax: $sz=size;

You can specify the size of text by integer value.

Variable

Syntax: $var=name;

You can display the variable value. Also check Variables of formatting command.

Reset

Syntax: $r;

Reset all format.


The following commands are only supported by message window etc and ignored by other environments.

Wait

Syntax: $w=time;

Specify a waiting time by real numbers of unit of seconds.

Prompt

Syntax: $nod;

Wait until users press the message feed key.

Speed

Syntax: $spd=time;

Specify a speed multiplier by real numbers. The default is 1. Messages will be displayed in a moment if you specify 0 and values will be converted to the positive if you specify the negative.


We will show the example usage.

: "There is a dusty PC."
: "I turned on it."
: "$spd=0;A night in 1000 years. When  $b;$c=yellow;seven rays $!c$!b; overlap in the sky, people will see $b;the dream of miracles$!b;."
: "$spd=0;Source: The Mystery of The Seven-color Meteor Shower"

TextComponent

TextComponent is an C# implementation of FTS.

TextComponent class provides API to get formatted string data or formatted text object. You can use it by compiling the above commands in the constructor.

TextComponent Method

string ToString();

Return the string compiled to Unity-compatible rich text.

var text = new TextComponent("A night in 1000 years. When  $b;$c=yellow;seven rays $!c; overlap in the sky, people will see $b;the dream of miracles$!b;.");
Debug.Log(text.ToString());

static string Parse(string text);

Compile the specified command string and return unity-compatible it.

var uiText = GetComponent<Text>();
uiText.text = TextComponent.Parse("$c=orange;R$c=yellow;a$c=purple;i$c=#ff80ff;n$c=green;b$c=#ff88c8;o$c=red;w");

TextComponent Properties

TextElement[] Elements;

Get an array of compiled text elements.

uiText.text = "";
foreach (var te in tc.Elements)
{
    if (te.Nod)
        yield return new WaitUntil(UserTapped);
    if (te.Wait > 0)
        yield return new WaitForSecond(te.Speed);
    uiText.text += te.ToString();
    if (te.Speed > 0)
    yield return new WaitForSecond(speed * te.Speed);
}

TextElement

This class have text data which contains format etc in FTS.

Show the type definitions to below.

public struct TextElement
{
    public string Text { get; set; }
    public float WaitTime { get; set; }
    public bool Bold { get; set; }
    public bool Italic { get; set; }
    public int Size { get; set; }
    public bool Nod { get; set; }
    public float Speed { get; set; }
    public string Color { get; set; }

    public override string ToString() { /*·_·*/ }
}

There is a helper class to construct TextComponent safely and easily in C#.

TextComponentBuilder

(Not implemented)

You can use it to construct a TextComponent with method-chain.

Method

Method Description
TextComponentBuilder Text(string text); Set plain text.
TextComponentBuilder Bold(bool on = true); Set bold.
TextComponentBuilder Italic(bool on = true); Set italic.
TextComponentBuilder Color(UnityEngine.Color col); Set a color.
TextComponentBuilder Color(string col); Set a color.
TextComponentBuilder Size(int size); Set a size.
TextComponentBuilder Reset(); Reset all setting.
TextComponentBuilder Wait(float time); Set a waiting time.
TextComponentBuilder Nod(); Set nod point.
TextComponentBuilder Speed(float time); Set the display speed multiplier.
TextComponentBuilder Variable(string name); Embed a value of a variable.
TextComponent Finish(); Convert to TextComponent.

Example:

var text = new TextComponentBuilder()
           .Text("My name is")
           .Color(Color.red)
           .Bold()
           .Text("John")
           .Reset()
           .Text(".")
           .Finish(); // Create TextComponent

Clone this wiki locally