Introduction:
When designing UI, we often use the ScrollView provided by UI in Unity, similar to the ScrollView in Android. When previewing images and turning multiple pages, we can achieve good results.
In this class, according to the drag event in Unity's EventSystems, slide monitoring of page numbers is realized. When using it, create a new UI--->ScrollView, add this class component to the ScrollView, add the corresponding content to the content in the script, adjust the ScrollView and Content, set the width of a single sliding page, and drag the threshold to listen to the drag. If it is a dynamic instantiation of child in the ScrollView, the current maximum number of page numbers must be set. SetCurIndex can directly locate the sliding page corresponding to the current page number. The code is relatively simple and can be posted directly.
public class ScrollViewListener : MonoBehaviour, IDragHandler, IBeginDragHandler, IEndDragHandler { //Sliding direction public enum MoveDirection { None = 0, Left, Right, } public float SingleItemWidth;//The width of a single sliding page public RectTransform content;//The current content of the ScrollView public float DragMinValue = 5f;//The minimum drag value allowed during the dragging process is not considered dragging or page turn event is not executed below this value. private MoveDirection direction = ; private int CurIndex = 0;//Current page number private int MaxIndex = 0;//Maximum page number public bool canMove = true;//Can it be able to move private Vector3 originalPos; private float maxDeltaX = 0f;//Take the maximum value during the entire dragging process public Action<int> OnPageChange;//Provide callbacks for page number modification to the outside world /// <summary> /// Slide to the next page /// </summary> private void MoveToNext() { if (direction == ) { if (CurIndex < MaxIndex) { CurIndex++; canMove = false; ( - SingleItemWidth, 1f).OnComplete(() => { if (null != OnPageChange) { OnPageChange(CurIndex); } canMove = true; }); } } else if (direction == ) { if (CurIndex > 0) { CurIndex--; canMove = false; ( + SingleItemWidth, 1f).OnComplete(() => { if (null != OnPageChange) { OnPageChange(CurIndex); } canMove = true; }); } } } /// <summary> /// Set the maximum number of pages in the current sliding list /// </summary> /// <param name="max"></param> public void SetMaxIndex(int max) { MaxIndex = max - 1;//The maximum subscript value is minus 1 page number } /// <summary> /// Set the current page /// </summary> /// <param name="index"></param> public void SetCurIndex(int index) { CurIndex = index; float x = - SingleItemWidth * CurIndex; = new Vector3(x, , ); } public void ResetPosition() { = originalPos; } private void Awake() { CurIndex = 0; originalPos = ; } public void OnDrag(PointerEventData eventData) { if (() > maxDeltaX) { maxDeltaX = (); } } public void OnBeginDrag(PointerEventData eventData) { if ( > 0) { direction = ; } else if ( < 0) { direction = ; } else { direction = ; } if (() > maxDeltaX) { maxDeltaX = (); } } public void OnEndDrag(PointerEventData eventData) { if (() > maxDeltaX) { maxDeltaX = (); } if (maxDeltaX > DragMinValue) { // Calculate the purpose of the next page and move if (canMove) { MoveToNext(); } } maxDeltaX = 0f; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.