用gw怎么no gameobject.find find

5134人阅读
Unity(4)
unity中提供了获取对象的五种方法:
通过对象名称(Find方法)通过标签获取单个游戏对象(FindWithTag方法)通过标签获取多个游戏对象(FindGameObjectsWithTags方法)通过类型获取单个游戏对象(FindObjectOfType方法)通过类型获取多个游戏对象(FindObjectsOfType方法)
Find方法:
static GameObject&Find&(string&name)
传入的name可以是单个的对象的名字,也可以是hierarchy中的一个路径名,如果找到会返回该对象(活动的),如果找不到就返回null。
var cubeF = GameObject.Find(&/CubeFather&);
if (null != cubeF)
Debug.Log(&find cube father~&);
cubeF = GameObject.Find(&CubeFather&);
if (null != cubeF)
Debug.Log(&find cube father, no /~&);
var cubeS = GameObject.Find(&/CubeFather/CubeSon&);
if (null != cubeS)
Debug.Log(&find cube son~&);
cubeS = GameObject.Find(&CubeFather/CubeSon&);
if (null != cubeS)
Debug.Log(&find cube son, no /~&);
cubeS = GameObject.Find(&CubeSon&);
if (null != cubeS)
Debug.Log(&find cube son, no one /~&);
结果如上,可见不论参数是对象名字还是对象的路径,只要对象存在都会查找到,但是建议最好是写详细的路径名例如CubeFather/CubeSon,这样的话,在unity查找的过程中会省很多事,效率高;另外不要在每一帧都执行的函数中调用该函数,可以看上图结果中会执行好多次,用到某个对象时可以在Start这种只执行一次的函数中定义变量获取Find的返回值,再在每帧都执行的函数中使用该变量即可~
FindWithTag方法:
static GameObject&FindWithTag&(string&tag)&
返回一个用tag做标识的活动的对象,如果没有找到则为null。
var sphere = GameObject.FindWithTag(&Sphere&);
if (null != sphere)
Debug.Log(&Sphere~&);
}将hierarchy中某个对象的Inspector面板上面的Tag自定义一个,然后为其选择自定义(上述例子中用的Sphere)
,当然没有的话,利用下拉列表中的AddTag构建
FindGameObjectsWithTag方法:
GameObject[]&FindGameObjectsWithTag&(string
返回一个用tag做标识的活动的游戏物体的列表,如果没有找到则为null。具体代码略过~
FindObjectOfType方法:
static Object FindObjectOfType(Type type)
返回类型为type的活动的第一个游戏对象
FindObjectsOfType方法:
static Object FindObjectsOfType(Type type)
返回类型为type的所有的活动的游戏对象列表
注意:一定保证对象是active的才会找到
& & & & &为了效率高,一定要保证别在每帧都调用的函数中使用上述函数
&&相关文章推荐
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:33509次
排名:千里之外
原创:20篇
(5)(3)(1)(12)c# - GameObject.FindObjectOfType&&() vs GetComponent&&() - Stack Overflow
Join the Stack Overflow Community
Stack Overflow is a community of 7.1 million programmers, just like you, helping each other.
J it only takes a minute:
I have been following several tutorial series and have seen these two used in very similar ways, and was hoping someone could explain how they differ and, if possible, examples of when you would use one instead of the other (presuming that they are actually similar!).
private LevelManager levelM
void Start () {
levelManager = GameObject.FindObjectOfType&LevelManager&();
private LevelManager levelM
void Start () {
levelManager = GetComponent&LevelManager&();
You don't want to use
void Start () {
levelManager = GameObject.FindObjectOfType&LevelManager&();
that often. Particularly on start
To answer your question though, these two functions are actually not very similar. One is a exterior call, the other an interior.
So what's the difference?
The GameObject.FindObjectOfType is more of a scene wide search and isn't the optimal way of getting an answer. Actually, Unity publicly said its super slow
The GetComponent&LevelManager&(); is a local call. Meaning whatever file is making this call will only search the GameObject that it is attached to. So in the inspector, the file will only search other things in the same inspector window. Such as Mesh Renderer, Mesh Filter, Etc. Or that objects children. I believe there is a separate call for this, though.
Also, you can use this to access other GameObject's components if you reference them first (show below).
Resolution:
I would recommend doing a tag search in the awake function.
private LevelManager levelM
void Awake () {
levelManager = GameObject.FindGameObjectWithTag ("manager").GetComponent&LevelManager&();
Don't forget to tag the GameObject with the script LevelManager on it by adding a tag. (Click the GameObject, look at the top of the inspector, and click Tag->Add Tag
You can do that, or do
public LevelManager levelM
And drag the GameObject into the box in the inspector.
Either option is significantly better than doing a GameObject.FindObjectOfType.
Hope this helps
There are two differences:
1.) GetComponent&T& finds a component only if it's attached to the same GameObject. GameObject.FindObjectOfType&T& on the other hand searches whole hierarchy and returns the first object that matches!
2.) GetComponent&T& returns only an object that inherits from Component, while GameObject.FindObjectOfType&T& doesn't really care.
10.7k43157
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
rev .25989
Stack Overflow works best with JavaScript enabled

参考资料

 

随机推荐