CopySizeIntoLayoutElement.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace SRF.UI
  5. {
  6. [RequireComponent(typeof(RectTransform))]
  7. [ExecuteInEditMode]
  8. [AddComponentMenu("SRF/UI/Copy Size Into Layout Element")]
  9. public class CopySizeIntoLayoutElement : LayoutElement
  10. {
  11. public override float preferredWidth
  12. {
  13. get
  14. {
  15. if (!this.SetPreferredSize || this.CopySource == null || !this.IsActive())
  16. {
  17. return -1f;
  18. }
  19. return this.CopySource.rect.width + this.PaddingWidth;
  20. }
  21. }
  22. public override float preferredHeight
  23. {
  24. get
  25. {
  26. if (!this.SetPreferredSize || this.CopySource == null || !this.IsActive())
  27. {
  28. return -1f;
  29. }
  30. return this.CopySource.rect.height + this.PaddingHeight;
  31. }
  32. }
  33. public override float minWidth
  34. {
  35. get
  36. {
  37. if (!this.SetMinimumSize || this.CopySource == null || !this.IsActive())
  38. {
  39. return -1f;
  40. }
  41. return this.CopySource.rect.width + this.PaddingWidth;
  42. }
  43. }
  44. public override float minHeight
  45. {
  46. get
  47. {
  48. if (!this.SetMinimumSize || this.CopySource == null || !this.IsActive())
  49. {
  50. return -1f;
  51. }
  52. return this.CopySource.rect.height + this.PaddingHeight;
  53. }
  54. }
  55. public override int layoutPriority
  56. {
  57. get
  58. {
  59. return 2;
  60. }
  61. }
  62. public RectTransform CopySource;
  63. public float PaddingHeight;
  64. public float PaddingWidth;
  65. public bool SetPreferredSize;
  66. public bool SetMinimumSize;
  67. }
  68. }