KaiClavier

STMAutoSplit.cs

Jul 14th, 2025 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.84 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. /*
  8.  * sets up STMPagination to a basic multiline format. This assumes you already have the script in your project.
  9.  * (It can be found in the sample scenes)
  10.  */
  11. #if UNITY_EDITOR
  12. [CustomEditor(typeof(STMAutoSplit))]
  13. public class STMAutoSplitEditor : Editor
  14. {
  15.     private STMAutoSplit autoSplit;
  16.     override public void OnInspectorGUI()
  17.     {
  18.         serializedObject.Update();
  19.         autoSplit = target as STMAutoSplit;
  20.        
  21.         EditorGUILayout.HelpBox("This script will set up multiple Super Text Mesh objects with pagination. \n\n" +
  22.                                 "If using autoLineCount and autoRebuildOnLineCountChange, new objects will" +
  23.                                 "be created and destroyed as text is edited. It is more efficient to set this" +
  24.                                 "up with the desired amount of lines, then disable/delete this component.\n\n" +
  25.                                 "Edit the text, width, and other properties on this initial Super Text Mesh component. \n\n" +
  26.                                 "Limitations: assumes each line has the same height. \n" +
  27.                                 "For UI text, set Rect Transform's pivot's Y value to 1 or text layout may shift.", MessageType.None, true);
  28.         if(GUILayout.Button("Manual Set Up"))
  29.         {
  30.             autoSplit.SetUp();
  31.             EditorUtility.SetDirty(target);
  32.         }
  33.         EditorGUILayout.PropertyField(serializedObject.FindProperty("stm"));
  34.         EditorGUILayout.PropertyField(serializedObject.FindProperty("autoLineCount"));
  35.         if(serializedObject.FindProperty("autoLineCount").boolValue)
  36.         {
  37.             EditorGUILayout.PropertyField(serializedObject.FindProperty("autoSetUpOnLineCountChange"));
  38.         }
  39.         else
  40.         {
  41.             EditorGUILayout.PropertyField(serializedObject.FindProperty("lineCount"));
  42.         }
  43.         EditorGUILayout.PropertyField(serializedObject.FindProperty("linesPerTextMesh"));
  44.         EditorGUILayout.PropertyField(serializedObject.FindProperty("stms"), true);
  45.  
  46.         serializedObject.ApplyModifiedProperties();
  47.     }
  48. }
  49. #endif
  50.  
  51. [ExecuteInEditMode]
  52. [RequireComponent(typeof(SuperTextMesh))]
  53. public class STMAutoSplit : MonoBehaviour
  54. {
  55.     public SuperTextMesh stm;
  56.     [Tooltip("If true, line count will be decided based on the text trying to fit in the first text mesh.")]
  57.     public bool autoLineCount = false;
  58.     [Tooltip("If true, SetUp() will be called if the amount of lines needed to fit the entire text mesh changes.")]
  59.     public bool autoSetUpOnLineCountChange = false;
  60.     [Tooltip("Amount of lines of text that will be displayed in total.")]
  61.     public int lineCount;
  62.     [Tooltip("How many lines an individual Super Text Mesh object will be configured to display.")]
  63.     public int linesPerTextMesh = 1;
  64.     //public float lineSpacing = 1f;
  65.    
  66.     public List<SuperTextMesh> stms = new List<SuperTextMesh>();
  67.  
  68.     private void Reset()
  69.     {
  70.         stm = GetComponent<SuperTextMesh>();
  71.     }
  72.  
  73.     private void OnValidate()
  74.     {
  75.         if(lineCount < 1) lineCount = 1;
  76.         if(linesPerTextMesh < 1) linesPerTextMesh = 1;
  77.     }
  78.  
  79.     public void OnEnable()
  80.     {
  81.         if(stm == null) return;
  82.         stm.OnRebuildEvent += OnRebuild;
  83.     }
  84.  
  85.     public void OnDisable()
  86.     {
  87.         if(stm == null) return;
  88.         stm.OnRebuildEvent -= OnRebuild;
  89.     }
  90.  
  91.     private void OnRebuild()
  92.     {
  93.         if(autoLineCount && lineCount != stm.lineHeights.Count)
  94.         {
  95.             SetUp();
  96.         }
  97.     }
  98.  
  99.     [ContextMenu("SetUp")]
  100.     public void SetUp()
  101.     {
  102.         if(autoLineCount)
  103.         {
  104.             lineCount = stm.lineHeights.Count;
  105.         }
  106.         if(GetComponent<STMPagination>() == null)
  107.         {
  108.             var page = stm.gameObject.AddComponent<STMPagination>();
  109.             page.originalText = stm;
  110.         }
  111.         //just create from scratch
  112.         stm.verticalLimitMode = SuperTextMesh.VerticalLimitMode.CutOff;
  113.         if(stm.uiMode && stm.uiLimit)
  114.         {
  115.             stm.tr.sizeDelta = new Vector2(stm.tr.sizeDelta.x, stm.size * linesPerTextMesh);
  116.         }
  117.         else
  118.         {
  119.             stm.verticalLimit = stm.size * linesPerTextMesh; //copy size
  120.         }
  121.  
  122.         foreach(var s in stms)
  123.         {
  124.             DestroyImmediate(s.gameObject);
  125.         }
  126.        
  127.         stms.Clear();
  128.  
  129.         var lineSpacing = stm.lineHeights[0];
  130.         //previous page...
  131.         var lastPage = stm.GetComponent<STMPagination>();
  132.         for(var i = linesPerTextMesh; i < lineCount; i += linesPerTextMesh)
  133.         {
  134.             var newPos = stm.t.position + (Vector3.down * (lineSpacing * i));
  135.             var newStm = Instantiate(stm, newPos, Quaternion.identity) as SuperTextMesh;
  136.             newStm.t.SetParent(stm.t.parent);
  137.             DestroyImmediate(newStm.gameObject.GetComponent<STMAutoSplit>());
  138.             var newPage = newStm.gameObject.GetComponent<STMPagination>();
  139.             lastPage.overflowText = newStm;
  140.             newPage.originalText = newStm;
  141.             newPage.overflowText = null; //clear overflow text until next loop
  142.             lastPage = newPage;
  143.             stms.Add(newStm);
  144.         }
  145.  
  146.         StartCoroutine(WaitFrameThenRebuild());
  147.     }
  148.     private IEnumerator WaitFrameThenRebuild()
  149.     {
  150.         yield return null;
  151.         //Rebuild(currentReadTime, reading ? true : autoRead);
  152.         stm.Rebuild();
  153.     }
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment