我正在Unity5.3上开发一个C#脚本。我有一个Vector2值的列表,我需要提取列表中最大的X值。我正在尝试做以下事情:
public List<Vector2> Series1Data;
... //I populate the List with some coordinates
MaXValue = Mathf.Max(Series1Data[0]);但是,我得到以下错误:
error CS1502: The best overloaded method match for `UnityEngine.Mathf.Max(params float[])' has some invalid arguments
error CS1503: Argument `#1' cannot convert `UnityEngine.Vector2' expression to type `float[]'有没有其他方法可以提取列表中最大的X值?
发布于 2016-08-02 07:27:00
您正在尝试将列表放在不能将该类型的变量作为参数的函数上。
Mathf.Max在这里您可以看到它可以处理哪些类型的参数。
下面的代码可能会完成以下工作:
public List<Vector2> Series1Data;
... //I populate the List with some coordinates
MaXValue = Series1Data[0].x; //Get first value
for(int i = 1; i < Series1Data.Count; i++) { //Go throught all entries
MaXValue = Mathf.Max(Series1Data[i].x, MaXValue); //Always get the maximum value
}发布于 2016-08-02 07:21:52
您可以通过Linq完成此操作:
MaxXValue = Series1Data.Max(v => v.x);这假设您的Series1Data列表对象不为null或空。
发布于 2016-08-02 07:23:52
你可以尝试这样做:
float xMax = Single.MinValue;
foreach (Vector2 vector in Series1Data)
{
if (vector.X > xMax)
{
xMax = vector.X;
}
}https://stackoverflow.com/questions/38709411
复制相似问题