Unity實現(xiàn)ScrollView滑動吸附功能
本文實例為大家分享了Unity實現(xiàn)ScrollView滑動吸附的具體代碼,供大家參考,具體內(nèi)容如下
最近在做一個展示模塊的時候遇到了一個需要實現(xiàn)滑動窗口并且能固定吸附距離的需求,借助UGUI的ScrollView的API以及Dotween實現(xiàn)了這個功能。主要核心邏輯就是檢測Content節(jié)點的RectTransform的localPosX的移動距離然后繼承實現(xiàn)OnDrag幾個接口來完成拖動再松開自動吸附到具體的位置。具體效果如下
另外說一下有幾個ScrollView自帶的API需要設(shè)置一下,一個事Movement Type設(shè)置成Unrestricted,以及關(guān)閉Inertia,這樣才能關(guān)閉ScrollView自帶的最大距離移動控制不會導(dǎo)致在松手吸附過程中因為拖動距離大于了左右兩邊限制而最終的移動結(jié)束的坐標(biāo)位置不對。
下面貼一下代碼,腳本直接附在Content物體上既可。
Tip:代碼里的450 225是一個子物體寬度的加上content的Space距離,我演示的工程師一個Image420的寬度 30的HorLayout Space,225則是這個距離/2,可以根據(jù)具體需求去改變由于只是為了出個DEMO就沒有寫成變量
using System.Collections.Generic; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; using DG.Tweening; using DG.Tweening.Core.Easing; using UnityEngine.Experimental.UIElements; public class ScorllViewAutoHandler : MonoBehaviour, IEndDragHandler, IBeginDragHandler, IDragHandler { private GameObject Scroll; private ScrollRect sc; private float OriginPosX; private float offsetX; private int CurIndex; private bool isDragging = false; private bool TimerFlag = false; private float Timer; // Start is called before the first frame update void Start() { CurIndex = 0; Scroll = this.gameObject; sc = Scroll.transform.parent.transform.parent.gameObject.GetComponent<ScrollRect>(); } public void OnEndDrag(PointerEventData eventData) { int tempIndex=0; sc.OnEndDrag(eventData); offsetX = this.transform.localPosition.x - OriginPosX; if (Mathf.Abs(offsetX) % 450 < 225) { tempIndex = (int)(Mathf.Abs(offsetX) / 450); int _TempTargetIndex = 0; if (offsetX <0) { _TempTargetIndex = CurIndex + tempIndex; } else { _TempTargetIndex = CurIndex - tempIndex; } if (_TempTargetIndex >= 0 && _TempTargetIndex <= Scroll.transform.childCount - 1) { _TempTargetIndex = _TempTargetIndex; } else if (_TempTargetIndex < 0) { _TempTargetIndex = 0; } else { _TempTargetIndex = Scroll.transform.childCount - 1; } Debug.LogError("本次位移目標(biāo)" + _TempTargetIndex + "初始" + CurIndex); this.transform.DOLocalMoveX(_TempTargetIndex*450, 0.5f).SetEase(Ease.OutBack); } else { tempIndex = (int)(Mathf.Abs(offsetX) / 450); tempIndex += 1; int _TempTargetIndex = 0; if (offsetX < 0) { _TempTargetIndex = CurIndex + tempIndex; } else { _TempTargetIndex = CurIndex - tempIndex; } if (_TempTargetIndex >= 0 && _TempTargetIndex <= Scroll.transform.childCount - 1) { _TempTargetIndex = _TempTargetIndex; } else if (_TempTargetIndex < 0) { _TempTargetIndex = 0; } else { _TempTargetIndex = Scroll.transform.childCount - 1; } Debug.LogError("本次位移目標(biāo)" + _TempTargetIndex + "初始" + CurIndex); this.transform.DOLocalMoveX(_TempTargetIndex*450, 0.5f).SetEase(Ease.OutBack); } } public void OnBeginDrag(PointerEventData eventData) { sc.OnBeginDrag(eventData); OriginPosX = this.transform.localPosition.x; CurIndex = (int)(Mathf.Abs(this.transform.localPosition.x) / 450); offsetX = 0; //當(dāng)鼠標(biāo)在A對象按下并開始拖拽時 A對象響應(yīng)此事件 // 此事件在OnInitializePotentialDrag之后響應(yīng) OnDrag之前響應(yīng) //Debug.Log("OnBeginDrag " ); } public void OnDrag(PointerEventData eventData) { sc.OnDrag(eventData); //Debug.LogError("Dragging" ); } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C# 使用鼠標(biāo)點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點擊對Chart控件實現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08C#讀取QQ純真IP數(shù)據(jù)庫QQWry.Dat的代碼
QQ純真IP庫算是IP地址收集較為全的一個IP庫,對于IP查詢來說這個是不錯的選擇。下面是如何查詢純真IP庫的一個類,使用C#代碼。2007-03-03C#/VB.NET 實現(xiàn)在PDF表格中添加條形碼
條碼的應(yīng)用已深入生活和工作的方方面面。在處理條碼時,常需要和各種文檔格式相結(jié)合。本文,以操作PDF文件為例,介紹如何在編輯表格時,向單元格中插入條形碼,需要的可以參考一下2022-06-06