Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unity 5.3-C#--List<;矢量2>;如何提取最大X值?_C#_Unity3d - Fatal编程技术网

Unity 5.3-C#--List<;矢量2>;如何提取最大X值?

Unity 5.3-C#--List<;矢量2>;如何提取最大X值?,c#,unity3d,C#,Unity3d,我正在开发Unity 5.3的C#脚本。我有一个向量2值的列表,我需要提取列表中最大的X值。我正在努力做到以下几点: public List<Vector2> Series1Data; ... //I populate the List with some coordinates MaXValue = Mathf.Max(Series1Data[0]); 有没有其他方法可以提取列表中的最大X值?您正试图用代码获取最大向量本身,而不是想要获取向量的最大“X”值-这是一个浮点值,而不是

我正在开发Unity 5.3的C#脚本。我有一个向量2值的列表,我需要提取列表中最大的X值。我正在努力做到以下几点:

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

有没有其他方法可以提取列表中的最大X值?

您正试图用代码获取最大向量本身,而不是想要获取向量的最大“X”值-这是一个浮点值,而不是向量;请尝试以下代码:

public List<Vector2> Series1Data;
//I populate the List with some coordinates
MaXValue = Mathf.Max(Series1Data[0].x);
公共列表系列1数据;
//我用一些坐标填充列表
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);
公共列表系列1数据;
//我用一些坐标填充列表
float[]xCoordinates=新的float[Series1Data.Count]
for(int i=0;i
您可以通过Linq执行此操作:

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

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

您可能可以这样尝试:

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

您的问题在于,当
Mathf.Max()
采用float类型的
数组时,您正在传递类型为
Vector2
列表。代替您的代码,执行以下操作:

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值

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

导致错误的原因是,Series1Data[]是vector2,MaxValue是float。这是一个类型转换错误。您需要Series1Data[]中的x值

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


或者您可以按升序排列列表,并将第一个索引值作为max.

Unity的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;
    }
}
使用UnityEngine;
使用System.Collections.Generic;
公共类:例如:单一行为
{
public List Vec2List=新列表();
公共浮动maxX;
公开作废开始()
{
float[]x;//设置临时数组
浮动[]y;
GetArraysFromVecList(Vec2List,out x,out y);//设置输出x和y的数组
maxX=Mathf.Max(x);//最大x
}
public void GetArraysFromVecList(列表列表,外浮点数[]x,外浮点数[]y)//我的自定义void
{
float[]tx=new float[list.Count];//定义临时数组
float[]ty=新的float[list.Count];
对于(int i=0;i

希望它能起作用:)~Sid

您试图在一个函数上放置一个列表,该函数不能将该类型的变量作为参数

在这里,您可以看到它可以处理哪种类型的参数

此代码可以完成以下工作:

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数据;
... //我用一些坐标填充列表
MaXValue=Series1Data[0].x//获取第一个值
对于(inti=1;i
您可能可以这样尝试:int xMax=Single.MinValue;foreach(vector2vector-in-Series1Data){if(vector.X>xMax){xMax=vector.X;}}}谢谢,我会马上试试@romansidorovt这很好,我不需要定义任何值。非常感谢。
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
}