CCText

class : MonoBehaviour

CCText is a component that uses a mesh to display a box of text.

Variables

The public variables are typically only uses by CCTextModifier objects and only when the a modifier is called by the CCText object during an update. None of them are serialized, except mesh, but the mesh object itself is not saved.
minBounds
Vector3

Minimum bounds coordinates used for bounding the mesh, in local space units. Modifiers might want change this if they update vertices.

maxBounds
Vector3

Maximum bounds coordinates used for bounding the mesh, in local space units. Modifiers might want change this if they update vertices.

mesh
Mesh

Mesh used to display the text. Modifiers might want to access it.

vertices
Vector3[]

Vertices used to position character sprites. Modifiers might want to access these. Will be pushed to the mesh after the modifier has been called.

colors
Color[]

Colors used to color character sprites. Modifiers might want to access these. Will not be pushed to the mesh by default, that's up to the modifier.

uv
Vector2[]

UV coordinates used to texture the character sprites. Modifiers might want to access these. Will be pushed to the mesh after the modifier has been called.

triangles
int[]

Triangles used to define character sprites. Modifiers might want to access these, but probably not. Will not be pushed to the mesh by default.

Properties

Almost all of the properties trigger an update if set to a different value.
Indexer
char [int]

Get a character from the currently displayed text at some position. Uses StringBuilder if assigned, otherwise Text.

Alignment
CCText.AlignmentMode

Get or set the text alignment. Setting a different value triggers an update.

Bounding
CCText.BoundingMode

Get or set the bounding mode. Setting a different value triggers an update.

HorizontalAnchor
CCText.HorizontalAnchorMode

Get or set the horizontal anchor mode. Setting a different value triggers an update.

VerticalAnchor
CCText.VerticalAnchorMode

Get or set the vertical anchor mode. Setting a different value triggers an update.

CaretMinBounds
Vector3

Get the minimum bounds of the caret based on minBounds and font borders, in local space units. Will be incorrect if a modifier changed minBounds.

CaretMaxBounds
Vector3

Get the maximum bounds of the caret based on maxBounds and font borders, in local space units. Will be incorrect if a modifier changed maxBounds.

ChunkSize
int

Get or set the chunk sized used to increase SpriteCount, with a minimum of 1.

Color
Color

Get or set the color of the text. Setting a different value triggers a color reset and an update.

Font
CCFont

Get or set the font used to place text. No text is displayed if set to null or an invalid font. Setting a different value triggers an update.

Length
int

Get the length of the text currently being displayed. Uses StringBuilder if assigned, otherwise Text.

LineCount
int

Get the number of text lines currently being displayed.

LineHeight
float

Get or set the line height, in local space units, with a minimum of 0f. A height of 1f corresponds to the font's default line height. Setting a different value triggers an update.

LineWidth
float

Get the line width to which the caret is constrained. Might be the same as Width, depending on Width, Bounding, and Font.

Modifier
CCTextModifier

Get or set the modifier used to alter the text sprites. Can be null. Setting a different value triggers an update.

Offset
Vector3

Get or set the text offset relative to the anchor. Setting a different value triggers an update.

SpriteCount
int

Get how many sprites are currently allocated.

UsedSpriteCount
int

Get how many sprites are currently used to display characters.

StringBuilder
StringBuilder

Get or set the StringBuilder used to display text. Setting a different value triggers an update. Modifying the StringBuilder itself does not trigger an update.

If not null, will be used instead of Text.

Text
string

Get or set the string used to display text. Setting always triggers an update. Setting to null will result in the empty string.

Will be ignored if StringBuilder is not null.

TabSize
float

Get or set the tab size, in local space units, with a minimum of 0.001f. Setting a different value triggers an update.

Width
float

Get or set the width of the box containing the text, with a minimum of 0f. Setting a different value triggers an update.

Methods

HitCharacterIndex
int

Return the index of the character hit by a raycast, or -1 if no character was hit. This is a convenient alternative for the TriangleToCharacterIndex method.

Parameters

  1. hit
hit
RaycastHit

A successful raycast hit.

Example

using UnityEngine;

public class HitTest : MonoBehaviour {

	public CCText text;

	void Update () {
		if(!Input.GetMouseButtonDown(0)){
			return;
		}
		RaycastHit hit;
		if(Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit)){
			Debug.Log(text.HitCharacterIndex(hit));
		}
	}
}
ResetColors
void

Reset the colors of all character sprites. By default colors aren't updated.

You typically don't call this method explictly.

UpdateText
void

Update the text mesh. Is called automatically when setting most properties.

Typically, you only call this yourself after modifying the contents of a StringBuilder.

TriangleToCharacterIndex
int

Return the index of a character based on a triangle index. Return -1 if no matching character was found.

Parameters

  1. triangleIndex
triangleIndex
int

Index of a triangle.

Nested Types

AlignmentMode
enum

How to align text.

Values

Left
Align text to the left.
Center
Align text in the center.
Right
Align text to the right.
Justify
Justify text, stretching word-wrapped lines to fill the entire width.
BoundingMode
enum

How to bound text.

Values

Caret
Constrain the caret, allow text to go beyond the bounds.
Margin
Constrain text so it never goes beyond the bounds.
HorizontalAnchorMode
enum

How to anchor text horizontally.

Values

Left
Anchor at the left.
Center
Anchor at the center.
Right
Anchor at the right.
VerticalAnchorMode
enum

How to anchor text vertically.

Values

Top
Anchor at the top.
Middle
Anchor at the middle.
Bottom
Anchor at the bottom.
Baseline
Anchor at the baseline of the first line.

Example

using System.Text;
using UnityEngine;

public class CCTextExample : MonoBehaviour {

	public CCText textBox;

	public string[] names = {"Alfred", "Bob", "Charlie"};
	public int[] scores = {9999999, 700042, 9001};

	private StringBuilder sb;

	void Start () {
		sb = new  StringBuilder();
		textBox.StringBuilder = sb;
		UpdateHighScores();
	}

	public void UpdateHighScores () {
		sb.Length = 0;
		for(int i = 0; i < names.Length; i++){
			sb.Append(names[i]);
			sb.Append('\t');
			CCStringBuilderUtility.AppendIntGrouped(sb, scores[i], 9);
			sb.AppendLine();
		}
		textBox.UpdateText();
	}
}
The above code defines a component that contains high score information which it uses to update a CCText component.

You can add this component to any game object, including the CCText component, as long as you properly set its textBox property. It calls UpdateHighScores on start, but you can call it through scripting at any later time, for instance after high score data has changed.

The high score list will look reasonable if you use a monospace font, left align the text, and set a large enough tab size. If you want to make it very elaborate, it might be easiest to place two seperate text boxes next to each other instead of trying to combine it all into one box.