我正在Unity制作一个基于2D网格的游戏,并且有一个Weapon
类,其中包含获取武器统计数据的属性。统计数据由武器组成的部分计算,部件的数量和类型由武器类型WepGun
,WepStaff
或WepSword
定义。每种武器类型都具有亲和力和抓地力。为了让玩家为武器配备部件,我必须得到它的实际类型(枪/工作人员/剑),以便我可以使用武器上的区域。武器作为武器(可能是问题idk)存储在保存数据中,并使用Newtonsoft.Json
json库保存到磁盘。
当我尝试分配Weapon
的字段时没问题,但是当我尝试分配实际武器类型的字段时,我得到InvalidCastException: Cannot cast from source type to destination type.
。 我很确定它不会向下倾斜,因为我创建了他们开始使用武器类型的角色。(this链接显示的示例与我的情况有些相似)
有问题的代码:
[SaveData.cs]
[JsonProperty] public static CharacterSaveData JESSIE = new CharacterSaveData(...,new StatManager.WepGun());
public class CharacterSaveData {
...
[JsonProperty] public StatManager.Weapon weapon;
internal CharacterSaveData (..., StatManager.Weapon weapon) {
this.weapon = weapon;
weapon.affinity = StatManager.Part.ART_AFFINITY;
((StatManager.WepGun)weapon).barrel = new StatManager.Part(StatManager.PartType.GUN_BARREL, StatManager.AttackType.CARDINAL_SINGLE_TARGET, 3, 0, 0.2f); //error occurs here
}
[StatManager.cs]
public abstract class Weapon {
[JsonProperty] private Part _affinity;
public Part affinity {...}
[JsonProperty] private Part _grip;
public Part grip {...}
...properties
internal Weapon (Part affinity, Part grip) {
_affinity = affinity;
_grip = grip;
}
}
public class Part {
...predefined parts (ART_AFFINITY is here)
public PartType? partType; //enum
public AttackType? attackType; //enum
public int? attackRange;
public int? damageRange;
public float? modifier;
public ModType? modifierType; //enum
public Part (PartType? partType = null, AttackType? attackType = null, int? attackRange = null, int? damageRange = null, float? modifier = null, ModType? modifierType = null) {
this.partType = partType;
this.attackType = attackType;
this.attackRange = attackRange;
this.damageRange = damageRange;
this.modifier = modifier;
this.modifierType = modifierType;
}
}
public class WepGun : Weapon {
[JsonProperty] private Part _barrel;
public Part barrel {...}
[JsonProperty] private Part _sight;
public Part sight {...}
[JsonProperty] private Part _bullet;
public Part bullet {...}
...properties
public WepGun (Part barrel = null, Part grip = null, Part sight = null, Part bullet = null, Part affinity = null) : base(affinity,grip) {
_barrel = barrel;
_sight = sight;
_bullet = bullet;
}
}
public class WepStaff : Weapon {
[JsonProperty] private Part _shaft;
public Part shaft {...}
[JsonProperty] private Part _head;
public Part head {...}
[JsonProperty] private Part _accessory;
public Part accessory {...}
...properties
public WepStaff (Part shaft = null, Part grip = null, Part head = null, Part accessory = null, Part affinity = null) : base(affinity,grip) {
_shaft = shaft;
_head = head;
_accessory = accessory;
}
}
public class WepSword : Weapon {
[JsonProperty] private Part _blade;
public Part blade {...}
[JsonProperty] private Part _guard;
public Part guard {...}
[JsonProperty] private Part _accessory;
public Part accessory {...}
...properties
public WepSword (Part blade = null, Part guard = null, Part grip = null, Part accessory = null, Part affinity = null) : base(affinity,grip) {
_blade = blade;
_guard = guard;
_accessory = accessory;
}
}
答案 0 :(得分:1)
如果这是违规行,请更改并调试它。
((StatManager.WepGun)weapon).barrel = new StatManager.Part(StatManager.PartType.GUN_BARREL, StatManager.AttackType.CARDINAL_SINGLE_TARGET, 3, 0, 0.2f);
更改为
var wepGun = weapon as StatManager.WepGun;
if(null != wepGun)
wepGun.barrel = new StatManager.Part(StatManager.PartType.GUN_BARREL, StatManager.AttackType.CARDINAL_SINGLE_TARGET, 3, 0, 0.2f);
else
{
// debug code
}
PS我认为你应该使用Interfaces而不是武器类型的类。总有一天你会想要将两种武器类型合并为一种。你不能用课程做到这一点。