unity 基于scrollRect实现翻页显示,并定为到某一页,而不是某一页的中间方法(第二个脚本采用实际位置计算,并在update里实现平滑过渡):
组场景时,经常需要获取鼠标(或者点击)开始结束时讯息(包括开始拖动时的事件以及对应的坐标),unity为我们提供了两个接口来实现相关方法之前在Unity基础命令中也提到过。
在此在重申一下,两个接口对应两个方法,即可以对应开始结束拖动时的事件,数据则为eventData中。
public void OnBeginDrag(PointerEventData eventData)
public void OnEndDrag(PointerEventData eventData)
public class NewMove : MonoBehaviour,IBeginDragHandler,IEndDragHandler { private ScrollRect scrollRect; public Toggle[] toggleArr; //存储特定的位置坐标 private float[] pageArr=new float[]{0,0.5f,1.0f}; public void OnBeginDrag(PointerEventData eventData) { // Debug.Log("Begin:"); // Debug.Log(eventData.position); //获取rect的初始坐标值 Vector2 pos = scrollRect.normalizedPosition; Debug.Log("Begin:"+pos); } public void OnEndDrag(PointerEventData eventData) { //Debug.Log("End:"); //Debug.Log(eventData.position); //获取rect的拖动后的坐标值 Vector2 pos = scrollRect.normalizedPosition; Debug.Log("End:"+pos); //获取rect拖动后的水平坐标值 float posX = scrollRect.horizontalNormalizedPosition; int index = 0; //计算与特定点的偏差 float offset = Math.Abs(pageArr[index] - posX); //与每一个坐标点进行比较,确定应该存在的位置 //偏差最小的位置即为rect的位置点 for(int i=0;i<pageArr.Length;i++) { float newOffset = Math.Abs(pageArr[i] - posX); if(newOffset<= offset) { index = i; offset = newOffset; } } scrollRect.horizontalNormalizedPosition = pageArr[index]; toggleArr[index].isOn = true; Debug.Log("End:" + scrollRect.horizontalNormalizedPosition); }
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.EventSystems; using UnityEngine.Events; public class DragController : MonoBehaviour, IBeginDragHandler, IEndDragHandler { [SerializeField] private GameObject scrollRect; [SerializeField] private GameObject scrollRectSmall; public float distance = 600f; public int smoothing = 2; public float deltaWidth = 3840f; public float deltaSmallWidth = 95f; private List<GameObject> pages; private List<GameObject> smallScreens; private float horizontalPos = 0; private float horizontalPosSmall = 0; private List<float> postions = new List<float>(); private bool onDrag = false; private float beginPos = 0; private float endPos = 0; private int pageIndex = 0; public void Init(List<GameObject> pages) { this.pages = new List<GameObject>(pages); //this.smallScreens = smallScreens; pageIndex = 0; horizontalPos = 0; horizontalPosSmall = 0; //for (int i=0;i<pages.Count;i++) //{ // postions.Add(i); //} } public void OnBeginDrag(PointerEventData eventData) { onDrag = true; beginPos = eventData.position.x; } public void OnEndDrag(PointerEventData eventData) { onDrag = false; endPos = eventData.position.x; float newDis = endPos - beginPos; if (newDis < -distance) { pageIndex++; if (pageIndex >= pages.Count) { pageIndex = pages.Count - 1; } } if(newDis > distance) { pageIndex--; if(pageIndex < 0) { pageIndex = 0; } } horizontalPos = -deltaWidth * pageIndex; //print(horizontalPos); horizontalPosSmall = -deltaSmallWidth * pageIndex; //print(horizontalPosSmall); pages[pageIndex].GetComponent<Toggle>().isOn = true; //smallScreens[pageIndex].GetComponent<Toggle>().isOn = true; } // Use this for initialization void Start () { float scale = 3840f / Screen.width; deltaWidth = deltaWidth / scale; } // Update is called once per frame void Update () { if (!onDrag) { //scrollRect.GetComponent<rec = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, // horizontalPos, smoothing * Time.deltaTime); Vector2 pos = scrollRect.GetComponent<RectTransform>().position; float xPos = Mathf.Lerp(pos.x, horizontalPos, Time.deltaTime * smoothing); scrollRect.GetComponent<RectTransform>().position = new Vector2(xPos, pos.y); //print(xPos + " " + horizontalPos); //Vector2 posS = scrollRectSmall.GetComponent<RectTransform>().anchoredPosition; //float xPosS = Mathf.Lerp(posS.x, horizontalPosSmall, Time.deltaTime * smoothing); //scrollRectSmall.GetComponent<RectTransform>().anchoredPosition = new Vector2(xPosS, posS.y); } } }