using System; using System.Data; using System.Collections.Generic; using TMPro; using XCharts.Runtime; using UnityEngine; using UnityEngine.UI; public class Moudle : MonoBehaviour { //==========Moudle=========================== public static void 載入日期(int num,TMP_Dropdown 開始年_cb,TMP_Dropdown 開始月_cb,TMP_Dropdown 開始日_cb){ Main.Global.目前開始月份[num] = 開始月_cb.options[開始月_cb.value].text; Main.Global.目前開始年度[num] = 開始年_cb.options[開始年_cb.value].text; if(Main.Global.上次開始月份[num] != Main.Global.目前開始月份[num] || Main.Global.上次開始年度[num] != Main.Global.目前開始年度[num]){ Main.Global.上次開始月份[num] = Main.Global.目前開始月份[num]; Main.Global.上次開始年度[num] = Main.Global.目前開始年度[num]; List 日期Options = new(){ "" }; int N_year,N_month; N_year = int.Parse(開始年_cb.options[開始年_cb.value].text); if(開始月_cb.value==0){ N_month = 1; } else{ N_month = 開始月_cb.value; } for(int i=1;i<= DateTime.DaysInMonth(N_year,N_month);i++){ DateTime day2 = new (N_year ,N_month ,i); 日期Options.Add(i.ToString()+","+day2.DayOfWeek); } 開始日_cb.ClearOptions(); 開始日_cb.AddOptions(日期Options); 開始日_cb.value=0; } } //=========================================== public static void 載入表格(string table_name,DataTable table,Transform tableParent,GameObject rowPrefab,GameObject ceneter,TMP_FontAsset myFont,Sprite UIsprite,GameObject LinePrefab,bool overflowMode){ 清除控件(ceneter); Image centerImage = ceneter.GetComponent(); if (centerImage == null) { centerImage = ceneter.AddComponent(); centerImage.type = Image.Type.Sliced; } centerImage.sprite = UIsprite; centerImage.fillCenter = false; centerImage.color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f); //==========動態生成表格================================== GameObject headerRow = Instantiate(rowPrefab, tableParent); // 設定表頭行的位置 headerRow.GetComponent().anchoredPosition = Vector2.zero; headerRow.AddComponent().type=Image.Type.Sliced; headerRow.GetComponent().sprite=UIsprite; headerRow.GetComponent().fillCenter=false; headerRow.GetComponent().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f); float width = headerRow.GetComponent().rect.width / table.Columns.Count ; //headerRow.AddComponent().color=new Color(0f,0.67f,0.87f,1f); // 取得所有欄位名稱,並建立表頭 for (int i = 0; i < table.Columns.Count; i++) { GameObject headerText = new GameObject(table_name+"_Header_"+i.ToString()); headerText.transform.SetParent(headerRow.transform, false); headerText.AddComponent().text = table.Columns[i].ColumnName; headerText.GetComponent().tag = "Header"; } GridLayoutGroup gridLayout = headerRow.GetComponent(); gridLayout.cellSize =new Vector2(width, 70); for (int i = 0; i < table.Rows.Count; i++){ // 建立資料列 GameObject row = Instantiate(rowPrefab, tableParent); // 設定資料列位置 row.GetComponent().anchoredPosition = new Vector2(0, -70 * (i + 1)); // 建立資料列中的所有欄位 for (int j = 0; j < table.Columns.Count; j++) { // 取得欄位值 string value = table.Rows[i][j].ToString(); if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)){ Toggle cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString()).AddComponent(); cellText.transform.SetParent(row.transform, false); cellText.tag = "Toggle"; Image Background_img = new GameObject("bg"+i.ToString()).AddComponent(); Background_img.transform.SetParent(cellText.transform, false); Background_img.sprite = Main.Global.sprite_box; Background_img.rectTransform.sizeDelta = new Vector2(100f,70f); Image Check_img = new GameObject("Check"+i.ToString()).AddComponent(); Check_img.transform.SetParent(cellText.transform, false); Check_img.rectTransform.sizeDelta = new Vector2(100f,70f); Check_img.sprite = Main.Global.sprite_check; cellText.graphic = Check_img; cellText.targetGraphic=Background_img; if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)){ cellText.isOn=true; } }else{ GameObject cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString()); cellText.transform.SetParent(row.transform, false); double number; if (double.TryParse(value, out number) && table_name=="區間"){ string formattedValue = String.Format("{0:###,##0.##}", number); cellText.AddComponent().text = formattedValue; }else{ cellText.AddComponent().text = value; } //cellText.AddComponent().text = value; cellText.GetComponent().tag="Cells"; if(overflowMode){cellText.GetComponent().overflowMode=TextOverflowModes.Ellipsis;} cellText.AddComponent