KaiClavier

STMRubyText.cs

Mar 6th, 2019 (edited)
224
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Copyright (c) 2019 Kai Clavier [kaiclavier.com] Do Not Distribute
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. /*
  6. Preparse STM, insert events at specific characters (H becomes <e=rt,aytch>H)
  7. If auto parse is off, manually type ruby text like "<e=rt,aytch>H" ("rt" means ruby text event, "aytch" is the ruby text to be shown.)
  8. these new events spawn temporary STM objects
  9.  */
  10. [RequireComponent(typeof(SuperTextMesh))]
  11. public class STMRubyText : MonoBehaviour {
  12.     private SuperTextMesh stm;
  13.     public bool autoParse = true;
  14.  
  15.     public void OnEnable()
  16.     {
  17.         //get stm component
  18.         stm = GetComponent<SuperTextMesh>();
  19.         //set up events
  20.         stm.OnCustomEvent += Event;
  21.         if(autoParse)
  22.         {
  23.             stm.OnPreParse += Parse;
  24.         }
  25.         stm.OnRebuildEvent += ClearRubyText;
  26.     }
  27.     public void OnDisable()
  28.     {
  29.         stm.OnCustomEvent -= Event;
  30.         if(autoParse)
  31.         {
  32.             stm.OnPreParse -= Parse;
  33.         }
  34.         stm.OnRebuildEvent -= ClearRubyText;
  35.     }
  36.     [System.Serializable]
  37.     public class Ruby
  38.     {
  39.         public char ch;
  40.         public string text;
  41.     }
  42.     public float verticalOffset = 0f;
  43.     public float rubyTextSize = 0.25f;
  44.     public Ruby[] ruby;
  45.     private List<SuperTextMesh> rubyText = new List<SuperTextMesh>();
  46.  
  47.     public void Parse(STMTextContainer x)
  48.     {
  49.         //go thru each letter, searching ruby
  50.         for(int i=0; i<x.text.Length; i++)
  51.         {
  52.             //search ruby to see if this letter is a match
  53.             for(int j=0; j<ruby.Length; j++)
  54.             {
  55.                 if(x.text[i] == ruby[j].ch)
  56.                 {
  57.                     x.text = x.text.Insert(i, "<e=rt," + ruby[j].text + ">");
  58.                     //skip over this new tag
  59.                     i += 7 + ruby[j].text.Length;
  60.                 }
  61.             }
  62.         }
  63.     }
  64.     private string[] split;
  65.     private SuperTextMesh tempStm;
  66.     private Vector3 tmpPos;
  67.     public void Event(string text, STMTextInfo info)
  68.     {
  69.         split = text.Split(',');
  70.         //if exactly two strings and first is the rt tag...
  71.         if(split.Length == 2 && split[0] == "rt")
  72.         {
  73.             //create a new STM, and assign the remaining text to it
  74.             tempStm = new GameObject().AddComponent<SuperTextMesh>();
  75.             tempStm.t.SetParent(this.transform); //parent it to this
  76.             tempStm.t.name = split[1];
  77.             //center above letter
  78.             tmpPos.x = info.Middle.x;
  79.             tmpPos.y = info.pos.y + info.size + verticalOffset;
  80.             tempStm.t.localPosition = tmpPos;
  81.  
  82.             tempStm.size = rubyTextSize;
  83.             tempStm.font = stm.font;
  84.             tempStm.color = stm.color;
  85.  
  86.             tempStm.anchor = TextAnchor.LowerCenter;
  87.             tempStm.alignment = SuperTextMesh.Alignment.Center; //center above letter
  88.             tempStm.autoWrap = info.RelativeWidth; //max width is letter width
  89.             tempStm.bestFit = SuperTextMesh.BestFitMode.OverLimit; //shrink to fit if it goes over
  90.  
  91.             tempStm.text = split[1];
  92.  
  93.             rubyText.Add(tempStm); //add to list
  94.         }
  95.     }
  96.     public void ClearRubyText()
  97.     {
  98.         //destroy gameobjects
  99.         for(int i=0; i<rubyText.Count; i++){
  100.             Destroy(rubyText[i].gameObject);
  101.         }
  102.         //clear list
  103.         rubyText.Clear();
  104.     }
  105. }
RAW Paste Data