虽然这确实涉及Unity,但它实际上更多是一个通用编程问题。有没有办法区分列表中的项目?
所以我不能使用元组,我需要返回多个浮点值。这些有点像伤害增益。所以我会让射手对骑兵造成额外的伤害,这个伤害是x。我的代码(繁琐尽管它是)能更好地告诉它:
private List<float> DetermineCounter()
{
CounterData cd = new CounterData();
List<float> counterList = new List<float>();
if (gameObject.tag == "Hoplite")
{
float archer = cd.counterArcher = 10.0f;
float peltast = cd.counterPeltast =5.0f;
float slinger = cd.counterSlinger = 20.0f;
List<float> counterData = new List<float>();
counterData.Add(archer);
counterData.Add(peltast);
counterData.Add(slinger);
counterData = counterList;
}
if (gameObject.tag == "Peltast")
{
float archer = cd.counterArcher = 5.0f;
float slinger = cd.counterSlinger = 5.0f;
List<float> counterData = new List<float>();
counterData.Add(archer);
counterData.Add(slinger);
counterData = counterList;
}
if (gameObject.tag == "Archer")
{
float cavalry = cd.counterCavalry = 15.0f;
float slinger = cd.counterSlinger = 10.0f;
List<float> counterData = new List<float>();
counterData.Add(cavalry);
counterData.Add(slinger);
counterData = counterList;
}
if (gameObject.tag == "Cavalry")
{
float hoplite = cd.counterHoplite = 20.0f;
float peltast = cd.counterPeltast = 10.0f;
List<float> counterData = new List<float>();
counterData.Add(hoplite);
counterData.Add(peltast);
counterData = counterList;
}
return counterList;
}
所以它返回2倍或3的值,取决于哪个对象我对剧本上。所以,如果是射手,我已经有脚本了,那么它会返回值15和10.我的问题是,这些不只是任何值,它们是指具体的东西。 15属于对骑兵造成的附加伤害因素,这些信息很重要。
因此,当我使用foreach作为列表时,无法确定每个值对应的内容。我想知道是否有办法弥补这一点?
一个解决方案是使一个新的类,将保留可能的返回值并返回该类的对象而不是列表。 –
为什么不返回一个类型为Damage的对象或类似的东西? – NicoRiff
出于闲散的好奇心,为什么你不能使用元组? – Abion47