Unity 两种Find()

官方:

Transform.Find

public Transform Find(string n);

Parameters

n Name of child to be found.

Returns

Transform The returned child transform or null if no child is found.

Description

Finds a child by n and returns it.

If no child with n can be found, null is returned. If n contains a '/' character it will access the Transform in the hierarchy like a path name.

Note: Find does not perform a recursive descend down a Transform hierarchy.

GameObject.Find

public static GameObject Find(string name);

Description

Finds a GameObject by name and returns it.

This function only returns active GameObjects. If no GameObject with name can be found, null is returned. If name contains a '/' character, it traverses the hierarchy like a path name.

For performance reasons, it is recommended to not use this function every frame. Instead, cache the result in a member variable at startup. or use GameObject.FindWithTag.

Note: If you wish to find a child GameObject, it is often easier to use Transform.Find.

Note: If the game is running with multiple scenes then Find will search in all of them.

通俗一点的理解:

Transform.Find是查找当前对象的子物体,即使子物体的active = false,也能查找到(当然当前对象必须active = true),效率更高。

GameObject.Find是查找整个视图中相对应的资源,如果需要查找的物体的active = false 或者要查找的物体的父物体任意一个active = false都不能查找到。另外,该方法不能放在Update里执行,因为太耗费性能。

Unity获取游戏对象详解 From 雨凇momo

http://www.xuanyusong.com/archives/2768

猜你喜欢

转载自blog.csdn.net/iov3Rain/article/details/81349542