CopyPreferredSize.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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.CopyPreferredSize)]
  14. public class CopyPreferredSize : LayoutElement
  15. {
  16. public RectTransform CopySource;
  17. public float PaddingHeight;
  18. public float PaddingWidth;
  19. public override float preferredWidth
  20. {
  21. get
  22. {
  23. if (CopySource == null || !IsActive())
  24. {
  25. return -1f;
  26. }
  27. return LayoutUtility.GetPreferredWidth(CopySource) + PaddingWidth;
  28. }
  29. }
  30. public override float preferredHeight
  31. {
  32. get
  33. {
  34. if (CopySource == null || !IsActive())
  35. {
  36. return -1f;
  37. }
  38. return LayoutUtility.GetPreferredHeight(CopySource) + PaddingHeight;
  39. }
  40. }
  41. public override int layoutPriority
  42. {
  43. get { return 2; }
  44. }
  45. }
  46. }