数字纸牌游戏Unity C#中的枚举问题
我一直在尝试创建两个脚本,让我可以快速轻松地为我的数字卡游戏制作各种类型的卡。虽然大多数脚本工作,我有一个问题时,改变颜色的卡的背景,根据卡的类型 下面是“卡片资产脚本”,允许我说明它是什么类型的卡片以及它是如何工作的:数字纸牌游戏Unity C#中的枚举问题,c#,unity3d,enums,C#,Unity3d,Enums,我一直在尝试创建两个脚本,让我可以快速轻松地为我的数字卡游戏制作各种类型的卡。虽然大多数脚本工作,我有一个问题时,改变颜色的卡的背景,根据卡的类型 下面是“卡片资产脚本”,允许我说明它是什么类型的卡片以及它是如何工作的: using UnityEngine; using System.Collections; using System.Collections.Generic; public enum CardType { Weapon, Armour, Spell,
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public enum CardType
{
Weapon,
Armour,
Spell,
Environment
}
public class CardAsset : ScriptableObject
{
// this object will hold the info about the most general card
[Header("General info")]
public CharacterAsset characterAsset; // if this is null, it`s a neutral
card
[TextArea(2,3)]
public string Description; // Description for spell or character
public Sprite CardImage;
public CardType Cardtype;
[Header("Weapon Info")]
public int Defence;
public int Attack;
public int Speed;
public string WeaponScriptName;
public int specialCreatureAmount;
[Header("Spell Info")]
public string SpellScriptName;
public int specialSpellAmount;
}
下面是单卡管理器的脚本,它编辑每张卡的所有视觉元素,以匹配卡资产上的视觉元素:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class OneCardManager : MonoBehaviour {
public CardAsset cardAsset;
public OneCardManager PreviewManager;
private CardType Cardtype;
[Header("Text Component References")]
public Text NameText;
public Text DescriptionText;
public Text ArmourText;
public Text AttackText;
public Text SpeedText;
[Header("GameObject References")]
public GameObject DefenceIcon;
public GameObject AttackIcon;
public GameObject SpeedIcon;
[Header("Image References")]
public Image CardTopRibbonImage;
public Image CardGraphicImage;
public Image CardFaceBodyImage;
public Image CardBackBodyImage;
public Image CardBackDecoration;
public Image CardFaceFrameImage;
public Image CardFaceGlowImage;
private bool Weapon_Choosen;
private bool Armour_Choosen;
private bool Spell_Choosen;
private bool Environment_Choosen;
public void ChooseCardType()
{
CardType Type;
Type = CardType.Weapon;
}
void Awake()
{
if (cardAsset != null)
ReadCardFromAsset();
}
private bool canBePlayedNow = false;
public bool CanBePlayedNow
{
get
{
return canBePlayedNow;
}
set
{
canBePlayedNow = value;
CardFaceGlowImage.enabled = value;
}
}
CardType ChangeColour(CardType Type)
{
if (Type == CardType.Weapon)
{
Weapon_Choosen = true;
Type = CardType.Weapon;
}
else if (Type == CardType.Armour)
{
Armour_Choosen = true;
Type = CardType.Armour;
}
else if (Type == CardType.Spell)
{
Spell_Choosen = true;
Type = CardType.Spell;
}
else if (Type == CardType.Environment)
{
Environment_Choosen = true;
Type = CardType.Environment;
}
return Type;
}
public void ReadCardFromAsset()
{
if (Weapon_Choosen == true)
{
CardFaceBodyImage.color = Color.red;
CardBackBodyImage.color = Color.red;
Debug.Log("Applied Red to the card");
}
else if (Armour_Choosen == true)
{
CardFaceBodyImage.color = Color.blue;
CardBackBodyImage.color = Color.blue;
Debug.Log("Applied Blue to the card");
}
else if (Spell_Choosen == true)
{
CardFaceBodyImage.color = Color.green;
CardBackBodyImage.color = Color.green;
Debug.Log("Applied Green to the card");
}
else if (Environment_Choosen == true)
{
CardFaceBodyImage.color = Color.yellow;
CardBackBodyImage.color = Color.yellow;
Debug.Log("Applied Yellow to the card");
}
NameText.text = cardAsset.name;
DescriptionText.text = cardAsset.Description;
CardGraphicImage.sprite = cardAsset.CardImage;
if ((Cardtype == CardType.Weapon) || (Cardtype == CardType.Armour))
{
AttackText.text = cardAsset.Attack.ToString();
ArmourText.text = cardAsset.Defence.ToString();
SpeedText.text = cardAsset.Speed.ToString();
}
if (PreviewManager != null)
{
PreviewManager.cardAsset = cardAsset;
PreviewManager.ReadCardFromAsset();
}
}
}
有什么想法吗
编辑:只是澄清一下问题是什么。我想做的是,当卡资产脚本中定义了一种卡类型时,单卡管理员可以识别出该卡的类型,然后根据该类型更改其背景颜色(红色表示武器卡,蓝色表示装甲卡,绿色表示法术卡,黄色表示环境卡)
但是,即使定义了卡类型,卡的背景颜色也不会改变。我将首先建议您使用一个开关,而不是其他几个if() 除此之外,我认为您的代码的逻辑中存在一个问题。从唤醒,以防cardAsset!=调用的ReadCardFromAsset()为空。
在ReadCardFromAsset()中,您已经在评估四个标志中的哪一个处于活动状态:武器选择、盔甲选择。。。但正如我所看到的,这些标志只有在ChangeColour()中才是真的,而这是不被调用的。请尽可能将其简化为一个值(我意识到这并不完全可行),并在格式设置上多加注意。现在,你说“我遇到的问题是根据卡片的类型改变卡片背景的颜色”,但你没有解释问题是什么。与您希望发生的事情相比,现在发生了什么?不完全确定,正如john skeet提到的,不清楚实际问题是什么,但我认为可能是因为在
中更改颜色(CardType)
你使用类型
而不是类字段卡片类型
我想让这些卡片根据它们的类型改变颜色(例如,武器卡是红色的,盔甲卡是蓝色的,法术卡是绿色的,环境卡是黄色的)。然而,即使在卡片资产脚本中选择了一种类型的卡片,该卡片的背景在游戏中仍然显示为白色。顺便说一句,在ChooseCardtype()
中也是一样的。初始化局部变量类型,设置它,然后。。。无:DYou没有将问题中的代码简化为相关代码,也没有将其格式化。请让人们更容易帮助你。