123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- using System.Collections;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.EventSystems;
- using UnityEngine.UI;
-
- public class PhotoScroll_bt : MonoBehaviour, IBeginDragHandler, IEndDragHandler
- {
- public ScrollRect scrollRect;
- private Vector2 startDragPos;
-
- [SerializeField] TextMeshProUGUI 相片總數_tb, 相片目前_tb, 報告書號_tb, 文件號_tb;
- [SerializeField] GameObject 前一張_bt, 後一張_bt;
-
- private float currentPage = 0;
- public float swipeThreshold = 200f;
- public static bool currentPageChange;
-
- void Start()
- {
- // 綁定按鈕事件
- 前一張_bt.GetComponent<Button>().onClick.AddListener(ShowPreviousPage);
- 後一張_bt.GetComponent<Button>().onClick.AddListener(ShowNextPage);
- }
-
- void Update()
- {
- if (currentPageChange)
- {
- currentPageChange = false;
- currentPage = 0;
- UpdatePageIndicator();
- UpdateButtonState();
- }
- }
-
- 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--;
- UpdatePageScroll();
- }
- }
-
- private void ShowNextPage()
- {
- if (相片總數_tb.text != "-")
- {
- if (currentPage < float.Parse(相片總數_tb.text) - 1)
- {
- currentPage++;
- UpdatePageScroll();
- }
- }
- }
-
- private void UpdatePageScroll()
- {
- float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text) - 1);
- StartCoroutine(SmoothScrollTo(normalizedPosition));
- UpdatePageIndicator();
- UpdateButtonState();
- }
-
- 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();
- string 文件號;
- if(相片總數_tb.name.Contains("相關文件")){
- 文件號 = Main.Global.報告書相關文件_table.Rows[int.Parse(currentPage.ToString())][0].ToString();
- }else{
- 文件號 = Main.Global.報告書施作圖片_table.Rows[int.Parse(currentPage.ToString())][0].ToString();
- }
-
-
- 文件號_tb.text = 文件號;
- }
-
- private void UpdateButtonState()
- {
- // 當是第一張時,禁用「上一張」
- 前一張_bt.GetComponent<Button>().interactable = currentPage > 0;
- // 當是最後一張時,禁用「下一張」
- 後一張_bt.GetComponent<Button>().interactable = currentPage < float.Parse(相片總數_tb.text) - 1;
- }
-
- public static void 清除currentPage()
- {
- currentPageChange = true;
- }
- }
|