如何比较两个不同类型的项目

我不确定这样做的最佳方法是什么,但我想要两个enums我可以比较的方法。所以当一个单位进入当前单位的触发器时,该单位可以通过比较两者来检测进入的单位是否可攻击enums。


我知道enums不能像下面的例子那样比较两个不同的东西,那么当我不None接受什么unit type但None可以接受时,最好的方法是什么attack type?


public enum UnitType { Ground, Air, Water }

public enum AttackType { None, Ground, Air, Water }


public class Unit : MonoBehaviour {

    public UnitType unitType;

    public AttackType attackType;


    void OnTriggerEnter(Collider other) {

        Unit unit = other.GetComponent<Unit>();

        if(unit.unitType == attackType) {

            // Send current unit to attack here

        }

    }

}


烙印99
浏览 260回答 3
3回答

牧羊人nacy

虽然枚举是不同的类型,并且可能具有不同的整数值,但您仍然可以通过它们的符号名称来比较它们,使用ToString().&nbsp;所以代替if&nbsp;(unit.unitType&nbsp;==&nbsp;attackType)只是使用if&nbsp;(unit.unitType.ToString()&nbsp;==&nbsp;attackType.ToString())

蛊毒传说

这从根本上是行不通的。您有两个枚举,它们由整数支持。第一个以 开头,Ground第二个以 开头None,这意味着UnitType.Ground == AttackType.None,哎呀!您应该做的是使用单个枚举:public enum UnitType { None, Ground, Air, Water }和字段:public UnitType unitType;public UnitType attackType;None对 没有意义unitType,但没关系!重要的是这两个领域有一个共同的关系,这个关系就是他们是什么类型的单位,他们可以攻击什么类型的单位。我们可以更进一步:[Flags]public enum UnitType {&nbsp; &nbsp; None =&nbsp; &nbsp;0,&nbsp; &nbsp; Ground = 1, // 1<<0&nbsp; &nbsp; Air =&nbsp; &nbsp; 2, // 1<<1&nbsp; &nbsp; Water =&nbsp; 4&nbsp; // 1<<2}现在我们可以这样做:this.attackType = UnitType.Ground|UnitType.Air;//...if(unit.unitType & attackType > 0) {&nbsp; &nbsp; // Send current unit to attack here}瞧,制作可以攻击不止一种类型的东西不需要魔法!或单位认为是多种类型的!(气垫船是Ground 和 Water)

忽然笑

这是我将采用的方法:设置有效攻击的列表,然后简单地与该列表进行比较。试试这个:var validAttacks = new (UnitType, AttackType)[]{&nbsp; &nbsp; (UnitType.Air, AttackType.Air),&nbsp; &nbsp; (UnitType.Air, AttackType.Ground),&nbsp; &nbsp; (UnitType.Ground, AttackType.Ground),&nbsp; &nbsp; (UnitType.Water, AttackType.Water),&nbsp; &nbsp; (UnitType.Water, AttackType.Air),};使用这种列表,您可以创建您喜欢的任何组合。您甚至可以在运行时设置它以使其灵活。现在,要使用它,只需执行以下操作:var unit = UnitType.Water;var attack = AttackType.Air;var attackable = validAttacks.Contains((unit, attack));Console.WriteLine(attackable);这会产生True因为该组合的UnitType.Water和AttackType.Air是在列表中。现在,你可以更进一步,设置这种事情:public class Unit{&nbsp; &nbsp; private Dictionary<(UnitType, AttackType), Action<Unit, Unit>> _validAttacks;&nbsp; &nbsp; public Unit()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; _validAttacks = new Dictionary<(UnitType, AttackType), Action<Unit, Unit>>()&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { (UnitType.Air, AttackType.Air), (s, o) => MissleAttack(s, o) },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { (UnitType.Air, AttackType.Ground), (s, o) => MissleAttack(s, o) },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { (UnitType.Ground, AttackType.Ground), (s, o) => TankAttack(s, o) },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { (UnitType.Water, AttackType.Water), (s, o) => TorpedoAttack(s, o) },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { (UnitType.Water, AttackType.Air), (s, o) => IcbmAttack(s, o) },&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; public UnitType unitType;&nbsp; &nbsp; public AttackType attackType;&nbsp; &nbsp; void OnTriggerEnter(Collider other)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Unit unit = other.GetComponent<Unit>();&nbsp; &nbsp; &nbsp; &nbsp; if (_validAttacks.ContainsKey((unit.unitType, attackType)))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _validAttacks[(unit.unitType, attackType)].Invoke(this, unit);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void TankAttack(Unit self, Unit other) { ... }&nbsp; &nbsp; public void MissleAttack(Unit self, Unit other) { ... }&nbsp; &nbsp; public void TorpedoAttack(Unit self, Unit other) { ... }&nbsp; &nbsp; public void IcbmAttack(Unit self, Unit other) { ... }}现在我可以合并一个与(UnitType, AttackType). 代码变得非常简洁明了。
打开App,查看更多内容
随时随地看视频慕课网APP