2016-08-01 6 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。 foreach(Series1DataのVector2ベクトル) { if(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'値を取得したいとします。これはベクトルではなくfloatです。このコードを試してみてください。

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

EDIT:

私はちょうどMathf.Max()関数は、パラメータとしてfloatの配列を取ることに気づいた - あなたはから「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 Listオブジェクトがnullまたは空ではない、あなたを想定しています。

2

あなたは、おそらくこのように試みることができる:

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

あなたの問題はMathf.Max()は、float型のarrayを取るとき、あなたがタイプVector2List<>を渡しているという事実の範囲内にあります。

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の値を格納するものを代わりにあなたのコードで、このような何かを行います。

0

以下のように使用します。あなたのエラーの

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

原因、Series1Data []はベクトル2で、MAXVALUEがフロートです。これは型変換エラーです。そして、Series1Data []のx値が必要です。

リストから最大値を取得するには、すべてのx値を反復処理し、大きいものを次のものでチェックし、現在のmax値が大きい場合はその値に置き換えます。

また、sortリストを昇順にして、最初のインデックス値をmaxとして取得することもできます。

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

にこの作品:)〜シド

関連する問題