简体   繁体   English

Unity — 如何通过脚本将游戏对象设置为可用对象?

[英]Unity — How to set a GameObject to an Available Object through Script?

I'm a beginner in Unity development.我是 Unity 开发的初学者。 I'm trying to set a GameObject using scripting, rather then via the inspector.我正在尝试使用脚本设置游戏对象,而不是通过检查器。 Here's what I mean.这就是我的意思。 Instead of setting the game object like this, by making Propeller public and setting it manually:不是像这样设置游戏对象,而是通过公开Propeller并手动设置它:

在此处输入图片说明

I want to set Propeller directly from my script.我想直接从我的脚本中设置Propeller How can I do this?我怎样才能做到这一点?

If the Game Object is a prefab...如果游戏对象是预制...

...You can use Resources.Load("prefab path") . ...您可以使用Resources.Load("prefab path") For this to work, you must create a Resources directory inside your Assets and put the prefab in there.为此,您必须在Assets创建一个Resources目录并将预制件放在那里。

If it's a Game Object in the scene, you have several options.如果它是场景中的游戏对象,您有多种选择。

1) GameObject.Find() 1) GameObject.Find()

You can just enter the path in the hierarchy and get the object that way.您只需在层次结构中输入路径并以这种方式获取对象。

However, not only is this slow, but you're essentially hard-coding the path.但是,这不仅很慢,而且您实际上是在对路径进行硬编码。 The moment that, or the object's name, changes, you're gonna have to change the code.当对象的名称或对象的名称发生更改时,您将不得不更改代码。

2) Transform.Find() 2) Transform.Find()

Unlike GameObject.Find() , this is not a static function.GameObject.Find()不同,这不是静态函数。 As such, you'll call it from the searching object's Transform: transform.Find() .因此,您将从搜索对象的 Transform: transform.Find()调用它。

I don't doubt this is a slow function as well, but it should be faster than the previous alternative, as it only searches inside the object's children.我不怀疑这也是一个缓慢的函数,但它应该比以前的替代方法更快,因为它只在对象的子对象内部进行搜索。 It also suffers from the same "hard-coding" problem as GameObject.Find() .它还存在与GameObject.Find()相同的“硬编码”问题。

Keep in mind that it's not recursive;请记住,它不是递归的; it won't search inside its children's children.它不会在它的孩子的孩子里面搜索。

3) Component.GetComponent[s]InChildren() 3) Component.GetComponent[s]InChildren()

Finally, if the Game Object you're searching for has a component specific to it, you can search for it with GetComponentInChildren<TheComponent>() .最后,如果您要搜索的游戏对象具有特定于它的组件,您可以使用GetComponentInChildren<TheComponent>()搜索它。 It will return the first occurrence of the component.它将返回组件的第一次出现。

If you have several such children, you can make the "Component" part plural: GetComponentsInChildren<TheComponent>() .如果您有多个这样的孩子,您可以将“组件”部分GetComponentsInChildren<TheComponent>()复数: GetComponentsInChildren<TheComponent>() This will return an array containing every such component in the children.这将返回一个包含子元素中每个此类组件的数组。

You can then access their Game Objects by typing returnedComponent.gameObject .然后,您可以访问他们的游戏通过键入对象returnedComponent.gameObject


However, I would recommend that you simply drag the object in the Inspector or make it a child of the searching object, unless you have a good reason not to.但是,我建议您只需在 Inspector 中拖动对象或使其成为搜索对象的子对象,除非您有充分的理由不这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM