[英]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>.Find
与GameObject.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 returnstrue
if the object passed to it matches the conditions defined in the delegate.如果传递给它的 object 与委托中定义的条件匹配,则
Predicate<T>
是一个方法的委托[(或在您的情况下为 lambda 表达式)],该方法返回true
。 The elements of the currentList<T>
are individually passed to thePredicate<T>
delegate, moving forward in theList<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
是什么......看看你的参数我会说它是一个名为PieceType
的enum
。
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
和返回值bool
、 Func<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
.目的是根据
type
和visualIndex
过滤Pieces
,其中x
是Piece
。 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
.根据此指令,
x
是Piece
class 的一个实例,它具有属性type
和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
列表中找到type
设置为pt
且visualIndex
设置为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.