using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.EventSystems; using UnityEngine.UI; public class PhotoScroll : MonoBehaviour, IBeginDragHandler, IEndDragHandler{ public ScrollRect scrollRect; private Vector2 startDragPos; // 拖曳开始位置 [SerializeField] TextMeshProUGUI 相片總數_tb,相片目前_tb,扶手長度_tb,報告書號_tb; private float currentPage=0; public float swipeThreshold = 200f; public static bool currentPageChange; void Update() { if (currentPageChange){ currentPageChange=false; currentPage=0; } } public void OnBeginDrag(PointerEventData eventData){ startDragPos = eventData.position; } public void OnEndDrag(PointerEventData eventData){ float dragDistance = Mathf.Abs(eventData.position.x - startDragPos.x); if (dragDistance > swipeThreshold){ if(eventData.position.x > startDragPos.x){ ShowPreviousPage(); }else{ ShowNextPage(); } }else{ SnapToNearestPage(); } } private void ShowPreviousPage() { if (currentPage > 0) { currentPage--; float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text)-1); StartCoroutine(SmoothScrollTo(normalizedPosition)); UpdatePageIndicator(); } } private void ShowNextPage() { if(相片總數_tb.text !="-"){ if (currentPage < float.Parse(相片總數_tb.text) - 1){ currentPage++; float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text)-1 ); StartCoroutine(SmoothScrollTo(normalizedPosition)); UpdatePageIndicator(); } } } private IEnumerator SmoothScrollTo(float targetNormalizedPosition) { while (Mathf.Abs(scrollRect.horizontalNormalizedPosition - targetNormalizedPosition) > 0.01f) { scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetNormalizedPosition, 10f * Time.deltaTime); yield return null; } scrollRect.horizontalNormalizedPosition = targetNormalizedPosition; } private void SnapToNearestPage() { float targetNormalizedPosition = currentPage / (float)(float.Parse(相片總數_tb.text)-1); StartCoroutine(SmoothScrollTo(targetNormalizedPosition)); } public void UpdatePageIndicator(){ 相片目前_tb.text=(currentPage + 1).ToString(); if (Main.Global.報告書施工狀態 == "施工前"){ string 文件號 = Main.Global.報告書施工圖片_table.Rows[int.Parse(currentPage.ToString())][0].ToString(); Main.Global.報告書施工圖檔文件號 = 文件號; SQL_Comm.SQL_報告書_施工圖檔扶手長度(報告書號_tb.text,Main.Global.報告書文件號,Main.Global.報告書物料料號,Main.Global.報告書物料明細流水號,文件號); if(SQL_Module.dr.Read()){ 扶手長度_tb.text=SQL_Module.dr[0].ToString(); }else{ 扶手長度_tb.text=""; } }else{ 扶手長度_tb.text=""; } } public static void 清除currentPage(){ currentPageChange=true; } }