설명 없음
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PhotoScroll_bt.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. public class PhotoScroll_bt : MonoBehaviour, IBeginDragHandler, IEndDragHandler
  8. {
  9. public ScrollRect scrollRect;
  10. private Vector2 startDragPos;
  11. [SerializeField] TextMeshProUGUI 相片總數_tb, 相片目前_tb, 報告書號_tb, 文件號_tb;
  12. [SerializeField] GameObject 前一張_bt, 後一張_bt;
  13. private float currentPage = 0;
  14. public float swipeThreshold = 200f;
  15. public static bool currentPageChange;
  16. void Start()
  17. {
  18. // 綁定按鈕事件
  19. 前一張_bt.GetComponent<Button>().onClick.AddListener(ShowPreviousPage);
  20. 後一張_bt.GetComponent<Button>().onClick.AddListener(ShowNextPage);
  21. }
  22. void Update()
  23. {
  24. if (currentPageChange)
  25. {
  26. currentPageChange = false;
  27. currentPage = 0;
  28. UpdatePageIndicator();
  29. UpdateButtonState();
  30. }
  31. }
  32. public void OnBeginDrag(PointerEventData eventData)
  33. {
  34. startDragPos = eventData.position;
  35. }
  36. public void OnEndDrag(PointerEventData eventData)
  37. {
  38. float dragDistance = Mathf.Abs(eventData.position.x - startDragPos.x);
  39. if (dragDistance > swipeThreshold)
  40. {
  41. if (eventData.position.x > startDragPos.x)
  42. {
  43. ShowPreviousPage();
  44. }
  45. else
  46. {
  47. ShowNextPage();
  48. }
  49. }
  50. else
  51. {
  52. SnapToNearestPage();
  53. }
  54. }
  55. private void ShowPreviousPage()
  56. {
  57. if (currentPage > 0)
  58. {
  59. currentPage--;
  60. UpdatePageScroll();
  61. }
  62. }
  63. private void ShowNextPage()
  64. {
  65. if (相片總數_tb.text != "-")
  66. {
  67. if (currentPage < float.Parse(相片總數_tb.text) - 1)
  68. {
  69. currentPage++;
  70. UpdatePageScroll();
  71. }
  72. }
  73. }
  74. private void UpdatePageScroll()
  75. {
  76. float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text) - 1);
  77. StartCoroutine(SmoothScrollTo(normalizedPosition));
  78. UpdatePageIndicator();
  79. UpdateButtonState();
  80. }
  81. private IEnumerator SmoothScrollTo(float targetNormalizedPosition)
  82. {
  83. while (Mathf.Abs(scrollRect.horizontalNormalizedPosition - targetNormalizedPosition) > 0.01f)
  84. {
  85. scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetNormalizedPosition, 10f * Time.deltaTime);
  86. yield return null;
  87. }
  88. scrollRect.horizontalNormalizedPosition = targetNormalizedPosition;
  89. }
  90. private void SnapToNearestPage()
  91. {
  92. float targetNormalizedPosition = currentPage / (float)(float.Parse(相片總數_tb.text) - 1);
  93. StartCoroutine(SmoothScrollTo(targetNormalizedPosition));
  94. }
  95. public void UpdatePageIndicator()
  96. {
  97. 相片目前_tb.text = (currentPage + 1).ToString();
  98. string 文件號;
  99. if(相片總數_tb.name.Contains("相關文件")){
  100. 文件號 = Main.Global.報告書相關文件_table.Rows[int.Parse(currentPage.ToString())][0].ToString();
  101. }else{
  102. 文件號 = Main.Global.報告書施作圖片_table.Rows[int.Parse(currentPage.ToString())][0].ToString();
  103. }
  104. 文件號_tb.text = 文件號;
  105. }
  106. private void UpdateButtonState()
  107. {
  108. // 當是第一張時,禁用「上一張」
  109. 前一張_bt.GetComponent<Button>().interactable = currentPage > 0;
  110. // 當是最後一張時,禁用「下一張」
  111. 後一張_bt.GetComponent<Button>().interactable = currentPage < float.Parse(相片總數_tb.text) - 1;
  112. }
  113. public static void 清除currentPage()
  114. {
  115. currentPageChange = true;
  116. }
  117. }