Skip to content

Instantly share code, notes, and snippets.

@anchan828
Created November 27, 2012 18:29
Show Gist options
  • Save anchan828/4156069 to your computer and use it in GitHub Desktop.
Save anchan828/4156069 to your computer and use it in GitHub Desktop.
RichTextUtilityのExample
using UnityEngine;
public class Example : MonoBehaviour
{
void OnGUI ()
{
GUIStyle style = GUIStyle.none;
{
style.normal.textColor = Color.white;
style.richText = true;
}
{
// We are <b>not</b> amused
GUILayout.Label (new RichTextUtility ("We are ").AppendBold ("not ").Append ("amused").ToString (), style);
// nWe are <b><i>definitely not</i></b> amused
GUILayout.Label (new RichTextUtility ("We are ").BeginBold ().AppendItalic ("definitely not ").EndBold ().Append ("amused").ToString (), style);
//We are <b>absolutely <i>definitely</i> not</b> amused
GUILayout.Label (new RichTextUtility ("We are ").BeginBold ().Append ("absolutely ").AppendItalic ("definitely ").Append ("not ").EndBold ().Append ("amused").ToString (), style);
//We are <size=50>largely</size> unaffected
GUILayout.Label (new RichTextUtility ("We are ").AppendSize ("largely ", 50).Append ("unaffected").ToString (), style);
//We are <color=green>green</color> with envy
GUILayout.Label (new RichTextUtility ("We are ").AppendColor ("green ", RichTextUtility.Color.green).Append ("with envy").ToString (), style);
}
{
//<color=red>red</color>
GUILayout.Label (new RichTextUtility ().AppendColor ("red", RichTextUtility.Color.red).ToString (), style);
//<color=#ff0000ff>red</color>
GUILayout.Label (new RichTextUtility ().AppendColor ("red", "#ff0000ff").ToString (), style);
}
{
// begin / end
GUILayout.Label (new RichTextUtility ().BeginBold ().Append ("bold").EndBold ().ToString (), style);
GUILayout.Label (new RichTextUtility ().BeginBold ().BeginColor (RichTextUtility.Color.red).BeginItalic ().Append ("text").EndItalic ().EndColor ().EndBold ().ToString (), style);
}
}
}
/// <summary>
/// Rich text utility.
/// </summary>
public class RichTextUtility
{
System.Text.StringBuilder sb = new System.Text.StringBuilder ();
/// <summary>
/// Initializes a new instance of the <see cref="RichTextUtility"/> class.
/// </summary>
public RichTextUtility ()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="RichTextUtility"/> class.
/// </summary>
/// <param name='text'>
/// Text.
/// </param>
public RichTextUtility (string text)
{
sb.Append (text);
}
/// <summary>
/// Append the specified text.
/// </summary>
/// <param name='text'>
/// Text.
/// </param>
public RichTextUtility Append (string text)
{
sb.Append (text);
return this;
}
/// <summary>
/// Appends the bold text.
/// </summary>
/// <returns>
/// The bold.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
public RichTextUtility AppendBold (string text)
{
sb.AppendFormat ("<b>{0}</b>", text);
return this;
}
/// <summary>
/// Appends the italic text.
/// </summary>
/// <returns>
/// The italic.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
public RichTextUtility AppendItalic (string text)
{
sb.AppendFormat ("<i>{0}</i>", text);
return this;
}
/// <summary>
/// Appends the size.
/// </summary>
/// <returns>
/// The size.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
/// <param name='size'>
/// Size.
/// </param>
public RichTextUtility AppendSize (string text, int size)
{
sb.AppendFormat ("<size={0}>{1}</size>", size, text);
return this;
}
/// <summary>
/// Appends the color.
/// </summary>
/// <returns>
/// The color.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
/// <param name='color'>
/// Color.
/// </param>
public RichTextUtility AppendColor (string text, RichTextUtility.Color color)
{
sb.AppendFormat ("<color={0}>{1}</color>", color.ToString(), text);
return this;
}
/// <summary>
/// Appends the color.
/// </summary>
/// <returns>
/// The color.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
/// <param name='hex'>
/// Hex.
/// </param>
public RichTextUtility AppendColor (string text, string hex)
{
sb.AppendFormat ("<color={0}>{1}</color>", hex, text);
return this;
}
/// <summary>
/// Appends the material.
/// </summary>
/// <returns>
/// The material.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
/// <param name='index'>
/// Index.
/// </param>
public RichTextUtility AppendMaterial (string text, int index)
{
sb.AppendFormat ("<material={0}>{1}</material>", index, text);
return this;
}
/// <summary>
/// Appends the quad.
/// </summary>
/// <returns>
/// The quad.
/// </returns>
/// <param name='text'>
/// Text.
/// </param>
/// <param name='material'>
/// Material.
/// </param>
/// <param name='size'>
/// Size.
/// </param>
/// <param name='x'>
/// X.
/// </param>
/// <param name='y'>
/// Y.
/// </param>
/// <param name='width'>
/// Width.
/// </param>
/// <param name='height'>
/// Height.
/// </param>
public RichTextUtility AppendQuad (string text, int material, int size, float x, float y, float width, float height)
{
sb.AppendFormat ("<quad material={0} size={1} x={2} y={3} width={4} height={5}/>", material, size, x, y, width, height);
return this;
}
/// <summary>
/// Begins the bold.
/// </summary>
/// <returns>
/// The bold.
/// </returns>
public RichTextUtility BeginBold ()
{
sb.Append ("<b>");
return this;
}
/// <summary>
/// Ends the bold.
/// </summary>
/// <returns>
/// The bold.
/// </returns>
public RichTextUtility EndBold ()
{
sb.Append ("</b>");
return this;
}
/// <summary>
/// Begins the italic.
/// </summary>
/// <returns>
/// The italic.
/// </returns>
public RichTextUtility BeginItalic ()
{
sb.Append ("<i>");
return this;
}
/// <summary>
/// Ends the italic.
/// </summary>
/// <returns>
/// The italic.
/// </returns>
public RichTextUtility EndItalic ()
{
sb.Append ("</i>");
return this;
}
/// <summary>
/// Begins the size.
/// </summary>
/// <returns>
/// The size.
/// </returns>
/// <param name='size'>
/// Size.
/// </param>
public RichTextUtility BeginSize (int size)
{
sb.AppendFormat ("<size={0}>", size);
return this;
}
/// <summary>
/// Ends the size.
/// </summary>
/// <returns>
/// The size.
/// </returns>
public RichTextUtility EndSize ()
{
sb.Append ("</size>");
return this;
}
/// <summary>
/// Begins the color.
/// </summary>
/// <returns>
/// The color.
/// </returns>
/// <param name='color'>
/// Color.
/// </param>
public RichTextUtility BeginColor (RichTextUtility.Color color)
{
sb.AppendFormat ("<color={0}>", color.ToString());
return this;
}
/// <summary>
/// Begins the color.
/// </summary>
/// <returns>
/// The color.
/// </returns>
/// <param name='hex'>
/// Hex.
/// </param>
public RichTextUtility BeginColor (string hex)
{
sb.AppendFormat ("<color={0}>", hex);
return this;
}
/// <summary>
/// Ends the color.
/// </summary>
/// <returns>
/// The color.
/// </returns>
public RichTextUtility EndColor ()
{
sb.Append ("</color>");
return this;
}
/// <summary>
/// Begins the material.
/// </summary>
/// <returns>
/// The material.
/// </returns>
/// <param name='index'>
/// Index.
/// </param>
public RichTextUtility BeginMaterial (int index)
{
sb.AppendFormat ("<material={0}>", index);
return this;
}
/// <summary>
/// Ends the material.
/// </summary>
/// <returns>
/// The material.
/// </returns>
public RichTextUtility EndMaterial ()
{
sb.Append ("</material>");
return this;
}
/// <summary>
/// Color.
/// </summary>
public enum Color
{
aqua,
black,
blue,
brown,
cyan,
darkblue,
fuchsia,
green,
grey,
lightblue,
lime,
magenta,
maroon,
navy,
olive,
orange,
purple,
red,
silver,
teal,
white,
yellow
}
/// <summary>
/// Returns a <see cref="System.String"/> that represents the current <see cref="RichTextUtility"/>.
/// </summary>
/// <returns>
/// A <see cref="System.String"/> that represents the current <see cref="RichTextUtility"/>.
/// </returns>
public override string ToString ()
{
return sb.ToString ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment