預覽效果,切換程序順滑
使用最后一次滑動的方向來判定目標方向,如果朝某邊偏移后往回移動,會回傳到原來的位置

支持代碼設定索引直接跳轉

不需要手動設定寬度,自動根據當前子類來設定滑動距離,支持動態添加和洗掉

使用方法:
0 新建腳本,重命名為Slider,復制代碼保存
點擊這里
1 新建Scroll View控制元件,修改長寬為600*200

2 將Content錨點改為如圖所示,并添加圖中2個組件并設定變數

3 添加頁面資料,以Image為例(按鈕或Panel皆可,但尺寸需要保持一致),設定尺寸為600*200,然后Ctrl+D復制多份,Content會自動拉伸長度

4 選擇Scroll View,按照圖中關閉Vertical與Inertil(滑動慣性),洗掉用不上的Scrollbar Vertical縱向滑條,加入Slide腳本,把Content拖入變數

然后點擊運行,就可以看見上面的效果了
5 附加代碼
//跳轉到指定節點,如果不存在會回傳false
bool isSucceed = GetComponent<Slide>().ToAppointNode(int 指定節點)
//回傳當前所在的節點
int x = GetComponent<Slide>().GetCurNodeCount()
//回傳當前總節點數(也就是Content的子物件數)
int x = GetComponent<Slide>().GetMaxNodeCount()
//注冊回呼,切換不同頁面時觸發
GetComponent<Slide>().SubscribeCallback(Action<int> callback);
//解除回呼
GetComponent<Slide>().UnSubscribeCallback();
代碼
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Slide : MonoBehaviour, IEndDragHandler, IDragHandler, IBeginDragHandler
{
//按下時判斷目標方向,松開時根據當前偏移方向與目標方向,計算目標節點,然后移動至該節點
//腳本需要放置在Scroll View組件中,MovementType設為Unrestricted,關閉Inertia
//參考框
[Header("Content組件")]
public GameObject content;
//節點數量
int nodeCount = 0;
//節點寬度/切換長度
float nodeWidth = 0;
//切換速度
[Header("切換速度")]
public float speed = 12;
//當前所在節點
int curNode = 0;
//目標節點
int targetNode = 0;
//當前觸摸坐標
float curTouchPos = 0;
//上一幀觸摸坐標
float lastTouchPos = 0;
//當前觸摸狀態
bool isTouch = false;
//切換目標方向
bool switchTargetIsLeft = false;
//當前偏移方向
bool offsetIsLeft = false;
//是否到達目標
bool isArrive = true;
//切換瞬間的回呼
public Action<int> switchCallback = null;
//按下
public void OnBeginDrag(PointerEventData eventData)
{
isTouch = true;
lastTouchPos = eventData.position.x;
curTouchPos = eventData.position.x;
}
//拖拽
public void OnDrag(PointerEventData eventData)
{
curTouchPos = eventData.position.x;
//設定滑動方向
if (lastTouchPos - curTouchPos < 0)
switchTargetIsLeft = true;
else if (lastTouchPos - curTouchPos > 0)
switchTargetIsLeft = false;
lastTouchPos = eventData.position.x;
}
//松開
public void OnEndDrag(PointerEventData eventData)
{
isTouch = false;
//設為未到達
isArrive = false;
//更新節點資訊
UpdateNodeData();
//判斷當前節點偏移方向
if (content.GetComponent<RectTransform>().anchoredPosition.x * -1 < curNode * nodeWidth)//如果當前UI坐標 < 當前節點坐標
offsetIsLeft = true;
else
offsetIsLeft = false;
//如果目標方向和偏移方向一致
if ((switchTargetIsLeft == offsetIsLeft) &&
((switchTargetIsLeft && (curNode - 1 >= 0)) || //且 朝左時沒有超過最小值
(!switchTargetIsLeft && curNode + 1 <= nodeCount - 1)))//且 朝右時沒有超過最大值
{
//設定目標節點
targetNode = switchTargetIsLeft ? curNode - 1 : curNode + 1;
//更新當前節點
curNode = targetNode;
//觸發回呼
switchCallback?.Invoke(targetNode);
}
//否則回到原始位置
else
{
//回到當前節點
targetNode = curNode;
}
}
//獲取當前節點數
public int GetCurNodeCount()
{
//更新節點資訊
UpdateNodeData();
return curNode;
}
//獲取總節點數
public int GetMaxNodeCount()
{
//更新節點資訊
UpdateNodeData();
return nodeCount;
}
//前往指定節點
public bool ToAppointNode(int appointNode)
{
//更新節點資訊
UpdateNodeData();
if (appointNode < 0 || appointNode >= nodeCount)
{
//Debug.LogError("指定節點超出范圍:");
return false;
}
isArrive = false;
//觸發回呼
if (appointNode != curNode)
switchCallback?.Invoke(appointNode);
//設定目標節點
targetNode = appointNode;
//更新當前節點
curNode = targetNode;
return true;
}
//注冊回呼
public void SubscribeCallback(Action<int> newCallback)
{
switchCallback = newCallback;
}
//清慷訓呼
public void UnsubscribeCallback()
{
switchCallback = null;
}
//更新節點總數和每個節點的寬度
void UpdateNodeData()
{
//獲取節點總數(所有子類)
nodeCount = content.transform.childCount;
//獲取每個節點的寬度
nodeWidth = content.GetComponent<RectTransform>().rect.width / nodeCount;
}
void Update()
{
//如果 不在點擊狀態 且 沒有到達目標節點
if (!isTouch && !isArrive)
{
//前往目標節點
Vector2 targetPos = new Vector2(
targetNode * nodeWidth * -1,
content.GetComponent<RectTransform>().anchoredPosition.y);
content.GetComponent<RectTransform>().anchoredPosition = Vector2.Lerp(content.GetComponent<RectTransform>().anchoredPosition, targetPos, Time.deltaTime * speed);
//如果很靠近
if (Mathf.Abs(content.GetComponent<RectTransform>().anchoredPosition.x - targetPos.x) <= 0.05)
isArrive = true;
}
}
}
萌新需要多多支持,如果對你有幫助,就請點個贊吧~
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/230776.html
標籤:其他
上一篇:element-ui查看大圖
