|
- using System;
- using Tweening;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.UI;
-
- namespace CIG
- {
- public class ScrollbarTweener : MonoBehaviour
- {
- private void Start()
- {
- this._scrollRect.onValueChanged.AddListener(new UnityAction<Vector2>(this.OnScrollRectValueChanged));
- this._tweener.FinishedPlaying += this.OnTweenerFinishedPlaying;
- }
-
- private void OnEnable()
- {
- if (this._startHidden)
- {
- this._canvasGroup.alpha = 0f;
- this._canvasGroup.interactable = false;
- }
- else
- {
- this.OnScrollRectValueChanged(this._scrollRect.normalizedPosition);
- }
- }
-
- private void LateUpdate()
- {
- this._scrollBar.size = Mathf.Clamp(this._scrollBar.size, this._minHandleSize, this._maxHandleSize);
- }
-
- private void OnDestroy()
- {
- this._scrollRect.onValueChanged.RemoveListener(new UnityAction<Vector2>(this.OnScrollRectValueChanged));
- this._tweener.FinishedPlaying -= this.OnTweenerFinishedPlaying;
- }
-
- public void OnScrollRectValueChanged(Vector2 scrollPosition)
- {
- if (this._tweener.IsPlaying)
- {
- this._tweener.Stop();
- }
- this._tweener.Reset(false);
- this._canvasGroup.interactable = true;
- this._updatingScrollBar = true;
- switch (this._scrollBar.direction)
- {
- case Scrollbar.Direction.LeftToRight:
- case Scrollbar.Direction.RightToLeft:
- {
- this._scrollBar.value = scrollPosition.x;
- float num = this._scrollRect.content.rect.width;
- float num2 = this._scrollRect.viewport.rect.width;
- break;
- }
- case Scrollbar.Direction.BottomToTop:
- case Scrollbar.Direction.TopToBottom:
- {
- this._scrollBar.value = scrollPosition.y;
- float num = this._scrollRect.content.rect.height;
- float num2 = this._scrollRect.viewport.rect.height;
- break;
- }
- default:
- UnityEngine.Debug.LogWarningFormat("Missing direction: '{0}'", new object[]
- {
- this._scrollBar.direction
- });
- break;
- }
- this._scrollBar.size = Mathf.Clamp(this._scrollBar.size, this._minHandleSize, this._maxHandleSize);
- this._updatingScrollBar = false;
- this._tweener.Play();
- }
-
- public void OnScrollBarValueChanged(float value)
- {
- if (this._updatingScrollBar)
- {
- return;
- }
- switch (this._scrollBar.direction)
- {
- case Scrollbar.Direction.LeftToRight:
- case Scrollbar.Direction.RightToLeft:
- this._scrollRect.horizontalNormalizedPosition = value;
- break;
- case Scrollbar.Direction.BottomToTop:
- case Scrollbar.Direction.TopToBottom:
- this._scrollRect.verticalNormalizedPosition = value;
- break;
- default:
- UnityEngine.Debug.LogWarningFormat("Missing direction: '{0}'", new object[]
- {
- this._scrollBar.direction
- });
- break;
- }
- }
-
- private void OnTweenerFinishedPlaying(Tweener tweener)
- {
- this._canvasGroup.interactable = false;
- }
-
- [SerializeField]
- private ScrollRect _scrollRect;
-
- [SerializeField]
- private Scrollbar _scrollBar;
-
- [SerializeField]
- private Tweener _tweener;
-
- [SerializeField]
- [Range(0f, 1f)]
- private float _minHandleSize;
-
- [SerializeField]
- [Range(0f, 1f)]
- private float _maxHandleSize = 1f;
-
- [SerializeField]
- private CanvasGroup _canvasGroup;
-
- [SerializeField]
- private bool _startHidden = true;
-
- private bool _updatingScrollBar;
- }
- }
|