GameObject.FindObjectOfType<>()与GetComponent<>(() [英] GameObject.FindObjectOfType<>() vs GetComponent<>()

查看:314
本文介绍了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<LevelManager>();是本地电话.这意味着无论进行此调用的文件是什么,都只会搜索其所附的GameObject.因此,在检查器中,该文件将仅在同一检查器窗口中搜索其他内容.例如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上进行标签. (点击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&lt;&gt;()与GetComponent&lt;&gt;(()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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