简体   繁体   中英

GameObject.Find() in Unity 3D

This is a C# method I found in a Unity3D tutorial:

Public List<Piece> pieces = new List<piece>(); // list of pieces in the pool

public Piece GetPiece(PieceType pt, int visualIndex) {
    Piece p = pieces.Find(x => x.type == pt && x.visualIndex == visualIndex);
} 

What I don't understand is in this line: x => x.type == pt...

Where does the "x" come from and why x.type?

This is List<T>.Find and has nothing to do with GameObject.Find !


List<T>.Find :

Searches for an element that matches the conditions defined by the specified predicate, and returns the first occurrence within the entire List<T> .

The Predicate<T> is a delegate [(or in your case lambda expression)] to a method that returns true if the object passed to it matches the conditions defined in the delegate. The elements of the current List<T> are individually passed to the Predicate<T> delegate, moving forward in the List<T> , starting with the first element and ending with the last element. Processing is stopped when a match is found.


Then what you have there is a Lambda Expression where x is the iterator variable like in

foreach(var x in pieces)

that is where the x comes from. It can basically called whatever you like. And its type is Piece so it depends on your Piece implementation what x.type is.. looking on your parameters I'ld say it is an enum called PieceType .


So it does basically the same as and is just a shorthand for

public Piece GetPiece(PieceType pt, int visualIndex) 
{
    foreach(var x in pieces)
    {
        if(x.type == pt && x.visualIndex == visualIndex) return x;
    }

    return default(Piece);
} 

If you look at the Find definition, public T Find (Predicate<T> match) , you will see that it receives the Predicate<T> which is nothing than a function with parametar T and return value bool , Func<T, bool> . This effectively means that the sequence of the elements will be filtered based on the provided function.

One possible way to specify the Func<T, bool> is using C# language construct called lambda expression. That being said x => x.type == pt... is an lambda expression which defines the conditions of the element to search for.

Looking at the:

Piece p = Pieces.Find(x => x.type == pt && x.visualIndex == visualIndex)

The intention is to filter Pieces based on type and visualIndex where x is Piece . Don't be confused with x , you could use any literal. You could read it like: Give me each Piece x where x.type is pt and x.visualIndex is visualIndex

x is the Piece object that was found in the Pieces list and x.type is something that was a field defined inside the Piece class which in turn lets you have a p object with the same PieceType as pt

In the find method you are declaring a lambda function (a predicate to be more precise) and x is the variable of this function. According to this instruction, x is an instance of the Piece class and it has the attributes type and visualIndex .

This line means: "Find the first element on the pieces list where type is set to pt and visualIndex is set to visualIndex ".

Pieces.Find(x => x.type == pt && x.visualIndex == visualIndex)

Could be written as

Pieces.Find(singlePiece => singlePiece.type == pt && singlePiece.visualIndex == visualIndex)

Which translates to:

"Within collection "Pieces" find first element that it of type pt and whose visualIndex property is equal to visualIndex)"

"singlePiece" or "x" denotes what conditions must be met for element to be "found" using Find method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM