using System; using UnityEngine; [ExecuteInEditMode] [RequireComponent(typeof(UIWidget))] public class DynamicLabelBG : MonoBehaviour { private void Start() { this.BGResize(); } private void Update() { if (!this.m_Label) { return; } if (this.m_MyWidget.pivot != this.m_Label.pivot) { this.m_MyWidget.pivot = this.m_Label.pivot; } this.BGResize(); } private void BGResize() { if (!this.m_Label) { return; } if (!string.IsNullOrEmpty(this.m_Label.text)) { this.m_MyWidget.width = this.m_Label.width + this.m_HorizontalMarjinSize; this.m_MyWidget.height = this.m_Label.height + this.m_VerticalMarjinSize; } else { this.m_MyWidget.width = 0; this.m_MyWidget.height = 0; } Vector3 a = Mathf.Lerp(-1f, 1f, this.m_Label.pivotOffset.x) * Vector3.right * (float)this.m_HorizontalMarjinSize / 2f; Vector3 b = Mathf.Lerp(-1f, 1f, this.m_Label.pivotOffset.y) * Vector3.up * (float)this.m_VerticalMarjinSize / 2f; base.transform.localPosition = a + b; } [SerializeField] [Header("縦マージンサイズ")] private int m_VerticalMarjinSize = 14; [SerializeField] [Header("横マージンサイズ")] private int m_HorizontalMarjinSize = 67; [SerializeField] [Header("実際にサイズを合わせるUIラベル")] private UILabel m_Label; [SerializeField] [HideInInspector] private UIWidget m_MyWidget; }