UISycnScrollContentSize.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using UnityEngine;
  2. using System.Collections;
  3. public class UISycnScrollContentSize : MonoBehaviour
  4. {
  5. public RectTransform content;
  6. public float maxHeight;
  7. float height;
  8. float width;
  9. private RectTransform rt;
  10. private RectTransform viewPoint;
  11. private float parentH = 0;
  12. // Use this for initialization
  13. void Start()
  14. {
  15. rt = GetComponent<RectTransform>();
  16. if (content != null && content.parent!=null)
  17. viewPoint = content.parent.GetComponent<RectTransform>();
  18. if(viewPoint!=null)
  19. {
  20. if(viewPoint.anchorMin == Vector2.zero && viewPoint.anchorMax == Vector2.one)
  21. {
  22. parentH = viewPoint.offsetMin.y - viewPoint.offsetMax.y;
  23. }
  24. }
  25. }
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. if (content == null || rt == null) return;
  30. float h = content.sizeDelta.y;
  31. if (height.FEqual(h, 0.1f))
  32. return;
  33. height = h + parentH;
  34. if (height >= maxHeight)
  35. height = maxHeight;
  36. Vector2 sizeDelta = rt.sizeDelta;
  37. sizeDelta.y = height;
  38. rt.sizeDelta = sizeDelta;
  39. }
  40. }