No Description
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.cs 3.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 : MonoBehaviour, IBeginDragHandler, IEndDragHandler{
  8. public ScrollRect scrollRect;
  9. private Vector2 startDragPos; // 拖曳开始位置
  10. [SerializeField] TextMeshProUGUI 相片總數_tb,相片目前_tb,扶手長度_tb,報告書號_tb;
  11. private float currentPage=0;
  12. public float swipeThreshold = 200f;
  13. public static bool currentPageChange;
  14. void Update() {
  15. if (currentPageChange){
  16. currentPageChange=false;
  17. currentPage=0;
  18. }
  19. }
  20. public void OnBeginDrag(PointerEventData eventData){
  21. startDragPos = eventData.position;
  22. }
  23. public void OnEndDrag(PointerEventData eventData){
  24. float dragDistance = Mathf.Abs(eventData.position.x - startDragPos.x);
  25. if (dragDistance > swipeThreshold){
  26. if(eventData.position.x > startDragPos.x){
  27. ShowPreviousPage();
  28. }else{
  29. ShowNextPage();
  30. }
  31. }else{
  32. SnapToNearestPage();
  33. }
  34. }
  35. private void ShowPreviousPage()
  36. {
  37. if (currentPage > 0)
  38. {
  39. currentPage--;
  40. float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text)-1);
  41. StartCoroutine(SmoothScrollTo(normalizedPosition));
  42. UpdatePageIndicator();
  43. }
  44. }
  45. private void ShowNextPage() {
  46. if(相片總數_tb.text !="-"){
  47. if (currentPage < float.Parse(相片總數_tb.text) - 1){
  48. currentPage++;
  49. float normalizedPosition = (float)currentPage / (float)(float.Parse(相片總數_tb.text)-1 );
  50. StartCoroutine(SmoothScrollTo(normalizedPosition));
  51. UpdatePageIndicator();
  52. }
  53. }
  54. }
  55. private IEnumerator SmoothScrollTo(float targetNormalizedPosition)
  56. {
  57. while (Mathf.Abs(scrollRect.horizontalNormalizedPosition - targetNormalizedPosition) > 0.01f)
  58. {
  59. scrollRect.horizontalNormalizedPosition = Mathf.Lerp(scrollRect.horizontalNormalizedPosition, targetNormalizedPosition, 10f * Time.deltaTime);
  60. yield return null;
  61. }
  62. scrollRect.horizontalNormalizedPosition = targetNormalizedPosition;
  63. }
  64. private void SnapToNearestPage()
  65. {
  66. float targetNormalizedPosition = currentPage / (float)(float.Parse(相片總數_tb.text)-1);
  67. StartCoroutine(SmoothScrollTo(targetNormalizedPosition));
  68. }
  69. public void UpdatePageIndicator(){
  70. 相片目前_tb.text=(currentPage + 1).ToString();
  71. if (Main.Global.報告書施工狀態 == "施工前"){
  72. string 文件號 = Main.Global.報告書施工圖片_table.Rows[int.Parse(currentPage.ToString())][0].ToString();
  73. Main.Global.報告書施工圖檔文件號 = 文件號;
  74. SQL_Comm.SQL_報告書_施工圖檔扶手長度(報告書號_tb.text,Main.Global.報告書文件號,Main.Global.報告書物料料號,Main.Global.報告書物料明細流水號,文件號);
  75. if(SQL_Module.dr.Read()){
  76. 扶手長度_tb.text=SQL_Module.dr[0].ToString();
  77. }else{
  78. 扶手長度_tb.text="";
  79. }
  80. }else{
  81. 扶手長度_tb.text="";
  82. }
  83. }
  84. public static void 清除currentPage(){
  85. currentPageChange=true;
  86. }
  87. }