Last active
December 31, 2015 05:38
-
-
Save hww/7941753 to your computer and use it in GitHub Desktop.
Dirty way add color tags like "Hello [FF0000]Wold[-]!" at the FLabel. Additionally this class have a 3D mesh builder. Which lets to make text attached to a 3D game object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using UnityEngine; | |
using System.Collections; | |
using System.Collections.Generic; | |
/* | |
* | |
* By: hww | |
* | |
* Use : | |
* | |
* // do not set string from constructor | |
* FLabelColorized label=new FLabelColorized(Config.fontFile,""); | |
* AddChild (label); | |
* | |
* // Use .richText instead the just .text | |
* label.richText = "Hello [FF0000]World[-]" | |
* | |
*/ | |
public class FLabelColorized : FLabel | |
{ | |
/// <summary> | |
/// Pair color and boolean flag | |
/// </summary> | |
public class CharColor | |
{ | |
/// <summary> | |
/// The color of character. | |
/// </summary> | |
public Color color; | |
/// <summary> | |
/// The character is colorized or used default color. | |
/// </summary> | |
public bool colorized; | |
public CharColor (bool colorized, Color color) | |
{ | |
this.colorized = colorized; | |
this.color = color; | |
} | |
} | |
/// <summary> | |
/// The rich text. | |
/// </summary> | |
string richText_; | |
/// <summary> | |
/// The list of character colors. Use just List or check the BetterList.cs at the NGUI | |
/// </summary> | |
BetterList<CharColor> characterColors; | |
public FLabelColorized (string fontName, string text):this(fontName, text, new FTextParams()) | |
{ | |
} | |
public FLabelColorized (string fontName, string text, FTextParams textParams):base(fontName, "", textParams) | |
{ | |
characterColors = new BetterList<CharColor> (); | |
richText = text; | |
} | |
public string richText { | |
get { return richText_; } | |
set { | |
if (value != richText_) { | |
richText_ = value; | |
TokenizeString(value); | |
} | |
} | |
} | |
private void TokenizeString (string text) | |
{ | |
characterColors.Clear (); | |
if (text.Length == 0) { | |
this.text = ""; | |
return; | |
} | |
bool colorized = false; | |
Color color = Color.white; | |
string str = ""; | |
for (int offset=0; offset<text.Length; offset++) { | |
char ch = text [offset]; | |
switch (ch) { | |
case '[': | |
if (offset + 2 < text.Length) { | |
if (text [offset + 1] == '-' && text [offset + 2] == ']') { | |
// colorization ends | |
offset += 2; | |
colorized = false; | |
color = Color.white; | |
continue; | |
} else if (offset + 7 < text.Length && text [offset + 7] == ']') { | |
// get color | |
color = RXUtils.GetColorFromHex (text.Substring (offset + 1, 6).ToUpper ()); | |
colorized = true; | |
offset += 7; | |
} | |
} | |
break; | |
case '\n': | |
str += ch; // do not create color entrie for the enter | |
break; | |
default: | |
str += ch; | |
characterColors.Add (new CharColor (colorized, color)); | |
break; | |
} | |
} | |
this.text = str; //< now lets change the text | |
_isAlphaDirty=true; | |
} | |
override public void PopulateRenderLayer () | |
{ | |
if (_isOnStage && _firstFacetIndex != -1) { | |
_isMeshDirty = false; | |
Vector3[] vertices = _renderLayer.vertices; | |
Vector2[] uvs = _renderLayer.uvs; | |
Color[] colors = _renderLayer.colors; | |
int vertexIndex0 = _firstFacetIndex * 4; | |
int vertexIndex1 = vertexIndex0 + 1; | |
int vertexIndex2 = vertexIndex0 + 2; | |
int vertexIndex3 = vertexIndex0 + 3; | |
int charIdx = 0; | |
int lineCount = _letterQuadLines.Length; | |
for (int i = 0; i<lineCount; i++) { | |
FLetterQuad[] quads = _letterQuadLines [i].quads; | |
int quadCount = quads.Length; | |
for (int q = 0; q<quadCount; q++) { | |
FLetterQuad quad = quads [q]; | |
FCharInfo charInfo = quad.charInfo; | |
_concatenatedMatrix.ApplyVector3FromLocalVector2 (ref vertices [vertexIndex0], quad.topLeft, 0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2 (ref vertices [vertexIndex1], quad.topRight, 0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2 (ref vertices [vertexIndex2], quad.bottomRight, 0); | |
_concatenatedMatrix.ApplyVector3FromLocalVector2 (ref vertices [vertexIndex3], quad.bottomLeft, 0); | |
uvs [vertexIndex0] = charInfo.uvTopLeft; | |
uvs [vertexIndex1] = charInfo.uvTopRight; | |
uvs [vertexIndex2] = charInfo.uvBottomRight; | |
uvs [vertexIndex3] = charInfo.uvBottomLeft; | |
if (characterColors[charIdx].colorized) { | |
//shown | |
colors [vertexIndex0] = characterColors[charIdx].color; | |
colors [vertexIndex1] = characterColors[charIdx].color; | |
colors [vertexIndex2] = characterColors[charIdx].color; | |
colors [vertexIndex3] = characterColors[charIdx].color; | |
} else { | |
//not shown | |
colors [vertexIndex0] = _alphaColor; | |
colors [vertexIndex1] = _alphaColor; | |
colors [vertexIndex2] = _alphaColor; | |
colors [vertexIndex3] = _alphaColor; | |
} | |
vertexIndex0 += 4; | |
vertexIndex1 += 4; | |
vertexIndex2 += 4; | |
vertexIndex3 += 4; | |
charIdx++; | |
} | |
} | |
_renderLayer.HandleVertsChange (); | |
} | |
} | |
/* | |
* The next function lets generate 3D text mesh | |
* | |
* Label3D label = new Label3D("Tondu", ); | |
* label.richText = "Test Text"; | |
* label.UpdateMesh(mesh); | |
* | |
*/ | |
BetterList<Vector3> vertices = new BetterList<Vector3>(); | |
BetterList<Vector2> uvs = new BetterList<Vector2>(); | |
BetterList<Color> colors = new BetterList<Color>(); | |
BetterList<int> triangles = new BetterList<int>(); | |
public void UpdateMesh(Mesh mesh) { | |
vertices.Clear(); | |
uvs.Clear(); | |
colors.Clear(); | |
triangles.Clear(); | |
int vertexIndex = 0; | |
int charIdx = 0; | |
int lineCount = _letterQuadLines.Length; | |
for (int i = 0; i<lineCount; i++) { | |
FLetterQuad[] quads = _letterQuadLines [i].quads; | |
int quadCount = quads.Length; | |
for (int q = 0; q<quadCount; q++) { | |
FLetterQuad quad = quads [q]; | |
FCharInfo charInfo = quad.charInfo; | |
Vector2 tl = quad.topLeft; tl.x *= -1; | |
Vector2 tr = quad.topRight; tr.x *= -1; | |
Vector2 br = quad.bottomRight; br.x *= -1; | |
Vector2 bl = quad.bottomLeft; bl.x *= -1; | |
vertices.Add(tl); | |
vertices.Add(tr); | |
vertices.Add(br); | |
vertices.Add(bl); | |
uvs.Add(charInfo.uvTopLeft); | |
uvs.Add(charInfo.uvTopRight); | |
uvs.Add(charInfo.uvBottomRight); | |
uvs.Add(charInfo.uvBottomLeft); | |
if (characterColors[charIdx].colorized) { | |
//shown | |
Color c = characterColors[charIdx].color; | |
colors.Add(c); | |
colors.Add(c); | |
colors.Add(c); | |
colors.Add(c); | |
} else { | |
//not shown | |
Color c = _alphaColor; | |
colors.Add(c); | |
colors.Add(c); | |
colors.Add(c); | |
colors.Add(c); | |
} | |
triangles.Add(vertexIndex); | |
triangles.Add(vertexIndex+1); | |
triangles.Add(vertexIndex+2); | |
triangles.Add(vertexIndex+0); | |
triangles.Add(vertexIndex+2); | |
triangles.Add(vertexIndex+3); | |
vertexIndex+=4; | |
charIdx++; | |
} | |
} | |
mesh.vertices = vertices.ToArray(); | |
mesh.colors = colors.ToArray(); | |
mesh.uv = uvs.ToArray(); | |
mesh.triangles = triangles.ToArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment