GameObject.FindObjectOfType<>() 与 GetComponent<>() [英] GameObject.FindObjectOfType&lt;&gt;() vs GetComponent&lt;&gt;()

查看:21
本文介绍了GameObject.FindObjectOfType<>() 与 GetComponent<>()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在关注几个教程系列,并看到这两个以非常相似的方式使用,并希望有人能解释它们的不同之处,并在可能的情况下,举例说明何时使用一个而不是另一个(假设它们其实很像!)

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 levelManager;

void Start () {
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

private LevelManager levelManager;

void Start () {
    levelManager = GetComponent<LevelManager>();
}

推荐答案

你不想用

void Start () {
    levelManager = GameObject.FindObjectOfType<LevelManager>();
}

经常这样.特别是在 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?

  1. GameObject.FindObjectOfType 更像是一个场景范围的搜索,并不是获得答案的最佳方式.实际上,Unity 公开表示其超慢 Unity3D API 参考 - FindObjectOfType

  1. 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 Unity3D API Reference - FindObjectOfType

GetComponent(); 是本地调用.这意味着进行此调用的任何文件都只会搜索它所附加到的游戏对象.所以在检查器中,文件只会在同一个检查器窗口中搜索其他内容.比如Mesh Renderer, Mesh Filter等等.或者是那个物体的子.不过,我相信有一个单独的呼吁.
此外,如果您首先引用它们,您可以使用它来访问其他 GameObject 的组件(如下所示).

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).

分辨率:

我建议在 awake 函数中进行 tag 搜索.

Resolution:

I would recommend doing a tag search in the awake function.

private LevelManager levelManager;

void Awake () {
    levelManager = GameObject.FindGameObjectWithTag ("manager").GetComponent<LevelManager>();
}

不要忘记通过添加标签来标记带有脚本 LevelManager 的游戏对象.(单击 GameObject,查看检查器顶部,然后单击 Tag->Add Tag

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 levelManager;

然后将 GameObject 拖入检查器中的框中.

And drag the GameObject into the box in the inspector.

任何一个选项都比使用 GameObject.FindObjectOfType 要好得多.

Either option is significantly better than doing a GameObject.FindObjectOfType.

希望能帮到你

这篇关于GameObject.FindObjectOfType<>() 与 GetComponent<>()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆