///////////////////////////////////////////////////////////////////////////////
// build mesh
///////////////////////////////////////////////////////////////////////////////
// ------------------------------------------------------------------
// Desc: This only calculate result in one line
// ------------------------------------------------------------------
public static void BuildTextLine( Vector3[] _vertices, Vector2[] _uvs,
string _text,
Font _font,
int _lineHeight,
int _fontSize,
int _wordSpacing,
int _letterSpacing )
{
int cur_x = 0;
int cur_y = 0;
// yes, Unity's GetCharacterInfo have y problem, you should get lowest character j's y-offset adjust it.
CharacterInfo jCharInfo;
_font.RequestCharactersInTexture ( "j", _fontSize, FontStyle.Normal );
_font.GetCharacterInfo('j', out jCharInfo, _fontSize, FontStyle.Normal);
float ttf_offset = (_fontSize + jCharInfo.vert.yMax);
//
for ( int i = 0; i < _text.Length; ++i ) {
char cur_char = _text[i];
// NOTE: we skip new-line operation, since we believe this function only have one-line text
if ( cur_char == '\n' ) {
continue;
}
// generate mesh
CharacterInfo charInfo;
_font.GetCharacterInfo ( cur_char, out charInfo, _fontSize );
int idx = 4*i;
// build vertices
_vertices[idx + 0] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f);
_vertices[idx + 1] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMin + ttf_offset, 0.0f);
_vertices[idx + 2] = new Vector3(cur_x + charInfo.vert.xMax, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f);
_vertices[idx + 3] = new Vector3(cur_x + charInfo.vert.xMin, cur_y - charInfo.vert.yMax + ttf_offset, 0.0f);
// build uv
if ( charInfo.flipped ) {
_uvs[idx + 0] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin);
_uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax);
_uvs[idx + 2] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax);
_uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin);
}
else {
_uvs[idx + 0] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMax);
_uvs[idx + 1] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMax);
_uvs[idx + 2] = new Vector2(charInfo.uv.xMax, charInfo.uv.yMin);
_uvs[idx + 3] = new Vector2(charInfo.uv.xMin, charInfo.uv.yMin);
}
// advance x
cur_x += (int)charInfo.width + _letterSpacing;
if ( cur_char == ' ' )
cur_x += _wordSpacing;
}
}