MaximilianPs

ResizeBackgroundToText

Sep 15th, 2025 (edited)
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.88 KB | Source Code | 0 0
  1. // This class must stay on the parent were the image used as background sits.
  2. // As child you have to have TextMeshProUGUI text.
  3. // Set the transform as centered, and as you wish for padding and stuff... That's it.
  4. // Will works automagically =)
  5.  
  6.         [ExecuteAlways]
  7.     [RequireComponent(typeof(RectTransform))]
  8.     public class ResizeBackgroundToText : MonoBehaviour
  9.     {
  10.         [Header("Riferimenti")]
  11.         [Tooltip("Il componente TextMeshPro figlio.")]
  12.         public TextMeshProUGUI text;
  13.  
  14.         [Tooltip("Il RectTransform di questo oggetto (il background). Si assegna da solo se lasciato vuoto.")]
  15.         public RectTransform background;
  16.  
  17.         [Header("Limiti e Padding")]
  18.         [Tooltip("Padding per ogni lato. X = sinistra/destra, Y = sopra/sotto.")]
  19.         public Vector2 padding = new Vector2(20f, 10f);
  20.  
  21.         [Tooltip("La larghezza massima che questo elemento può raggiungere prima che il testo vada a capo.")]
  22.         public float maxWidth = 500f;
  23.  
  24.         [Header("Editor")]
  25.         public bool autoUpdateInEditor = true;
  26.  
  27.         void OnEnable()
  28.         {
  29.             if (background == null)
  30.             {
  31.                 background = GetComponent<RectTransform>();
  32.             }
  33.         }
  34.  
  35.         void LateUpdate()
  36.         {
  37.             if (Application.isPlaying)
  38.             {
  39.                 Resize();
  40.             }
  41. #if UNITY_EDITOR
  42.             else if (autoUpdateInEditor)
  43.             {
  44.                 Resize();
  45.             }
  46. #endif
  47.         }
  48.  
  49. #if UNITY_EDITOR
  50.         void OnValidate()
  51.         {
  52.             if (autoUpdateInEditor)
  53.             {
  54.                 if (background == null)
  55.                 {
  56.                     background = GetComponent<RectTransform>();
  57.                 }
  58.                 Resize();
  59.             }
  60.         }
  61. #endif
  62.  
  63.         public void Resize()
  64.         {
  65.             if (text == null || background == null || maxWidth <= 0)
  66.                 return;
  67.  
  68.             // 1. Definiamo la larghezza massima del testo.
  69.             //    Questa è la larghezza massima dello sfondo meno il DOPPIO del padding orizzontale (sinistra + destra).
  70.             float maxTextWidth = maxWidth - (padding.x * 2);
  71.  
  72.             // 2. Calcoliamo la dimensione preferita del testo con il vincolo di larghezza.
  73.             Vector2 preferredSize = text.GetPreferredValues(text.text, maxTextWidth, float.PositiveInfinity);
  74.  
  75.             // 3. La nuova dimensione dello sfondo sarà la dimensione del testo
  76.             //    più il DOPPIO del padding (sinistra+destra e sopra+sotto).
  77.             Vector2 backgroundSize = new Vector2(preferredSize.x + (padding.x * 2), preferredSize.y + (padding.y * 2));
  78.  
  79.             // Applica la dimensione calcolata al RectTransform del background.
  80.             background.sizeDelta = backgroundSize;
  81.  
  82.             LayoutRebuilder.ForceRebuildLayoutImmediate(background);
  83.         }
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment