Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By Tim Volp
- // Updated 04/05/2015
- using UnityEngine;
- using System.Collections;
- [AddComponentMenu("UI/Bitmap Font Text")]
- [ExecuteInEditMode]
- public class BitmapFontTextRenderer : MonoBehaviour {
- [Multiline] public string text = "";
- //public float offsetZ = 0;
- public float characterSize = 1;
- public float lineSpacing = 1;
- public TextAnchor anchor;
- public TextAlignment alignment;
- public float tabSize = 4;
- public Texture font;
- [Range(1, 256)]public int charactersPerRow = 16;
- //public Color color;
- public Space coordinateSystem;
- // Return the characters texture coordinates, assuming that the texture contains 16 equal columns and rows.
- Rect TexCoords(char chr) {
- float invColumns = 1f / (float)charactersPerRow;
- float invRows = 1f / (float)(256 / charactersPerRow);
- return new Rect((chr % charactersPerRow) * invColumns,
- 1 - Mathf.Floor(chr / charactersPerRow) * invRows - invRows,
- invColumns,
- invRows);
- }
- void OnGUI() {
- if (Event.current.type != EventType.Repaint || font == null) {
- return;
- }
- string[] lines = text.Split('\n');
- float[] lineSize = new float[lines.Length]; // The size of each line including tab size.
- Vector2 size = Vector2.zero; // The size of the text including tab size and line spacing.
- // Populate the line size and size variables.
- for (int i = 0, j; i < lines.Length; i++) {
- size.y += lineSpacing;
- // Parse each character of the line.
- for (j = 0; j < lines[i].Length; j++) {
- if (lines[i][j] == '\t') {
- lineSize[i] += tabSize;
- } else {
- lineSize[i]++;
- }
- }
- // Set the text width to the width of this line, if this line is wider.
- if (lineSize[i] > size.x) {
- size.x = lineSize[i];
- }
- }
- Vector3 screenPosition = transform.position;
- // Position the text in world space rather than local screen space.
- if (coordinateSystem == Space.World) {
- screenPosition = Camera.main.WorldToScreenPoint(screenPosition);
- screenPosition.y = Camera.main.pixelHeight - screenPosition.y;
- }
- // Calculate the position and size of the first character.
- Rect position = new Rect( screenPosition.x * transform.lossyScale.x,
- screenPosition.y * transform.lossyScale.y,
- characterSize * transform.lossyScale.x,
- characterSize * transform.lossyScale.y);
- // Horizontally anchor the text by repositioning the first character if applicable.
- if (anchor == TextAnchor.UpperCenter || anchor == TextAnchor.MiddleCenter || anchor == TextAnchor.LowerCenter) {
- position.x -= characterSize * size.x / 2 * transform.lossyScale.x;
- } else if (anchor == TextAnchor.UpperRight || anchor == TextAnchor.MiddleRight || anchor == TextAnchor.LowerRight) {
- position.x -= characterSize * size.x * transform.lossyScale.x;
- }
- // Vertically anchor the text by repositioning the first character if applicable.
- if (anchor == TextAnchor.MiddleLeft || anchor == TextAnchor.MiddleCenter || anchor == TextAnchor.MiddleRight) {
- position.y -= characterSize * size.y / 2 * transform.lossyScale.y;
- } else if (anchor == TextAnchor.LowerLeft || anchor == TextAnchor.LowerCenter || anchor == TextAnchor.LowerRight) {
- position.y -= characterSize * size.y * transform.lossyScale.y;
- }
- size.y = position.x; // Reuse variable to cache the horizontal position.
- // Draw the text line by line onto the screen.
- for (int i = 0, j; i < lines.Length; i++) {
- // Align the text by repositioning the first character of the line if applicable.
- if (alignment == TextAlignment.Center) {
- position.x += characterSize * (size.x - lineSize[i]) / 2 * transform.lossyScale.x;
- } else if (alignment == TextAlignment.Right) {
- position.x += characterSize * (size.x - lineSize[i]) * transform.lossyScale.x;
- }
- // Process each character of the line.
- for (j = 0; j < lines[i].Length; j++) {
- if (lines[i][j] == '\t') {
- position.x += characterSize * tabSize * transform.lossyScale.x;
- } else {
- GUI.DrawTextureWithTexCoords( position,
- font,
- TexCoords((char)lines[i][j])); // Draw the character
- position.x += characterSize * transform.lossyScale.x;
- }
- }
- position.y += characterSize * lineSpacing * transform.lossyScale.y;
- position.x = size.y; // Reset the horizontal position to the cached value.
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment