CopySizeIntoLayoutElement.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. namespace SRF.UI
  2. {
  3. using Internal;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6. /// <summary>
  7. /// Copies the preferred size of another layout element (useful for a parent object basing its sizing from a child
  8. /// element).
  9. /// This does have very quirky behaviour, though.
  10. /// </summary>
  11. [RequireComponent(typeof(RectTransform))]
  12. [ExecuteInEditMode]
  13. [AddComponentMenu(ComponentMenuPaths.CopySizeIntoLayoutElement)]
  14. public class CopySizeIntoLayoutElement : LayoutElement
  15. {
  16. public RectTransform CopySource;
  17. public float PaddingHeight;
  18. public float PaddingWidth;
  19. public bool SetPreferredSize = false;
  20. public bool SetMinimumSize = false;
  21. public override float preferredWidth
  22. {
  23. get
  24. {
  25. if (!SetPreferredSize || CopySource == null || !IsActive())
  26. {
  27. return -1f;
  28. }
  29. return CopySource.rect.width + PaddingWidth;
  30. }
  31. }
  32. public override float preferredHeight
  33. {
  34. get
  35. {
  36. if (!SetPreferredSize || CopySource == null || !IsActive())
  37. {
  38. return -1f;
  39. }
  40. return CopySource.rect.height + PaddingHeight;
  41. }
  42. }
  43. public override float minWidth
  44. {
  45. get
  46. {
  47. if (!SetMinimumSize || CopySource == null || !IsActive())
  48. {
  49. return -1f;
  50. }
  51. return CopySource.rect.width + PaddingWidth;
  52. }
  53. }
  54. public override float minHeight
  55. {
  56. get
  57. {
  58. if (!SetMinimumSize || CopySource == null || !IsActive())
  59. {
  60. return -1f;
  61. }
  62. return CopySource.rect.height + PaddingHeight;
  63. }
  64. }
  65. public override int layoutPriority
  66. {
  67. get { return 2; }
  68. }
  69. }
  70. }