ScrollToTop.cs 869 B

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