暫無描述
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.

Module.cs 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. using System;
  2. using System.Data;
  3. using System.Collections.Generic;
  4. using TMPro;
  5. using UnityEngine;
  6. using UnityEngine.UI;
  7. public class Module : MonoBehaviour
  8. {
  9. public static void 載入表格(string table_name,DataTable table,Transform tableParent,GameObject rowPrefab,GameObject ceneter,TMP_FontAsset myFont,Sprite UIsprite,
  10. GameObject LinePrefab,bool overflowMode){
  11. 清除控件(ceneter);
  12. Image centerImage = ceneter.GetComponent<Image>();
  13. if(centerImage == null){
  14. centerImage = ceneter.AddComponent<Image>();
  15. centerImage.type = Image.Type.Sliced;
  16. }
  17. centerImage.sprite = UIsprite;
  18. centerImage.fillCenter = false;
  19. centerImage.color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
  20. //==========動態生成表格==================================
  21. for (int i = 0; i < table.Rows.Count; i++){
  22. // 建立資料列
  23. GameObject row = Instantiate(rowPrefab, tableParent);
  24. // 設定資料列位置
  25. row.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -90 * i);
  26. // 建立資料列中的所有欄位
  27. for (int j = 0; j < table.Columns.Count; j++){
  28. // 取得欄位值
  29. string value = table.Rows[i][j].ToString();
  30. if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase) || string.Equals(value, "false", StringComparison.OrdinalIgnoreCase)){
  31. Toggle cellText = new GameObject(table_name+"_Cells_"+i.ToString()+"_"+j.ToString()).AddComponent<Toggle>();
  32. cellText.transform.SetParent(row.transform, false);
  33. cellText.tag = "Toggle";
  34. Image Background_img = new GameObject("bg"+i.ToString()).AddComponent<Image>();
  35. Background_img.transform.SetParent(cellText.transform, false);
  36. //Background_img.sprite = Main.Global.sprite_box;
  37. Background_img.rectTransform.sizeDelta = new Vector2(100f,70f);
  38. Image Check_img = new GameObject("Check"+i.ToString()).AddComponent<Image>();
  39. Check_img.transform.SetParent(cellText.transform, false);
  40. Check_img.rectTransform.sizeDelta = new Vector2(100f,70f);
  41. //Check_img.sprite = Main.Global.sprite_check;
  42. cellText.graphic = Check_img;
  43. cellText.targetGraphic=Background_img;
  44. if(string.Equals(value, "true", StringComparison.OrdinalIgnoreCase)){
  45. cellText.isOn=true;
  46. }
  47. }else{
  48. GameObject cellText = new(table_name+"_Cells_"+i.ToString()+"_"+j.ToString());
  49. cellText.transform.SetParent(row.transform, false);
  50. double number;
  51. if (double.TryParse(value, out number) && table_name=="區間"){
  52. string formattedValue = String.Format("{0:###,##0.##}", number);
  53. cellText.AddComponent<TextMeshProUGUI>().text = formattedValue;
  54. }else{
  55. cellText.AddComponent<TextMeshProUGUI>().text = value;
  56. }
  57. //cellText.AddComponent<TextMeshProUGUI>().text = value;
  58. cellText.GetComponent<TextMeshProUGUI>().tag="Cells";
  59. if(overflowMode){cellText.GetComponent<TextMeshProUGUI>().overflowMode=TextOverflowModes.Ellipsis;}
  60. //cellText.AddComponent<Button>();
  61. //cellText.GetComponent<Button>().onClick.AddListener(() => { Main.按鈕事件2(cellText); });
  62. }
  63. }
  64. }
  65. TextMeshProUGUI[] texts = ceneter.GetComponentsInChildren<TextMeshProUGUI>();
  66. foreach (TextMeshProUGUI text in texts){
  67. if(text.tag.Contains("Header")){
  68. text.font = myFont;
  69. text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
  70. text.alignment = TextAlignmentOptions.Center;
  71. text.fontStyle = FontStyles.Bold;
  72. text.fontSize=22;
  73. if(!text.name.Contains("年度")){
  74. text.enableAutoSizing = true;
  75. text.autoSizeTextContainer = true;
  76. }
  77. text.margin = new Vector4(10, 0, 10, 0);
  78. }else if(text.tag.Contains("Cells")){
  79. text.font = myFont;
  80. text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
  81. text.alignment = TextAlignmentOptions.Center;
  82. //text.fontStyle = FontStyles.Bold;
  83. text.fontSize=18;
  84. if(!text.name.Contains("年度")){
  85. text.enableAutoSizing = true;
  86. text.autoSizeTextContainer = true;
  87. }
  88. text.margin = new Vector4(10, 0, 10, 0);
  89. }else{
  90. }
  91. }
  92. ceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(ceneter.GetComponent<RectTransform>().sizeDelta.x,(table.Rows.Count+1)*70f);
  93. //========建立分隔線=============
  94. for(int i=0;i<=table.Columns.Count-1;i++){
  95. GameObject Separation = Instantiate(LinePrefab, tableParent);
  96. Separation.GetComponent<RectTransform>().sizeDelta = new Vector2(3f,ceneter.GetComponent<RectTransform>().sizeDelta.y-5f);
  97. //Separation.GetComponent<RectTransform>().anchoredPosition = new Vector2(width* (i + 1)-5f,0);
  98. Separation.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
  99. }
  100. }
  101. public static void 清除控件(GameObject content){
  102. List<GameObject> childList = new();
  103. foreach (Transform child in content.transform) {
  104. childList.Add(child.gameObject);
  105. }
  106. foreach (GameObject child in childList) {
  107. if (child.name.Contains("(Clone)") ) {
  108. Destroy(child);
  109. }
  110. }
  111. }
  112. public static void 清除底色(GameObject content){
  113. List<GameObject> childList = new();
  114. foreach (Transform child in content.transform) {
  115. childList.Add(child.gameObject);
  116. }
  117. foreach (GameObject child in childList) {
  118. if (child.name.Contains("(Clone)") && child.name.Contains("Header")){
  119. child.GetComponent<Image>().color=new Color(0f,0f,0f,0f);
  120. }
  121. }
  122. }
  123. public void 截圖(){
  124. string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
  125. ScreenCapture.CaptureScreenshot(fileName);
  126. }
  127. }