CopyLayoutElement.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. namespace SRF.UI
  2. {
  3. using Internal;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. /// <summary>
  8. /// Copies the preferred size of another layout element (useful for a parent object basing its sizing from a child
  9. /// element).
  10. /// This does have very quirky behaviour, though.
  11. /// TODO: Write custom editor for this to match layout element editor
  12. /// </summary>
  13. [RequireComponent(typeof (RectTransform))]
  14. [ExecuteInEditMode]
  15. [AddComponentMenu(ComponentMenuPaths.CopyLayoutElement)]
  16. public class CopyLayoutElement : UIBehaviour, ILayoutElement
  17. {
  18. public bool CopyMinHeight;
  19. public bool CopyMinWidth;
  20. public bool CopyPreferredHeight;
  21. public bool CopyPreferredWidth;
  22. public RectTransform CopySource;
  23. public float PaddingMinHeight;
  24. public float PaddingMinWidth;
  25. public float PaddingPreferredHeight;
  26. public float PaddingPreferredWidth;
  27. public float preferredWidth
  28. {
  29. get
  30. {
  31. if (!CopyPreferredWidth || CopySource == null || !IsActive())
  32. {
  33. return -1f;
  34. }
  35. return LayoutUtility.GetPreferredWidth(CopySource) + PaddingPreferredWidth;
  36. }
  37. }
  38. public float preferredHeight
  39. {
  40. get
  41. {
  42. if (!CopyPreferredHeight || CopySource == null || !IsActive())
  43. {
  44. return -1f;
  45. }
  46. return LayoutUtility.GetPreferredHeight(CopySource) + PaddingPreferredHeight;
  47. }
  48. }
  49. public float minWidth
  50. {
  51. get
  52. {
  53. if (!CopyMinWidth || CopySource == null || !IsActive())
  54. {
  55. return -1f;
  56. }
  57. return LayoutUtility.GetMinWidth(CopySource) + PaddingMinWidth;
  58. }
  59. }
  60. public float minHeight
  61. {
  62. get
  63. {
  64. if (!CopyMinHeight || CopySource == null || !IsActive())
  65. {
  66. return -1f;
  67. }
  68. return LayoutUtility.GetMinHeight(CopySource) + PaddingMinHeight;
  69. }
  70. }
  71. public int layoutPriority
  72. {
  73. get { return 2; }
  74. }
  75. public float flexibleHeight
  76. {
  77. get { return -1; }
  78. }
  79. public float flexibleWidth
  80. {
  81. get { return -1; }
  82. }
  83. public void CalculateLayoutInputHorizontal() {}
  84. public void CalculateLayoutInputVertical() {}
  85. }
  86. }