ScrollToBottomBehaviour.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. using UnityEngine.UI;
  5. namespace SRF.UI
  6. {
  7. [AddComponentMenu("SRF/UI/Scroll To Bottom Behaviour")]
  8. [RequireComponent(typeof(RectTransform))]
  9. [ExecuteInEditMode]
  10. public class ScrollToBottomBehaviour : MonoBehaviour
  11. {
  12. public void Start()
  13. {
  14. if (this._scrollRect == null)
  15. {
  16. UnityEngine.Debug.LogError("[ScrollToBottomBehaviour] ScrollRect not set");
  17. return;
  18. }
  19. if (this._canvasGroup == null)
  20. {
  21. UnityEngine.Debug.LogError("[ScrollToBottomBehaviour] CanvasGroup not set");
  22. return;
  23. }
  24. this._scrollRect.onValueChanged.AddListener(new UnityAction<Vector2>(this.OnScrollRectValueChanged));
  25. this.Refresh();
  26. }
  27. private void OnEnable()
  28. {
  29. this.Refresh();
  30. }
  31. public void Trigger()
  32. {
  33. this._scrollRect.normalizedPosition = new Vector2(0f, 0f);
  34. }
  35. private void OnScrollRectValueChanged(Vector2 position)
  36. {
  37. this.Refresh();
  38. }
  39. private void Refresh()
  40. {
  41. if (this._scrollRect == null)
  42. {
  43. return;
  44. }
  45. if (this._scrollRect.normalizedPosition.y < 0.001f)
  46. {
  47. this.SetVisible(false);
  48. }
  49. else
  50. {
  51. this.SetVisible(true);
  52. }
  53. }
  54. private void SetVisible(bool truth)
  55. {
  56. if (truth)
  57. {
  58. this._canvasGroup.alpha = 1f;
  59. this._canvasGroup.interactable = true;
  60. this._canvasGroup.blocksRaycasts = true;
  61. }
  62. else
  63. {
  64. this._canvasGroup.alpha = 0f;
  65. this._canvasGroup.interactable = false;
  66. this._canvasGroup.blocksRaycasts = false;
  67. }
  68. }
  69. [SerializeField]
  70. private ScrollRect _scrollRect;
  71. [SerializeField]
  72. private CanvasGroup _canvasGroup;
  73. }
  74. }