为了有效管理这些卡牌,我们设计了一个基础的卡牌类
Card
,并为其衍生出多个子类以支持不同类型的卡牌功能。
卡牌基类(Card)
首先,我们定义了一个名为
Card
的基类,它包含了所有卡牌共有的基本属性:
Id
(唯一标识符)、
CardName
(卡牌名称)和
Skill
(卡牌效果的文字解释)。
创建了
Card
类的四个子类:
Attackcards
(攻击牌)、
Defensecards
(防御牌)、
Buffcards
(Buff牌)和
Goldcards
(金币牌)。每个子类都继承自
Card
类,并添加了各自特有的属性。
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using UnityEngine;
public class Card : MonoBehaviour
{
// Start is called before the first frame update
public int Id;
public string CardName;
public string Skill;
public Card(int _Id, string _CardName, string _Skill)
{
this.Id = _Id;
this.CardName = _CardName;
this.Skill = _Skill;
}
}
public class Defensecards : Card
{
public int Defense;
public Defensecards(int _Id, string _CardName, int _Defense, string _Skill) : base(_Id, _CardName, _Skill)
{
this.Defense = _Defense;
}
}
public class Attackcards : Card
{
public int Attack;
public Attackcards(int _Id, string _CardName, int _Attack, string _Skill) : base(_Id, _CardName, _Skill)
{
this.Attack = _Attack;
}
}
public class Buffcards : Card
{
public string Buff;
public Buffcards(int _Id, string _CardName, string _Buff, string _Skill) : base(_Id, _CardName, _Skill)
{
this.Buff = _Buff;
}
}
public class Goldcards : Card
{
public int Gold;
public Goldcards(int _Id, string _CardName, int _Gold, string _Skill) : base(_Id, _CardName, _Skill)
{
this.Gold = _Gold;
}
}
为了动态地加载卡牌数据,我们设计了一个
Cardlist
类,该类负责从外部数据源(读取卡牌数据,并将这些数据转换为
Card
或其子类的实例,存储在列表中。看的视频参考使用的是CSV文件,这里以CSV文件为例,但实际应用中可能更倾向于使用JSON,因为其数据结构更为灵活。
同时是边学习边写的,代码格式顺序不一致,可以根据实际情况修改。
| # | id | CardName | Skill | num |
| Goldcards | 1 | Goldcards | 获得1枚金币 | 1 |
| Attackcards | 2 | Attackcards | 获得5点攻击 | 5 |
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cardlist : MonoBehaviour
{
public TextAsset cardData;
public List<Card> cardList = new List<Card>();
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void LoadCardData()
{
string[] dataRow = cardData.text.Split('\n');
foreach (var row in dataRow)
{
string[] rowArray = row.Split(",");
if (rowArray[0] == "#")
{
continue;
}
else if (rowArray[0] == "Buffcards")
{
int id = int.Parse(rowArray[1]);
string cardName = rowArray[2];
string skill = rowArray[3];
Buffcards buffcards = new Buffcards(id, cardName, skill, skill);
cardList.Add(buffcards);
}
else if (rowArray[0] == "Goldcards")
{
int id = int.Parse(rowArray[1]);
string cardName = rowArray[2];
string skill = rowArray[3];
int gold = int.Parse(rowArray[4]);
Goldcards goldcards = new Goldcards(id, cardName, gold, skill);
cardList.Add(goldcards);
}
else if (rowArray[0] == "Attackcards")
{
int id = int.Parse(rowArray[1]);
string cardName = rowArray[2];
string skill = rowArray[3];
int attack = int.Parse(rowArray[4]);
Attackcards attackcards = new Attackcards(id, cardName, attack, skill);
cardList.Add(attackcards);
}
else if (rowArray[0] == "Defensecards")
{
int id = int.Parse(rowArray[1]);
string cardName = rowArray[2];
string skill = rowArray[3];
int defense = int.Parse(rowArray[4]);
Defensecards defensecards = new Defensecards(id, cardName, defense, skill);
cardList.Add(defensecards);
}
}
}
}
建立Carddisplay用于显示卡牌名称、数值和技能的
Text
组件,以及一个用于显示卡牌图案的
Image
组件。
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class Carddisplay : MonoBehaviour
{
// Start is called before the first frame update
public Text NameText;
public Text NumText;
public Text SkillText;
private Player player;
public Card Card;
public Image CardFace;
public void ShowCard()
{
NameText.text = Card.CardName;
switch (Card)
{
case Buffcards buffcards:
SkillText.text = buffcards.Skill.ToString();
break;
case Goldcards goldcards:
NumText.text = goldcards.Gold.ToString();
SkillText.text = goldcards.Skill.ToString();
break;
case Attackcards attackcards:
NumText.text = attackcards.Attack.ToString();
SkillText.text = attackcards.Skill.ToString();
break;
case Defensecards defensecards:
NumText.text = defensecards.Defense.ToString();
SkillText.text = defensecards.Skill.ToString();
break;
default:
SkillText.text = string.Empty;
break;
}
}
void Start()
{
ShowCard();
}
}
在Unity编辑器中,将
CardDisplay
脚本附加到一个UI元素上。然后,你需要将UI组件(如名称文本、数值文本和技能文本)从Unity的Inspector面板中拖拽到
CardDisplay
脚本的相应属性上。
绑定结束后可以建立预制体,方便管理。


发布评论