Guest User

BitmapFontTextRenderer.cs

a guest
May 4th, 2015
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.32 KB | None | 0 0
  1. // By Tim Volp
  2. // Updated 04/05/2015
  3.  
  4. using UnityEngine;
  5. using System.Collections;
  6.  
  7. [AddComponentMenu("UI/Bitmap Font Text")]
  8. [ExecuteInEditMode]
  9. public class BitmapFontTextRenderer : MonoBehaviour {
  10.     [Multiline] public string text = "";
  11.     //public float offsetZ = 0;
  12.     public float characterSize = 1;
  13.     public float lineSpacing = 1;
  14.     public TextAnchor anchor;
  15.     public TextAlignment alignment;
  16.     public float tabSize = 4;
  17.     public Texture font;
  18.     [Range(1, 256)]public int charactersPerRow = 16;
  19.     //public Color color;
  20.     public Space coordinateSystem;
  21.  
  22.     // Return the characters texture coordinates, assuming that the texture contains 16 equal columns and rows.
  23.     Rect TexCoords(char chr) {
  24.         float invColumns = 1f / (float)charactersPerRow;
  25.         float invRows = 1f / (float)(256 / charactersPerRow);
  26.  
  27.         return new Rect((chr % charactersPerRow) * invColumns,
  28.                         1 - Mathf.Floor(chr / charactersPerRow) * invRows - invRows,
  29.                         invColumns,
  30.                         invRows);
  31.     }
  32.  
  33.     void OnGUI() {
  34.         if (Event.current.type != EventType.Repaint || font == null) {
  35.             return;
  36.         }
  37.  
  38.         string[] lines = text.Split('\n');
  39.         float[] lineSize = new float[lines.Length]; // The size of each line including tab size.
  40.         Vector2 size = Vector2.zero; // The size of the text including tab size and line spacing.
  41.  
  42.         // Populate the line size and size variables.
  43.         for (int i = 0, j; i < lines.Length; i++) {
  44.             size.y += lineSpacing;
  45.  
  46.             // Parse each character of the line.
  47.             for (j = 0; j < lines[i].Length; j++) {
  48.                 if (lines[i][j] == '\t') {
  49.                     lineSize[i] += tabSize;
  50.                 } else {
  51.                     lineSize[i]++;
  52.                 }
  53.             }
  54.  
  55.             // Set the text width to the width of this line, if this line is wider.
  56.             if (lineSize[i] > size.x) {
  57.                 size.x = lineSize[i];
  58.             }
  59.         }
  60.  
  61.         Vector3 screenPosition = transform.position;
  62.  
  63.         // Position the text in world space rather than local screen space.
  64.         if (coordinateSystem == Space.World) {
  65.             screenPosition = Camera.main.WorldToScreenPoint(screenPosition);
  66.             screenPosition.y = Camera.main.pixelHeight - screenPosition.y;
  67.         }
  68.  
  69.         // Calculate the position and size of the first character.
  70.         Rect position = new Rect(   screenPosition.x * transform.lossyScale.x,
  71.                                     screenPosition.y * transform.lossyScale.y,
  72.                                     characterSize * transform.lossyScale.x,
  73.                                     characterSize * transform.lossyScale.y);
  74.  
  75.         // Horizontally anchor the text by repositioning the first character if applicable.
  76.         if (anchor == TextAnchor.UpperCenter || anchor == TextAnchor.MiddleCenter || anchor == TextAnchor.LowerCenter) {
  77.             position.x -= characterSize * size.x / 2 * transform.lossyScale.x;
  78.         } else if (anchor == TextAnchor.UpperRight || anchor == TextAnchor.MiddleRight || anchor == TextAnchor.LowerRight) {
  79.             position.x -= characterSize * size.x * transform.lossyScale.x;
  80.         }
  81.  
  82.         // Vertically anchor the text by repositioning the first character if applicable.
  83.         if (anchor == TextAnchor.MiddleLeft || anchor == TextAnchor.MiddleCenter || anchor == TextAnchor.MiddleRight) {
  84.             position.y -= characterSize * size.y / 2 * transform.lossyScale.y;
  85.         } else if (anchor == TextAnchor.LowerLeft || anchor == TextAnchor.LowerCenter || anchor == TextAnchor.LowerRight) {
  86.             position.y -= characterSize * size.y * transform.lossyScale.y;
  87.         }
  88.  
  89.         size.y = position.x; // Reuse variable to cache the horizontal position.
  90.  
  91.         // Draw the text line by line onto the screen.
  92.         for (int i = 0, j; i < lines.Length; i++) {
  93.             // Align the text by repositioning the first character of the line if applicable.
  94.             if (alignment == TextAlignment.Center) {
  95.                 position.x += characterSize * (size.x - lineSize[i]) / 2 * transform.lossyScale.x;
  96.             } else if (alignment == TextAlignment.Right) {
  97.                 position.x += characterSize * (size.x - lineSize[i]) * transform.lossyScale.x;
  98.             }
  99.  
  100.             // Process each character of the line.
  101.             for (j = 0; j < lines[i].Length; j++) {
  102.                 if (lines[i][j] == '\t') {
  103.                     position.x += characterSize * tabSize * transform.lossyScale.x;
  104.                 } else {
  105.                     GUI.DrawTextureWithTexCoords(   position,
  106.                                                     font,
  107.                                                     TexCoords((char)lines[i][j])); // Draw the character
  108.                     position.x += characterSize * transform.lossyScale.x;
  109.                 }
  110.             }
  111.  
  112.             position.y += characterSize * lineSpacing * transform.lossyScale.y;
  113.             position.x = size.y; // Reset the horizontal position to the cached value.
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment