C# 在unity3d中通过标签查找不活动的游戏对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16168596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:13:44  来源:igfitidea点击:

find inactive gameobject by tag in unity3d

c#unity3d

提问by kUr4m4

I have a gameobject which I wish to activate given a certain condition. I gave it a unique tag and I tried using GameObject.FindObjectWithTag("Tag name").From what I can tell, this method will only find active gameobjects in the scene and not inactive ones.

我有一个游戏对象,我希望在特定条件下激活它。我给了它一个独特的标签,我尝试使用据GameObject.FindObjectWithTag("Tag name").我所知,这种方法只会在场景中找到活动的游戏对象,而不是不活动的游戏对象。

Is there a method that I can call that will also search inactive gameobjects? (Preferably searching by tag).

有没有我可以调用的方法也可以搜索不活动的游戏对象?(最好按标签搜索)。

Thanks!

谢谢!

采纳答案by kUr4m4

After some research it seems that there is no way to find an inactive gameobject by tag.

经过一些研究,似乎无法通过标签找到不活动的游戏对象。

solutions exist however to access inactive gameobjects:

然而,存在访问非活动游戏对象的解决方案:

1 - Store inactive game objects in an array if you need to reactivate them afterwards (only applies to game objects inactivated at runtime).

1 - 将不活动的游戏对象存储在数组中,如果您之后需要重新激活它们(仅适用于在运行时停用的游戏对象)。

2 - Do not deactivate game object, simply deactivate the components you want inactive. If you wish to make the object disappear, deactivate the renderer. If it is a specific script, deactivate that script, etc.

2 - 不要停用游戏对象,只需停用您想要停用的组件。如果您希望使对象消失,请停用渲染器。如果是特定脚本,请停用该脚本等。

This solution will allow you to still find a game object by its tag name.

此解决方案将允许您仍然通过标签名称找到游戏对象。

回答by sage

FindObjectsOfTypeAll does find inactive, it just may also find prefabs and things you are not looking for, so you have to be careful.

FindObjectsOfTypeAll 确实会发现处于非活动状态,它也可能会发现预制件和您不想要的东西,因此您必须小心。

回答by Vineet

Things that can find inactive gameObjects:

可以找到不活动的东西gameObjects

transform.Find() or transform.FindChild()
transform.GetComponentsInChildren<Component>(true)

Resources.FindObjectsOfTypeAll<Component>()