using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; // TextMeshPro 支援 using UnityEngine.UI; // 引入 UI 相關類別 public class CardDatabase : MonoBehaviour { public TextMeshProUGUI textDisplay; // UI 文字物件 (需手動設定) public Image cardImageDisplay; // 顯示卡片圖片的 UI Image (需手動設定) private List cards; // 卡牌清單 public Sprite[,] cardImages; // 用來存放卡片圖片的二維陣列 (可以在 Unity 編輯器中設定) void Start() { // 初始化卡片列表 cards = new List { new GameCard(1, "火龍", "火", 2500, 2000), new GameCard(2, "水精靈", "水", 1800, 2200), new GameCard(3, "森林妖精", "草", 2000, 1500), new GameCard(4, "雷鳴獵豹", "雷", 2300, 1700) }; // 初始化卡片圖片 (假設 4 張卡片,每張卡片有 1 張圖片) cardImages = new Sprite[4, 1]; // 4 張卡片,1 張圖片 // 在 Unity 編輯器中將圖片設置到此陣列 DisplayCards(); } void DisplayCards() { if (textDisplay != null) { textDisplay.text = ""; // 清空文字 foreach (GameCard card in cards) { textDisplay.text += card.ToString() + "\n"; } } else { Debug.LogError("請將 TextMeshProUGUI 物件指派給 textDisplay!"); } } // 顯示選定的卡片圖片 public void DisplayCardImage(int cardIndex) { if (cardImageDisplay != null && cardIndex >= 0 && cardIndex < cards.Count) { // 顯示卡片圖片 cardImageDisplay.sprite = cardImages[cardIndex, 0]; // 顯示第一張圖片(如果有多張可以調整) } else { Debug.LogError("無法顯示圖片,請確保 cardImageDisplay 被設定並且卡片索引正確"); } } } // GameCard 類別,包含編號、名稱、屬性、攻擊力、防禦力 public class GameCard { public int cardID; // 卡片編號 public string name; // 卡片名稱 public string type; // 屬性 public int attack; // 攻擊力 public int defense; // 防禦力 // 建構函數,初始化卡片資料 public GameCard(int id, string cardName, string cardType, int cardAttack, int cardDefense) { cardID = id; name = cardName; type = cardType; attack = cardAttack; defense = cardDefense; } // 這個方法用來顯示卡片的資訊 public override string ToString() { return $"編號: {cardID}, 名稱: {name}, 屬性: {type}, 攻擊力: {attack}, 防禦力: {defense}"; } }