Unity 5.3 - C# - List <vector2>如何提取最大的X值?

时间:2016-08-01 23:07:55

标签: c# unity3d

我正在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值?

7 个答案:

答案 0 :(得分:3)

您正在尝试将List放在不能将该类型的变量作为参数的函数上。

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
}

答案 1 :(得分:2)

您可以通过Linq执行此操作:

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

这假设您的Series1Data List对象不为null或为空。

答案 2 :(得分:2)

您可能会尝试这样:

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

答案 3 :(得分:0)

您试图通过代码获取最大的Vector本身,而不是希望得到最大的&#39; x&#39;向量的值 - 浮点数而不是向量;试试这段代码:

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

编辑:

我刚刚意识到Mathf.Max()函数将一个浮点数组作为参数 - 你需要将&#39; x&#39;值从向量浮动;也许以下可能有用:

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);

答案 4 :(得分:0)

您的问题在于,当List<>采用Vector2类型为float时,您传递的Mathf.Max()类型为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的数组和一个存储Vector2 s'X值的数组。

答案 5 :(得分:0)

使用如下。

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

错误原因,Series1Data []为vector2,MaxValue为float。这是一种类型转换错误。你想要Series1Data []的x值。

要从列表中获取最大值,您必须遍历所有x值并检查较大的x值,如果它更大则替换当前最大值。

或者您可以sort列表升序并将第一个索引值设为最大值

答案 6 :(得分:0)

Unity的Mathf.Max仅适用于float数组, 你可以创建一个包含所有浮点数的临时数组来解决它

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;
    }
}

希望这可行:)〜Sid

相关问题