DynamicLabelBG.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. using System;
  2. using UnityEngine;
  3. [ExecuteInEditMode]
  4. [RequireComponent(typeof(UIWidget))]
  5. public class DynamicLabelBG : MonoBehaviour
  6. {
  7. private void Start()
  8. {
  9. this.BGResize();
  10. }
  11. private void Update()
  12. {
  13. if (!this.m_Label)
  14. {
  15. return;
  16. }
  17. if (this.m_MyWidget.pivot != this.m_Label.pivot)
  18. {
  19. this.m_MyWidget.pivot = this.m_Label.pivot;
  20. }
  21. this.BGResize();
  22. }
  23. private void BGResize()
  24. {
  25. if (!this.m_Label)
  26. {
  27. return;
  28. }
  29. if (!string.IsNullOrEmpty(this.m_Label.text))
  30. {
  31. this.m_MyWidget.width = this.m_Label.width + this.m_HorizontalMarjinSize;
  32. this.m_MyWidget.height = this.m_Label.height + this.m_VerticalMarjinSize;
  33. }
  34. else
  35. {
  36. this.m_MyWidget.width = 0;
  37. this.m_MyWidget.height = 0;
  38. }
  39. Vector3 a = Mathf.Lerp(-1f, 1f, this.m_Label.pivotOffset.x) * Vector3.right * (float)this.m_HorizontalMarjinSize / 2f;
  40. Vector3 b = Mathf.Lerp(-1f, 1f, this.m_Label.pivotOffset.y) * Vector3.up * (float)this.m_VerticalMarjinSize / 2f;
  41. base.transform.localPosition = a + b;
  42. }
  43. [SerializeField]
  44. [Header("縦マージンサイズ")]
  45. private int m_VerticalMarjinSize = 14;
  46. [SerializeField]
  47. [Header("横マージンサイズ")]
  48. private int m_HorizontalMarjinSize = 67;
  49. [SerializeField]
  50. [Header("実際にサイズを合わせるUIラベル")]
  51. private UILabel m_Label;
  52. [SerializeField]
  53. [HideInInspector]
  54. private UIWidget m_MyWidget;
  55. }