123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.SceneManagement;
- using UnityEngine.UI;
- using UnityEngine.Networking;
- using System.Text.RegularExpressions;
- using System.Data.SqlClient;
- using TMPro;
- using System;
- using System.IO;
- using ZXing;
- using ZXing.QrCode;
- using System.Net;
- using SimpleJSON;
- using System.ComponentModel;
- using System.Runtime.InteropServices;
-
- public class 書架 : MonoBehaviour
- {
-
- public RawImage rawImage;
-
- public RawImage rawImage1;
-
- public CanvasScaler canvasScaler;
- [SerializeField] GameObject 細板;
- [SerializeField] GameObject 寬板;
-
- [SerializeField] GameObject 回主頁紐;
- [SerializeField] GameObject 回選擇書籍紐;
- [SerializeField] GameObject 選擇頁面紐;
-
- [SerializeField] GameObject 選擇書籍;
-
- [SerializeField] GameObject 選擇頁面;
-
- [SerializeField] TextMeshProUGUI 書編;
- [SerializeField] TextMeshProUGUI 書名s;
- [SerializeField] TextMeshProUGUI 書頁名;
- [SerializeField] TMP_InputField 第幾頁;
-
- [SerializeField] TextMeshProUGUI 書編1;
- [SerializeField] TextMeshProUGUI 書名s1;
- [SerializeField] TextMeshProUGUI 書頁名1;
- [SerializeField] TMP_InputField 第幾頁1;
-
- [SerializeField] GameObject 讀取面板;
- [SerializeField] TextMeshProUGUI 讀取;
- [SerializeField] GameObject 彈跳面板;
- [SerializeField] TextMeshProUGUI 彈跳文字;
- private string loadingText = "";
- private int dotCount = 0;
- JSONArray 書籍清單;
- JSONArray 書籍內容清單;
-
- public GameObject buttonPrefab;
- public Transform buttonParent;
- public GameObject buttonceneter;
- private List<GameObject> copy = new List<GameObject>();
-
- public GameObject buttonPrefab1;
- public Transform buttonParent1;
- public GameObject buttonceneter1;
- private List<GameObject> copy1 = new List<GameObject>();
- int 最大頁數;
-
- [SerializeField] Slider scrollbar;
- [SerializeField] Slider scrollbar1;
- private float initialScale;
- private ScreenOrientation currentOrientation;
-
- private float 旋轉角度 = 0f;
- private float 旋轉角度1 = 0f;
-
- void Start()
- {
- //Main.Global.固寬=1000f;
- //Main.Global.固高=1600f;
- //Main.Global.固比=1f;
- //Main.Global.螢幕格式="胖";
- //Main.Global.sql="Data Source=180.177.181.162,5264;Initial Catalog=BIFENG-ERP-SYS;User ID=b70340;Password=0911274990";
-
- Screen.autorotateToPortrait = true;
- Screen.orientation = ScreenOrientation.AutoRotation;
- if (Main.Global.螢幕方向=="直")
- {
- canvasScaler.referenceResolution = new Vector2(Main.Global.固寬, Main.Global.固高);
- canvasScaler.matchWidthOrHeight=Main.Global.固比;
- }
- else
- {
- canvasScaler.referenceResolution = new Vector2(Main.Global.固高, Main.Global.固寬);
- canvasScaler.matchWidthOrHeight=Main.Global.固比1;
- }
- currentOrientation = Screen.orientation;
-
- if (Main.Global.螢幕格式=="胖")
- {
- initialScale = rawImage1.transform.localScale.x;
- if (Main.Global.PA1 == "")
- {
- 選擇書籍.SetActive(true);
- 回主頁紐.SetActive(true);
- 回選擇書籍紐.SetActive(false);
- 選擇頁面紐.SetActive(false);
- }
- else if (Main.Global.PA1 != "" && Main.Global.PA4 !="")
- {
-
- 選擇書籍.SetActive(false);
- string str = Main.Global.PA1;
- string str1 = Main.Global.PA4;
- 細板.SetActive(false);
- 寬板.SetActive(true);
- 書編1.text =Main.Global.PA1;
- 書名s1.text=Main.Global.PA2;
- 書頁名1.text=Main.Global.PA3;
- 第幾頁1.text=Main.Global.PA4;
- 選擇頁面.SetActive(false);
- 回主頁紐.SetActive(false);
- 回選擇書籍紐.SetActive(true);
- 選擇頁面紐.SetActive(true);
- 讀取書籍內容清單(str);
- 顯示圖片(str, str1);
- }
-
- }
- else
- {
- initialScale = rawImage.transform.localScale.x;
- if (Main.Global.PA1 == "")
- {
- 選擇書籍.SetActive(true);
- 回主頁紐.SetActive(true);
- 回選擇書籍紐.SetActive(false);
- 選擇頁面紐.SetActive(false);
- }
- else if (Main.Global.PA1 != "" && Main.Global.PA4 !="")
- {
-
- 選擇書籍.SetActive(false);
- string str = Main.Global.PA1;
- string str1 = Main.Global.PA4;
- 細板.SetActive(true);
- 寬板.SetActive(false);
- 書編.text =Main.Global.PA1;
- 書名s.text=Main.Global.PA2;
- 書頁名.text=Main.Global.PA3;
- 第幾頁.text=Main.Global.PA4;
- 選擇頁面.SetActive(false);
- 回主頁紐.SetActive(false);
- 回選擇書籍紐.SetActive(true);
- 選擇頁面紐.SetActive(true);
- 讀取書籍內容清單(str);
- 顯示圖片(str, str1);
- }
- }
-
- StartCoroutine(AnimateText());
-
- 讀取書籍清單();
- }
- void Update()
- {
-
- if (Screen.orientation != currentOrientation)
- {
- if (Screen.orientation == ScreenOrientation.LandscapeLeft)
- {
- Main.Global.螢幕方向="橫";
- if (Main.Global.螢幕格式=="胖")
- {
-
- Main.Global.PA1=書編1.text;
- Main.Global.PA2=書名s1.text;
- Main.Global.PA3=書頁名1.text;
- Main.Global.PA4=第幾頁1.text;
- }
- else
- {
- Main.Global.PA1=書編.text;
- Main.Global.PA2=書名s.text;
- Main.Global.PA3=書頁名.text;
- Main.Global.PA4=第幾頁.text;
- }
- change_Secen(4);
- }
- if (Screen.orientation == ScreenOrientation.Portrait)
- {
- if (Main.Global.螢幕格式=="胖")
- {
- Main.Global.PA1=書編1.text;
- Main.Global.PA2=書名s1.text;
- Main.Global.PA3=書頁名1.text;
- Main.Global.PA4=第幾頁1.text;
- }
- else
- {
- Main.Global.PA1=書編.text;
- Main.Global.PA2=書名s.text;
- Main.Global.PA3=書頁名.text;
- Main.Global.PA4=第幾頁.text;
- }
- Main.Global.螢幕方向="直";
- change_Secen(2);
- }
- }
- }
- IEnumerator AnimateText()
- {
- string qqq = 讀取.text.ToString();
- while (true)
- {
- yield return new WaitForSeconds(0.5f);
-
- if (dotCount < 3)
- {
- loadingText += ".";
- dotCount++;
- }
- else
- {
- loadingText = "";
- dotCount = 0;
- }
-
- 讀取.text = qqq+loadingText;
- }
- }
- public void 讀取書籍清單()
- {
- 讀取面板.SetActive(true);
- 書籍清單 = new JSONArray();
- using (SqlConnection connection = new SqlConnection(Main.Global.sql))
- {
- try
- {
- connection.Open();
-
- string query = "SELECT 書名, 書籍編號 FROM 書籍資料庫 GROUP BY 書籍編號, 書名 ORDER BY 書籍編號";
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- using (SqlDataReader reader = command.ExecuteReader())
- {
- while (reader.Read()) // 假設只取前兩筆資料
- {
- JSONNode newValue = new JSONObject();
- newValue["書名"] = reader["書名"].ToString();
- newValue["書籍編號"] = reader["書籍編號"].ToString();
- 書籍清單.Add(newValue);
- }
- }
- }
- }
- catch (Exception ex)
- {
- Debug.LogError("Error connecting to the database: " + ex.Message);
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
- }
- finally
- {
- connection.Close();
-
- }
- }
- int sum = 5;
- for (int i = 0; i < 書籍清單.Count; i++)
- {
- GameObject buttonObj = Instantiate(buttonPrefab) as GameObject;
- buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
- buttonObj.transform.SetParent(buttonParent, false);
- TextMeshProUGUI text = buttonObj.GetComponentInChildren<TextMeshProUGUI>();
- Button bt = buttonObj.GetComponentInChildren<Button>();
- int index = i;
- bt.onClick.AddListener(() =>
- {
- 選擇書籍.SetActive(false);
- string str = 書籍清單[index][1].ToString().Trim('"');
- if (Main.Global.螢幕格式=="胖")
- {
- 寬板.SetActive(true);
- 細板.SetActive(false);
- 書編1.text =書籍清單[index][1].ToString().Trim('"');
- 書名s1.text=書籍清單[index][0].ToString().Trim('"');
- }
- else
- {
- 細板.SetActive(true);
- 寬板.SetActive(false);
- 書編.text =書籍清單[index][1].ToString().Trim('"');
- 書名s.text=書籍清單[index][0].ToString().Trim('"');
- }
- 選擇頁面.SetActive(false);
- 回主頁紐.SetActive(false);
- 回選擇書籍紐.SetActive(true);
- 選擇頁面紐.SetActive(true);
-
-
- 讀取書籍內容清單(str);
- 顯示圖片(str, "1");
- });
- text.text = 書籍清單[i][0].ToString().Trim('"');
- sum+=120;
- copy.Add(buttonObj);
- }
- buttonceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter.GetComponent<RectTransform>().sizeDelta.x, sum);
- 讀取面板.SetActive(false);
- }
- public void 讀取書籍內容清單(string 書名)
- {
- 讀取面板.SetActive(true);
- 書籍內容清單 = new JSONArray();
- using (SqlConnection connection = new SqlConnection(Main.Global.sql))
- {
- try
- {
- connection.Open();
-
- string query = "SELECT 書頁名, 頁數, 備註 FROM 書籍資料庫 WHERE (書籍編號 LIKE '" + 書名 + "') ORDER BY 頁數";
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- using (SqlDataReader reader = command.ExecuteReader())
- {
- while (reader.Read()) // 假設只取前兩筆資料
- {
- JSONNode newValue = new JSONObject();
- newValue["書頁名"] = reader["書頁名"].ToString();
- newValue["頁數"] = reader["頁數"].ToString();
- newValue["備註"] = reader["備註"].ToString();
- 書籍內容清單.Add(newValue);
- }
- }
- }
- }
- catch (Exception ex)
- {
- Debug.LogError("Error connecting to the database: " + ex.Message);
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
- }
- finally
- {
- connection.Close();
-
- }
- }
- if (Main.Global.PA4 =="")
- {
- if (Main.Global.螢幕格式=="胖")
- {
- 書頁名1.text=書籍內容清單[0][0];
- 第幾頁1.text=書籍內容清單[0][1];
- }
- else
- {
- 書頁名.text=書籍內容清單[0][0];
- 第幾頁.text=書籍內容清單[0][1];
- }
- }
- 最大頁數 =書籍內容清單.Count;
- if (copy1 != null)
- {
- DeletetextCopies1();
- }
- int sum = 5;
- for (int i = 0; i < 書籍內容清單.Count; i++)
- {
- GameObject buttonObj = Instantiate(buttonPrefab1) as GameObject;
- buttonObj.GetComponent<RectTransform>().anchoredPosition = new Vector2(2, -sum);
- buttonObj.transform.SetParent(buttonParent1, false);
- TextMeshProUGUI[] text = buttonObj.GetComponentsInChildren<TextMeshProUGUI>();
- Button bt = buttonObj.GetComponentInChildren<Button>();
- int index = i;
- bt.onClick.AddListener(() =>
- {
- 選擇頁面.SetActive(!選擇頁面.activeSelf);
- string str1 = 書籍內容清單[index][1].ToString().Trim('"');
- if (Main.Global.螢幕格式=="胖")
- {
- 寬板.SetActive(!寬板.activeSelf);
- string str = 書編1.text;
- 書頁名1.text=書籍內容清單[index][0].ToString().Trim('"');
- 第幾頁1.text=書籍內容清單[index][1].ToString().Trim('"');
- 顯示圖片(str, str1);
- }
- else
- {
- 細板.SetActive(!細板.activeSelf);
- string str = 書編.text;
- 書頁名.text=書籍內容清單[index][0].ToString().Trim('"');
- 第幾頁.text=書籍內容清單[index][1].ToString().Trim('"');
- 顯示圖片(str, str1);
- }
-
- });
- text[0].text = 書籍內容清單[i][0].ToString().Trim('"');
- text[1].text = 書籍內容清單[i][1].ToString().Trim('"');
- sum+=120;
- copy1.Add(buttonObj);
- }
- buttonceneter1.GetComponent<RectTransform>().sizeDelta = new Vector2(buttonceneter1.GetComponent<RectTransform>().sizeDelta.x, sum);
- 讀取面板.SetActive(false);
- }
- public void 顯示圖片(string 書名 , string 頁數)
- {
- rawImage.texture =Texture2D.blackTexture;
- 讀取面板.SetActive(true);
- using (SqlConnection connection = new SqlConnection(Main.Global.sql))
- {
- try
- {
- connection.Open();
-
- string query = "SELECT 圖片 FROM 書籍資料庫 WHERE 書籍編號 LIKE N'" + 書名 + "' AND 頁數 LIKE N'" + 頁數 + "'";
- Debug.Log(query);
- using (SqlCommand command = new SqlCommand(query, connection))
- {
- using (SqlDataReader reader = command.ExecuteReader())
- {
- while (reader.Read()) // 假設只取前兩筆資料
- {
- byte[] bytes = (byte[])reader["圖片"];
- Texture2D texture = new Texture2D(2, 2);
- texture.LoadImage(bytes);
-
- if (Main.Global.螢幕方向=="直")
- {
- if (Main.Global.螢幕格式=="胖")
- {
- rawImage1.texture = texture;
- float aspectRatio = (float)texture.width / texture.height;
- float newWidth = Mathf.Min(1000f, rawImage1.rectTransform.rect.width);
- float newHeight = newWidth / aspectRatio;
- rawImage1.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
- }
- else
- {
- rawImage.texture = texture;
- float aspectRatio = (float)texture.width / texture.height;
- float newWidth = Mathf.Min(900f, rawImage.rectTransform.rect.width);
- float newHeight = newWidth / aspectRatio;
- rawImage.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
- }
- }
- else
- {
- if (Main.Global.螢幕格式=="胖")
- {
- rawImage1.texture = texture;
- float aspectRatio = (float)texture.width / texture.height;
- float newHeight = Mathf.Min(850f, rawImage1.rectTransform.rect.height);
- float newWidth = newHeight * aspectRatio;
- rawImage1.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
- }
- else
- {
- rawImage.texture = texture;
- float aspectRatio = (float)texture.width / texture.height;
- float newHeight = Mathf.Min(750f, rawImage.rectTransform.rect.height);
- float newWidth = newHeight * aspectRatio;
- rawImage.rectTransform.sizeDelta = new Vector2(newWidth, newHeight);
- }
- }
-
-
-
- }
- }
- }
- }
- catch (Exception ex)
- {
- Debug.LogError("Error connecting to the database: " + ex.Message);
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "目前無法連到伺服器,請通知系統管理員";
- }
- finally
- {
- connection.Close();
-
- }
- }
- 讀取面板.SetActive(false);
- }
- public void 選單按鈕()
- {
- if (Main.Global.螢幕格式=="胖")
- {
- 寬板.SetActive(!寬板.activeSelf);
- }
- else
- {
- 細板.SetActive(!細板.activeSelf);
- }
- 選擇頁面.SetActive(!選擇頁面.activeSelf);
- }
-
- public void 倍率改變()
- {
- float scaleFactor = scrollbar.value;
- rawImage.transform.localScale = new Vector3(initialScale * scaleFactor, initialScale * scaleFactor, 1f);
- }
- public void 倍率改變1()
- {
- float scaleFactor = scrollbar1.value;
- rawImage1.transform.localScale = new Vector3(initialScale * scaleFactor, initialScale * scaleFactor, 1f);
- }
- public void DeletetextCopies1()
- {
- // 刪除複製出來的物件
- foreach (GameObject copy in copy1)
- {
- Destroy(copy);
- }
- copy1.Clear();
- }
- public void 上頁()
- {
- int currentPage = 0;
- if (Main.Global.螢幕格式=="胖")
- {
- currentPage = int.Parse(第幾頁1.text);
- }
- else
- {
- currentPage = int.Parse(第幾頁.text);
- }
- if (currentPage < 1 || currentPage > 最大頁數)
- {
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "超過最大頁數,或小於最小頁數";
- }
- else
- {
- int newPage = Mathf.Max(1, currentPage - 1); // 確保不會小於 1
- if (Main.Global.螢幕格式=="胖")
- {
- 書頁名1.text=書籍內容清單[newPage-1][0];
- 第幾頁1.text = newPage.ToString();
- string str = 書編1.text;
- string str1 = 第幾頁1.text;
- 顯示圖片(str, str1);
- }
- else
- {
- 書頁名.text=書籍內容清單[newPage-1][0];
- 第幾頁.text = newPage.ToString();
- string str = 書編.text;
- string str1 = 第幾頁.text;
- 顯示圖片(str, str1);
- }
-
- }
-
- }
- public void 下頁()
- {
- int currentPage = 0;
- if (Main.Global.螢幕格式=="胖")
- {
- currentPage = int.Parse(第幾頁1.text);
- }
- else
- {
- currentPage = int.Parse(第幾頁.text);
- }
- if (currentPage < 1 || currentPage > 最大頁數)
- {
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "超過最大頁數,或小於最小頁數";
- }
- else
- {
- int newPage = Mathf.Min(最大頁數, currentPage + 1); // 確保不會超過最大頁數
- if (Main.Global.螢幕格式=="胖")
- {
- 書頁名1.text=書籍內容清單[newPage-1][0];
- 第幾頁1.text = newPage.ToString();
- string str = 書編1.text;
- string str1 = 第幾頁1.text;
- 顯示圖片(str, str1);
- }
- else
- {
- 書頁名.text=書籍內容清單[newPage-1][0];
- 第幾頁.text = newPage.ToString();
- string str = 書編.text;
- string str1 = 第幾頁.text;
- 顯示圖片(str, str1);
- }
-
- }
-
- }
- public void 指定()
- {
- int currentPage = 0;
- if (Main.Global.螢幕格式=="胖")
- {
- currentPage = int.Parse(第幾頁1.text);
- }
- else
- {
- currentPage = int.Parse(第幾頁.text);
- }
- if (currentPage < 1 || currentPage > 最大頁數)
- {
- 彈跳面板.SetActive(true);
- 彈跳文字.text = "超過最大頁數,或小於最小頁數";
- }
- else
- {
- if (Main.Global.螢幕格式=="胖")
- {
- 書頁名1.text=書籍內容清單[currentPage-1][0];
- string str = 書編1.text;
- string str1 = 第幾頁1.text;
- 顯示圖片(str, str1);
- }
- else
- {
- 書頁名.text=書籍內容清單[currentPage-1][0];
- string str = 書編.text;
- string str1 = 第幾頁.text;
- 顯示圖片(str, str1);
- }
-
- }
-
- }
- public void 彈跳面板關閉()
- {
- 彈跳面板.SetActive(false);
- 彈跳文字.text = "";
- }
- public void 回選書()
- {
- 選擇書籍.SetActive(true);
- 細板.SetActive(false);
- 寬板.SetActive(false);
- 選擇頁面.SetActive(false);
- 回主頁紐.SetActive(true);
- 回選擇書籍紐.SetActive(false);
- 選擇頁面紐.SetActive(false);
- 書編1.text="";
- 書名s1.text="";
- 書頁名1.text="";
- 第幾頁1.text="";
- 書編.text="";
- 書名s.text="";
- 書頁名.text="";
- 第幾頁.text="";
- }
- public void 回主()
- {
- if (Main.Global.螢幕方向=="直")
- {
- change_Secen(1);
- }
- else
- {
- change_Secen(3);
- }
- }
- public void change_Secen(int _screenNum)
- {
-
- SceneManager.LoadScene(_screenNum);
- }
- public void 截圖()
- {
- string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
- ScreenCapture.CaptureScreenshot(fileName);
- }
-
- }
|