using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using UnityEngine.Networking; using UnityEngine.EventSystems; using System.Text.RegularExpressions; using System.Data.SqlClient; using System.Runtime.InteropServices; using System.Text; using TMPro; using System; using System.IO; using System.Net; using System.Data; using SimpleJSON; using System.Linq; using System.ComponentModel; using System.Runtime.InteropServices; using System.Globalization; using static System.Net.WebRequestMethods; public class Main : MonoBehaviour { [SerializeField] GameObject 面板1; [SerializeField] GameObject 面板2; [SerializeField] GameObject 讀取面板; [SerializeField] TextMeshProUGUI 讀取; [SerializeField] GameObject 股東帳1; [SerializeField] Toggle 股東帳; [SerializeField] TextMeshProUGUI 天; [SerializeField] TextMeshProUGUI 周; [SerializeField] TextMeshProUGUI 月; [SerializeField] TextMeshProUGUI 天1; [SerializeField] TextMeshProUGUI 周1; [SerializeField] TextMeshProUGUI 月1; public GameObject buttonPrefab; public ScrollRect buttonscroll; private List copy = new List(); public GameObject buttonPrefab1; public ScrollRect buttonscroll1; private List copy1 = new List(); public CanvasScaler canvasScaler; private float timer = 0f; private float interval = 60f; float screenWidth; float screenHeight; private string loadingText = ""; private int dotCount = 0; string 選擇日期; string 本周一; string 月初; string 年初; string 開始日期; string 結束日期; string PA2; JSONArray 彙總1; JSONNode 彙總2; JSONNode 彙總3; JSONNode 彙總4; JSONNode 彙總5; JSONNode 彙總6; JSONNode 彙總7; string 路徑1; // Start is called before the first frame update void Start() { Screen.autorotateToPortrait = false; Screen.orientation = ScreenOrientation.Portrait; screenWidth = Screen.width; screenHeight = Screen.height; float rate = 900.0f / 1900.0f; if (canvasScaler == null) { canvasScaler = GetComponent(); } Main.Global.rate = screenWidth/screenHeight; if (Main.Global.rate>rate) { canvasScaler.matchWidthOrHeight = 1f; } else { canvasScaler.matchWidthOrHeight = 0f; } StartCoroutine(AnimateText()); 讀取面板.SetActive(true); 面板1.SetActive(true); 面板2.SetActive(false); if (Main.Global.股東) { 股東帳1.SetActive(true); } else { 股東帳1.SetActive(false); } AdManager.Instance.ShowInterstitialAd(); StartCoroutine(讀取IP()); } private void 輸入日期(string date) { DateTime today = DateTime.ParseExact(date, "yyyy/MM/dd", null); DateTime yearStart = new DateTime(today.Year, 1, 1); DateTime monthStart = new DateTime(today.Year, today.Month, 1); int daysUntilMonday = ((int)DayOfWeek.Monday - (int)today.DayOfWeek + 7) % 7; if (daysUntilMonday == 0) { daysUntilMonday = 7; // 如果差距為 0,則禮拜一仍在本周 } DateTime monday = today.AddDays(daysUntilMonday-7); 選擇日期 = today.ToString("yyyy/MM/dd"); 年初 = yearStart.ToString("yyyy/MM/dd"); 月初 = monthStart.ToString("yyyy/MM/dd"); 本周一= monday.ToString("yyyy/MM/dd"); } 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; } } IEnumerator 讀取IP() { string strcon = "讀取IP"; string strUrl = string.Format(Main.Global.阿帕契路徑 + "comm={0}", strcon); Debug.Log(strUrl); UnityWebRequest request = UnityWebRequest.Get(strUrl); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError) { //Debug.Log(request.error); } string query = request.downloadHandler.text.ToString(); using (SqlConnection connection = new SqlConnection(Main.Global.路徑)) { try { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { if (reader.Read()) { string pa = reader.GetString("資料庫"); string pa1 = reader.GetString("IP位置"); string pa2 = reader.GetString("帳號"); string pa3 = reader.GetString("密碼"); 路徑1 = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3}", pa1, pa, pa2, pa3); Debug.Log(路徑1); StartCoroutine(日期清單()); } } } } catch (Exception ex) { Debug.LogError("Error connecting to the database: " + ex.Message); } finally { connection.Close(); } } } IEnumerator 日期清單() { string strcon = "日期清單"; string strUrl = string.Format(Main.Global.阿帕契路徑+ "comm={0}", strcon); UnityWebRequest request = UnityWebRequest.Get(strUrl); yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.ConnectionError) { //Debug.Log(request.error); } string query = request.downloadHandler.text.ToString(); Debug.Log(query); Debug.Log(路徑1); JSONArray json = new JSONArray(); using (SqlConnection connection = new SqlConnection(路徑1)) { try { connection.Open(); using (SqlCommand command = new SqlCommand(query, connection)) { using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) // 逐行讀取資料 { JSONNode jsona = new JSONObject(); // 創建 JSON 物件來存放每一行的資料 for (int i = 0; i < reader.FieldCount; i++) { string columnName = reader.GetName(i); // 取得欄位名稱 string value = reader.GetValue(i).ToString(); // 取得欄位值,並轉換成字串形式 jsona[columnName] = value; // 將欄位名稱和值加入 JSON 物件中 } json.Add(jsona); // 將整個 JSON 物件加入 JSON Array 中 } } int sum = 0; for (int i = 0; i < json.Count; i++) { GameObject buttonObj = Instantiate(buttonPrefab) as GameObject; buttonObj.GetComponent().anchoredPosition = new Vector2(3, -sum); Transform tran = buttonscroll.content; buttonObj.transform.SetParent(tran, false); TextMeshProUGUI[] texts = buttonObj.GetComponentsInChildren(); texts[0].text = json[i][1].ToString().Trim('"'); texts[1].text = json[i][2].ToString().Trim('"'); Button bt = buttonObj.GetComponentInChildren