ScrollToTop.cs 863 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. using UnityEngine;
  2. using System.Collections;
  3. /// <summary>
  4. /// This component should be placed on a UIScrollbar and is responsible for scrolling it to the top.
  5. /// This is useful for dynamic content inside a scrollable panel
  6. /// </summary>
  7. public class ScrollToTop : MonoBehaviour
  8. {
  9. UIScrollBar scrollBar;
  10. void Awake()
  11. {
  12. scrollBar = GetComponent<UIScrollBar>();
  13. }
  14. void OnEnable()
  15. {
  16. StartCoroutine(ResetScrollCoroutine());
  17. }
  18. IEnumerator ResetScrollCoroutine()
  19. {
  20. yield return new WaitForSeconds(0.5f); //wait for transition
  21. iTween.ValueTo(gameObject, iTween.Hash(
  22. "from", scrollBar.scrollValue,
  23. "to", 0,
  24. "time", 0.5f,
  25. "easetype", iTween.EaseType.easeOutCubic,
  26. "onupdate", "UpdateScrollValue",
  27. "onupdatetarget", gameObject));
  28. }
  29. void UpdateScrollValue(float newScrollValue)
  30. {
  31. scrollBar.scrollValue = newScrollValue;
  32. }
  33. }