2016-08-01 225 views
3

我正在Unity 5.3上开发C#脚本。我有一个Vector2值列表,我需要提取列表中最大的X值。我试图做到以下几点:Unity 5.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值的任何其他方式?

+0

你也许可以尝试这样的: INT XMAX = Single.MinValue; (Vector.X> xMax) { xMax = vector.X; } } – Roman

+0

谢谢,我马上试试@RomanSidorov – xrr

回答

2

你正试图把不能将该类型的变量作为参数的函数列表。

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 
} 
+0

这很好,我不必定义任何值。谢谢! – xrr

0

你试图用你的代码获得最大的Vector本身,而不是你想获得矢量的最大'x'值 - 这是一个浮点数而不是矢量;试试这个代码:

public List<Vector2> Series1Data; 
//I populate the List with some coordinates 
MaXValue = Mathf.Max(Series1Data[0].x); 

编辑:

我刚刚意识到Mathf.Max()函数花车作为参数数组 - 你需要单独的“X”值从浮矢量;或许下面可能工作:

public List<Vector2> Series1Data; 
//I populate the List with some coordinates 
float[] xCoordinates = new float[Series1Data.Count] 
for(int i = 0; i < Series1Data.Count; i++) 
{ 
    xCoordinates[i] = Series1Data[i].x; 
} 
MaXValue = Mathf.Max(xCoordinates); 
2

您可以通过LINQ的做到这一点:

MaxXValue = Series1Data.Max(v => v.x); 

这是假设你Series1Data列表对象不是null或空。

2

你也许可以尝试这样的:

float xMax = Single.MinValue; 
foreach (Vector2 vector in Series1Data) 
{ 
    if (vector.X > xMax) 
    { 
    xMax = vector.X; 
    } 
} 
0

你的问题在于,你是传递一个Vector2类型的List<>Mathf.Max()采用float类型的array的事实中。而不是你的代码,做这样的事情:

public Vector2[] Series1Data; 
public float[] Series1DataX; 
... //Populate Series1Data with data like this: Series1Data[number] = data; 
... //Populate Series1DataX with data like this: Series1DataX[number] = data.x; 
MaXValue = Mathf.Max(Series1DataX); 

所以,你有两个数组:存储所有Vector2 S和一个存储Vector2 s'的X值的数组。

0

使用方法如下。

MaXValue = Mathf.Max(Series1Data[0].x);

导致你错误的,Series1Data []是vector2和MaxValue的是浮动。这是一种类型转换错误。并且您希望从Series1Data []获得x值。

为了从列表中获取最大值,您必须遍历所有x值,并检查较大的值和下一个值,如果值较大,则替换当前的最大值。

或者您可以sort列表升序并获取第一个索引值作为最大值。

0

统一的Mathf.Max只适用于浮标阵, 可以使所有的临时数组浮来解决它

using UnityEngine; 
using System.Collections.Generic; 

public class LelExample : MonoBehaviour 
{ 
    public List<Vector2> Vec2List = new List<Vector2>(); 
    public float maxX; 

    public void Start() 
    { 
     float[] x; //set temp arrays 
     float[] y; 

     GetArraysFromVecList(Vec2List, out x, out y); //set the arrays outputting x and y 

     maxX = Mathf.Max(x); //Max the x's 
    } 
    public void GetArraysFromVecList(List<Vector2> list, out float[] x, out float[] y) //My Custom Void 
    { 
     float[] tx = new float[list.Count]; //Define temporary arrays 
     float[] ty = new float[list.Count]; 

     for (int i = 0; i < list.Count; ++i) 
     { 
      tx[i] = list[i].x; //Add x to each corrosponding tx 
      ty[i] = list[i].y; //Add y to each corrosponding ty 
     } 
     x = tx; //set the arrays 
     y = ty; 
    } 
} 

希望这个作品:)〜希德

相关问题