theMessyCoder

TMP Container Size Fitter

Mar 7th, 2022 (edited)
1,839
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.01 KB | None | 0 0
  1. /*****************************************************
  2. // File: TMP Container Size Fitter
  3. // Author: The Messy Coder
  4. // Date: March 2022
  5. // Please support by following on Twitch & social media
  6. // Twitch:   www.Twitch.tv/TheMessyCoder
  7. // YouTube:  www.YouTube.com/TheMessyCoder
  8. // Facebook: www.Facebook.com/TheMessyCoder
  9. // Twitter:  www.Twitter.com/TheMessyCoder
  10. *****************************************************/
  11.  
  12. using UnityEngine;
  13.  
  14. namespace Messy
  15. {
  16.     [AddComponentMenu("Layout/TMP Container Size Fitter")]
  17.     public class TextContainerFitter : MonoBehaviour
  18.     {
  19.         [SerializeField]
  20.         private TMPro.TextMeshProUGUI m_TMProUGUI;
  21.         public TMPro.TextMeshProUGUI TextMeshPro
  22.         {
  23.             get
  24.             {
  25.                 if (m_TMProUGUI == null && transform.GetComponentInChildren<TMPro.TextMeshProUGUI>())
  26.                 {
  27.                     m_TMProUGUI = transform.GetComponentInChildren<TMPro.TextMeshProUGUI>();
  28.                     m_TMPRectTransform = m_TMProUGUI.rectTransform;
  29.                 }
  30.                 return m_TMProUGUI;
  31.             }
  32.         }
  33.  
  34.         protected RectTransform m_RectTransform;
  35.         public RectTransform rectTransform
  36.         {
  37.             get
  38.             {
  39.                 if (m_RectTransform == null)
  40.                 {
  41.                     m_RectTransform = GetComponent<RectTransform>();
  42.                 }
  43.                 return m_RectTransform;
  44.             }
  45.         }
  46.  
  47.         protected RectTransform m_TMPRectTransform;
  48.         public RectTransform TMPRectTransform { get { return m_TMPRectTransform; } }
  49.  
  50.         protected float m_PreferredHeight;
  51.         public float PreferredHeight { get { return m_PreferredHeight; } }
  52.  
  53.         protected virtual void SetHeight()
  54.         {
  55.             if (TextMeshPro == null)
  56.                 return;
  57.  
  58.             m_PreferredHeight = TextMeshPro.preferredHeight;
  59.             rectTransform.sizeDelta = new Vector2(rectTransform.sizeDelta.x, PreferredHeight);
  60.  
  61.             SetDirty();
  62.         }
  63.  
  64.         protected virtual void OnEnable()
  65.         {            
  66.             SetHeight();
  67.         }
  68.  
  69.         protected virtual void Start()
  70.         {
  71.             SetHeight();
  72.         }
  73.  
  74.         protected virtual void Update()
  75.         {
  76.             if (PreferredHeight != TextMeshPro.preferredHeight)
  77.             {
  78.                 SetHeight();
  79.             }
  80.         }
  81.  
  82.         #region MarkLayoutForRebuild
  83.         public virtual bool IsActive()
  84.         {
  85.             return isActiveAndEnabled;
  86.         }
  87.  
  88.         protected void SetDirty()
  89.         {
  90.             if (!IsActive())
  91.                 return;
  92.  
  93.             UnityEngine.UI.LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
  94.         }
  95.  
  96.  
  97.         protected virtual void OnRectTransformDimensionsChange()
  98.         {
  99.             SetDirty();
  100.         }
  101.  
  102.  
  103.         #if UNITY_EDITOR
  104.         protected virtual void OnValidate()
  105.         {
  106.             SetDirty();
  107.         }
  108.         #endif
  109.  
  110.         #endregion
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment