Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- /*
- * sets up STMPagination to a basic multiline format. This assumes you already have the script in your project.
- * (It can be found in the sample scenes)
- */
- #if UNITY_EDITOR
- [CustomEditor(typeof(STMAutoSplit))]
- public class STMAutoSplitEditor : Editor
- {
- private STMAutoSplit autoSplit;
- override public void OnInspectorGUI()
- {
- serializedObject.Update();
- autoSplit = target as STMAutoSplit;
- EditorGUILayout.HelpBox("This script will set up multiple Super Text Mesh objects with pagination. \n\n" +
- "If using autoLineCount and autoRebuildOnLineCountChange, new objects will" +
- "be created and destroyed as text is edited. It is more efficient to set this" +
- "up with the desired amount of lines, then disable/delete this component.\n\n" +
- "Edit the text, width, and other properties on this initial Super Text Mesh component. \n\n" +
- "Limitations: assumes each line has the same height. \n" +
- "For UI text, set Rect Transform's pivot's Y value to 1 or text layout may shift.", MessageType.None, true);
- if(GUILayout.Button("Manual Set Up"))
- {
- autoSplit.SetUp();
- EditorUtility.SetDirty(target);
- }
- EditorGUILayout.PropertyField(serializedObject.FindProperty("stm"));
- EditorGUILayout.PropertyField(serializedObject.FindProperty("autoLineCount"));
- if(serializedObject.FindProperty("autoLineCount").boolValue)
- {
- EditorGUILayout.PropertyField(serializedObject.FindProperty("autoSetUpOnLineCountChange"));
- }
- else
- {
- EditorGUILayout.PropertyField(serializedObject.FindProperty("lineCount"));
- }
- EditorGUILayout.PropertyField(serializedObject.FindProperty("linesPerTextMesh"));
- EditorGUILayout.PropertyField(serializedObject.FindProperty("stms"), true);
- serializedObject.ApplyModifiedProperties();
- }
- }
- #endif
- [ExecuteInEditMode]
- [RequireComponent(typeof(SuperTextMesh))]
- public class STMAutoSplit : MonoBehaviour
- {
- public SuperTextMesh stm;
- [Tooltip("If true, line count will be decided based on the text trying to fit in the first text mesh.")]
- public bool autoLineCount = false;
- [Tooltip("If true, SetUp() will be called if the amount of lines needed to fit the entire text mesh changes.")]
- public bool autoSetUpOnLineCountChange = false;
- [Tooltip("Amount of lines of text that will be displayed in total.")]
- public int lineCount;
- [Tooltip("How many lines an individual Super Text Mesh object will be configured to display.")]
- public int linesPerTextMesh = 1;
- //public float lineSpacing = 1f;
- public List<SuperTextMesh> stms = new List<SuperTextMesh>();
- private void Reset()
- {
- stm = GetComponent<SuperTextMesh>();
- }
- private void OnValidate()
- {
- if(lineCount < 1) lineCount = 1;
- if(linesPerTextMesh < 1) linesPerTextMesh = 1;
- }
- public void OnEnable()
- {
- if(stm == null) return;
- stm.OnRebuildEvent += OnRebuild;
- }
- public void OnDisable()
- {
- if(stm == null) return;
- stm.OnRebuildEvent -= OnRebuild;
- }
- private void OnRebuild()
- {
- if(autoLineCount && lineCount != stm.lineHeights.Count)
- {
- SetUp();
- }
- }
- [ContextMenu("SetUp")]
- public void SetUp()
- {
- if(autoLineCount)
- {
- lineCount = stm.lineHeights.Count;
- }
- if(GetComponent<STMPagination>() == null)
- {
- var page = stm.gameObject.AddComponent<STMPagination>();
- page.originalText = stm;
- }
- //just create from scratch
- stm.verticalLimitMode = SuperTextMesh.VerticalLimitMode.CutOff;
- if(stm.uiMode && stm.uiLimit)
- {
- stm.tr.sizeDelta = new Vector2(stm.tr.sizeDelta.x, stm.size * linesPerTextMesh);
- }
- else
- {
- stm.verticalLimit = stm.size * linesPerTextMesh; //copy size
- }
- foreach(var s in stms)
- {
- DestroyImmediate(s.gameObject);
- }
- stms.Clear();
- var lineSpacing = stm.lineHeights[0];
- //previous page...
- var lastPage = stm.GetComponent<STMPagination>();
- for(var i = linesPerTextMesh; i < lineCount; i += linesPerTextMesh)
- {
- var newPos = stm.t.position + (Vector3.down * (lineSpacing * i));
- var newStm = Instantiate(stm, newPos, Quaternion.identity) as SuperTextMesh;
- newStm.t.SetParent(stm.t.parent);
- DestroyImmediate(newStm.gameObject.GetComponent<STMAutoSplit>());
- var newPage = newStm.gameObject.GetComponent<STMPagination>();
- lastPage.overflowText = newStm;
- newPage.originalText = newStm;
- newPage.overflowText = null; //clear overflow text until next loop
- lastPage = newPage;
- stms.Add(newStm);
- }
- StartCoroutine(WaitFrameThenRebuild());
- }
- private IEnumerator WaitFrameThenRebuild()
- {
- yield return null;
- //Rebuild(currentReadTime, reading ? true : autoRead);
- stm.Rebuild();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment