private void ApplyTextAlignmentToTRSMatrix(ref Matrix4x4 matrix, TextPivot pivot, Bounds textBounds) { var textHalfHeight = textBounds.extents.y; var textHalfWidth = textBounds.extents.x; if (pivot == TextPivot.BottomLeft) { TranslateMatrixInXY(ref matrix, textHalfWidth, textHalfHeight); } else if (pivot == TextPivot.BottomCenter) { TranslateMatrixInYAxis(ref matrix, textHalfHeight); } else if (pivot == TextPivot.BottomRight) { TranslateMatrixInXY(ref matrix, -textHalfWidth, textHalfHeight); } else if (pivot == TextPivot.CenterLeft) { TranslateMatrixInXAxis(ref matrix, textHalfWidth); } else if (pivot == TextPivot.CenterRight) { TranslateMatrixInXAxis(ref matrix, -textHalfWidth); } else if (pivot == TextPivot.TopLeft) { TranslateMatrixInXY(ref matrix, textHalfWidth, -textHalfHeight); } else if (pivot == TextPivot.TopCenter) { TranslateMatrixInYAxis(ref matrix, -textHalfHeight); } else if (pivot == TextPivot.TopRight) { TranslateMatrixInXY(ref matrix, -textHalfWidth, -textHalfHeight); } }
public static void DrawText(string text, float fontSize, Color color, Matrix4x4 mat, TMP_FontAsset font, TextPivot pivot = TextPivot.Center) { Instance.DrawTextInternal(text, fontSize, color, mat, font, pivot); }
private void DrawTextInternal(string text, float fontSize, Color color, Matrix4x4 mat, TMP_FontAsset font = null, TextPivot pivot = TextPivot.Center) { if (_materialPropertyBlockLastColorSet != color) { _materialPropertyBlockLastColorSet = color; _materialPropertyBlock.SetColor(_materialTextColorPropertyId, color); } if (font == null) { font = _defaultFontAsset; } //Since TMP generates meshes that, by default, face the -z direction, we need to rotate it by 180 degrees in the y axis RotateMatrix180DegreesInYAxis(ref mat); var textMesh = GenerateMeshForText(text, fontSize, font); ApplyTextAlignmentToTRSMatrix(ref mat, pivot, textMesh.bounds); Graphics.DrawMesh(textMesh, mat, font.material, 0, null, 0, _materialPropertyBlock); }