Unity ugui to realize sliding page turning and jump page number directly

Time:2020-11-27

In this paper, we share the specific code of unity ugui to realize sliding page turning and jump page number directly for your reference. The specific content is as follows

Let’s first look at the end result

In fact, this function is basically a cliche, so the code is still very simple

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.EventSystems;
using System;
 
public class PageView : MonoBehaviour, IBeginDragHandler, IEndDragHandler
{
 
 Private scrollrect rect; // slide component 
 Private float targethorizontal = 0; // starting coordinate of slide 
 Private bool isdrag = false; // drag end 
 Private list < float > poslist = new list < float > (); // calculate the critical angle of each page, and the page index starts from 0 
 private int currentPageIndex = -1;
 public Action<int> OnPageChanged;
 public RectTransform content;
 private bool stopMove = true;
 Public float smoothing = 4; // sliding speed 
 public float sensitivity = 0;
 private float startTime;
 
 private float startDragHorizontal;
 public Transform toggleList;
 
 void Start()
 {
  rect = transform.GetComponent<ScrollRect>();
  var _rectWidth = GetComponent<RectTransform>();
  var tempWidth = ((float)content.transform.childCount * _rectWidth.rect.width);
  content.sizeDelta = new Vector2(tempWidth, _rectWidth.rect.height);
  //Length not shown
  float horizontalLength = content.rect.width - _rectWidth.rect.width;
  for (int i = 0; i < rect.content.transform.childCount; i++)
  {
   posList.Add(_rectWidth.rect.width * i / horizontalLength);
  }
 }
 
 void Update()
 {
  if (!isDrag && !stopMove)
  {
   startTime += Time.deltaTime;
   float t = startTime * smooting;
   rect.horizontalNormalizedPosition = Mathf.Lerp(rect.horizontalNormalizedPosition, targethorizontal, t);
   if (t >= 1)
    stopMove = true;
  }
  //Debug.Log(rect.horizontalNormalizedPosition);
 }
 
 public void pageTo(int index)
 {
  if (index >= 0 && index < posList.Count)
  {
   rect.horizontalNormalizedPosition = posList[index];
   SetPageIndex(index);
   GetIndex(index);
  }
 }
 private void SetPageIndex(int index)
 {
  if (currentPageIndex != index)
  {
   currentPageIndex = index;
   if (OnPageChanged != null)
    OnPageChanged(index);
  }
 }
 
 public void OnBeginDrag(PointerEventData eventData)
 {
  isDrag = true;
  //Start dragging
  startDragHorizontal = rect.horizontalNormalizedPosition;
 }
 
 public void OnEndDrag(PointerEventData eventData)
 {
  float posX = rect.horizontalNormalizedPosition;
  posX += ((posX - startDragHorizontal) * sensitivity);
  posX = posX < 1 ? posX : 1;
  posX = posX > 0 ? posX : 0;
  int index = 0;
 
  float offset = Mathf.Abs(posList[index] - posX);
  //Debug.Log("offset " + offset);
 
 
  for (int i = 1; i < posList.Count; i++)
  {
   float temp = Mathf.Abs(posList[i] - posX);
   //Debug.Log("temp " + temp);
   //Debug.Log("i" + i);
   if (temp < offset)
   {
    index = i;
    offset = temp;
   }
   //Debug.Log("index " + index);
  }
  //Debug.Log(index);
  SetPageIndex(index);
  GetIndex(index);
  Targethorizontal = poslist [index]; // set the current coordinates and update the function for interpolation 
  isDrag = false;
  startTime = 0;
  stopMove = false;
 
 }
 
 public void GetIndex(int index)
 {
  var toogle = toggleList.GetChild(index).GetComponent<Toggle>();
  toogle.isOn = true;
 }
}
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
 
public class GameController : MonoBehaviour {
 [SerializeField]
 private Text pageNumber;
 [SerializeField]
 private InputField inputField;
 [SerializeField]
 private PageView pageView;
 // Use this for initialization
 void Start () {
  pageNumber.text  = string.Format  ("current page number: 0");
  pageView.OnPageChanged = pageChanged;
 }
 
 void pageChanged (int index) {
  pageNumber.text  = string.Format  ("current page number: {0})", index.ToString  ());
 }
 
 public void onClick () {
  try {
   int idnex = int.Parse (inputField.text);
   pageView.pageTo (idnex);
  } catch(Exception ex) {
   Debug.LogWarning  ("please enter a number"+ ex.ToString ()); 
  }
 }
 
 void Destroy () {
  pageView.OnPageChanged = null;
 }
}

Attached project: Address

All of the above, I hope that all of the above will be helpful to you.