using System.Collections; using System.Collections.Generic; using System.Threading; using UnityEngine; using UnityEngine.UI; public class ScrollTest2 : MonoBehaviour { // Start is called before the first frame update private ScrollRect scroll; private Transform content; private float contentSize; [SerializeField] Button L, R; void Start() { scroll = gameObject.GetComponent(); content = gameObject.transform.Find("Viewport").Find("Content"); contentSize = 1f/(content.childCount-2); scroll.horizontalNormalizedPosition = contentSize / 2; if (L != null) { L.onClick.AddListener(goLeft); } if (R != null) { R.onClick.AddListener(goRight); } } void goLeft() { if (scroll.horizontalNormalizedPosition <= contentSize) return; start = true; scrollValue = -1 * contentSize / 30; } void goRight() { if ((scroll.horizontalNormalizedPosition + contentSize)>=1) return; start = true; scrollValue = contentSize / 30; } private bool start = false, _lock = false; private float scrollValue = 0,totalScrollValue = 0; // Update is called once per frame void Update() { if (start) { if (_lock) return; _lock = true; totalScrollValue += scrollValue; if (System.Math.Abs(totalScrollValue) >= contentSize) { scrollValue = 0; start = false; totalScrollValue = 0; } else { scroll.horizontalNormalizedPosition += scrollValue; } _lock = false; } } }