123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- using System;
- using System.Data;
- using System.Collections.Generic;
- using TMPro;
- using UnityEngine;
- using UnityEngine.UI;
-
-
-
- public class Module : MonoBehaviour
- {
-
- 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<Image>();
- if(centerImage == null){
- centerImage = ceneter.AddComponent<Image>();
- 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);
- //==========動態生成表格==================================
- for (int i = 0; i < table.Rows.Count; i++){
- // 建立資料列
- GameObject row = Instantiate(rowPrefab, tableParent);
- // 設定資料列位置
- row.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, -90 * i);
- // 建立資料列中的所有欄位
- 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<Toggle>();
- cellText.transform.SetParent(row.transform, false);
- cellText.tag = "Toggle";
- Image Background_img = new GameObject("bg"+i.ToString()).AddComponent<Image>();
- 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<Image>();
- 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(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<TextMeshProUGUI>().text = formattedValue;
-
- }else{
- cellText.AddComponent<TextMeshProUGUI>().text = value;
- }
- //cellText.AddComponent<TextMeshProUGUI>().text = value;
- cellText.GetComponent<TextMeshProUGUI>().tag="Cells";
- if(overflowMode){cellText.GetComponent<TextMeshProUGUI>().overflowMode=TextOverflowModes.Ellipsis;}
- //cellText.AddComponent<Button>();
- //cellText.GetComponent<Button>().onClick.AddListener(() => { Main.按鈕事件2(cellText); });
- }
- }
- }
- TextMeshProUGUI[] texts = ceneter.GetComponentsInChildren<TextMeshProUGUI>();
- foreach (TextMeshProUGUI text in texts){
- if(text.tag.Contains("Header")){
- text.font = myFont;
- text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
- text.alignment = TextAlignmentOptions.Center;
- text.fontStyle = FontStyles.Bold;
- text.fontSize=22;
- if(!text.name.Contains("年度")){
- text.enableAutoSizing = true;
- text.autoSizeTextContainer = true;
- }
- text.margin = new Vector4(10, 0, 10, 0);
- }else if(text.tag.Contains("Cells")){
- text.font = myFont;
- text.color = Main.Global.系統主題 == 1 ? new Color(0, 0, 0, 1) : text.color;
- text.alignment = TextAlignmentOptions.Center;
- //text.fontStyle = FontStyles.Bold;
- text.fontSize=18;
- if(!text.name.Contains("年度")){
- text.enableAutoSizing = true;
- text.autoSizeTextContainer = true;
- }
- text.margin = new Vector4(10, 0, 10, 0);
- }else{
-
- }
- }
- ceneter.GetComponent<RectTransform>().sizeDelta = new Vector2(ceneter.GetComponent<RectTransform>().sizeDelta.x,(table.Rows.Count+1)*70f);
- //========建立分隔線=============
- for(int i=0;i<=table.Columns.Count-1;i++){
- GameObject Separation = Instantiate(LinePrefab, tableParent);
- Separation.GetComponent<RectTransform>().sizeDelta = new Vector2(3f,ceneter.GetComponent<RectTransform>().sizeDelta.y-5f);
- //Separation.GetComponent<RectTransform>().anchoredPosition = new Vector2(width* (i + 1)-5f,0);
- Separation.GetComponent<Image>().color = Main.Global.系統主題 == 1 ? new Color(0f, 0f, 0f, 1f) : new Color(1f, 1f, 1f, 1f);
- }
- }
- public static void 清除控件(GameObject content){
- List<GameObject> childList = new();
- foreach (Transform child in content.transform) {
- childList.Add(child.gameObject);
- }
- foreach (GameObject child in childList) {
- if (child.name.Contains("(Clone)") ) {
- Destroy(child);
- }
- }
- }
- public static void 清除底色(GameObject content){
- List<GameObject> childList = new();
- foreach (Transform child in content.transform) {
- childList.Add(child.gameObject);
- }
- foreach (GameObject child in childList) {
- if (child.name.Contains("(Clone)") && child.name.Contains("Header")){
- child.GetComponent<Image>().color=new Color(0f,0f,0f,0f);
- }
- }
- }
- public void 截圖(){
- string fileName = string.Format("{0}/screenshot_{1}.png", Application.persistentDataPath, System.DateTime.Now.ToString("yyyyMMdd_HHmmss"));
- ScreenCapture.CaptureScreenshot(fileName);
- }
- }
|