简体   繁体   English

Unity 3D 中的 GameObject.Find()

[英]GameObject.Find() in Unity 3D

This is a C# method I found in a Unity3D tutorial:这是我在 Unity3D 教程中找到的 C# 方法:

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...我不明白的是在这一行: x => x.type == pt...

Where does the "x" come from and why x.type? “x”从何而来,为什么是 x.type?

This is List<T>.Find and has nothing to do with GameObject.Find !这是List<T>.FindGameObject.Find无关!


List<T>.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> .搜索与指定谓词定义的条件匹配的元素,并返回整个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.如果传递给它的 object 与委托中定义的条件匹配,则Predicate<T>是一个方法的委托[(或在您的情况下为 lambda 表达式)],该方法返回true 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.当前List<T>的元素单独传递给Predicate<T>委托,在List<T>中向前移动,从第一个元素开始,到最后一个元素结束。 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然后你有一个Lambda 表达式,其中x是迭代器变量,如

foreach(var x in pieces)

that is where the x comes from.这就是x的来源。 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 .它的类型是Piece所以它取决于你的Piece实现x.type是什么......看看你的参数我会说它是一个名为PieceTypeenum


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> .如果您查看Find定义public T Find (Predicate<T> match) ,您将看到它接收到Predicate<T> ,它只不过是带有参数T和返回值boolFunc<T, bool>的 function . This effectively means that the sequence of the elements will be filtered based on the provided function.这实际上意味着将根据提供的 function 过滤元素的序列。

One possible way to specify the Func<T, bool> is using C# language construct called lambda expression.指定Func<T, bool>的一种可能方法是使用称为 lambda 表达式的 C# 语言结构。 That being said x => x.type == pt... is an lambda expression which defines the conditions of the element to search for.也就是说x => x.type == pt...是一个 lambda 表达式,它定义了要搜索的元素的条件。

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 .目的是根据typevisualIndex过滤Pieces ,其中xPiece Don't be confused with x , you could use any literal.不要与x混淆,您可以使用任何文字。 You could read it like: Give me each Piece x where x.type is pt and x.visualIndex is visualIndex你可以这样读:给我每个 Piece x 其中 x.type 是 pt 并且 x.visualIndex 是 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 x是在Pieces列表中找到的Piece object ,而x.type是在 Piece class 中定义的字段,这反过来又让您拥有一个与PieceType相同的p 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.find方法中,您声明了 lambda function(更准确的谓词), x是此 function 的变量。 According to this instruction, x is an instance of the Piece class and it has the attributes type and visualIndex .根据此指令, xPiece class 的一个实例,它具有属性typevisualIndex

This line means: "Find the first element on the pieces list where type is set to pt and visualIndex is set to visualIndex ".这一行的意思是:“在pieces列表中找到type设置为ptvisualIndex设置为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)" “在集合“Pieces”中找到它的第一个元素,它的类型为 pt,并且其 visualIndex 属性等于 visualIndex)”

"singlePiece" or "x" denotes what conditions must be met for element to be "found" using Find method. “singlePiece”或“x”表示必须满足哪些条件才能使用 Find 方法“找到”元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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