using System; using System.Collections; using System.Threading.Tasks; using UnityEngine; using UnityEngine.UI; /// <summary> /// Handles auto scrolling helper GUIs that are instanced when an item is hovered /// </summary> public class AutoScroller : MonoBehaviour { [Header("Scroll Values")] [SerializeField] private AspectHelperUI aspectHelperComponent; [SerializeField] private bool isHorizontal = false; [SerializeField] private bool isVertical = true; private ScrollRect scrollingRect; private bool resetScroll; private int effectCount; // Start is called before the first frame update private async void OnEnable() { scrollingRect = GetComponent<ScrollRect>(); try { effectCount = aspectHelperComponent.GetEffectCount(); } catch { while (effectCount == default) await Task.Yield(); } StartCoroutine(Scroll(0f, 1f)); } private void Update() { if (resetScroll) { StopAllCoroutines(); StartCoroutine(Scroll(0f, 1f)); } } private IEnumerator Scroll(float startPosition, float endPosition) { resetScroll = false; yield return new WaitForSeconds(1f); float currentNormalization = 0.0f; while (currentNormalization < 1f) { currentNormalization += Time.deltaTime / effectCount; // Scrolls in the desired direction if (isHorizontal) scrollingRect.horizontalNormalizedPosition = Mathf.Lerp(endPosition, startPosition, currentNormalization); if (isVertical) scrollingRect.verticalNormalizedPosition = Mathf.Lerp(endPosition, startPosition, currentNormalization); yield return null; } yield return new WaitForSeconds(1f); resetScroll = true; } public void InjectEffectCount(int count) => effectCount = count; }