[英]How can i get all the Mesh Colliders Components i added to all models and objects in Hierarchy?
To add them all i marked and selected in the Hierarchy all the models and objects and then in the meny i did Component > Physics > Mesh Collider为了添加它们,我在层次结构中标记并选择了所有模型和对象,然后在菜单中我做了组件>物理>网格碰撞器
But now i want to delete/remove all the Mesh Colliders i added.但现在我想删除/删除我添加的所有网格碰撞器。 But i want to do it with a script.
但我想用脚本来做。
What i want is to list in array or List all the Mesh Colliders and then to decide what to do with each one of them if to delete rename or anything else.我想要的是在数组中列出或列出所有网格碰撞器,然后决定如何处理它们中的每一个,如果删除重命名或其他任何东西。
GameObject.FindObjectsOfType(typeof(MonoBehaviour)); //returns Object[]
GameObject.FindGameObjectsWithTag("Untagged"); //returns GameObject[]
This will return all the gameobjects but it's not what i need.这将返回所有游戏对象,但这不是我需要的。
With FindObjectsOfType
:使用
FindObjectsOfType
:
To find with MeshColliders with FindObjectsOfType
, provide MeshCollider
as the type.要使用 FindObjectsOfType 使用
FindObjectsOfType
,请提供MeshCollider
作为类型。 Convert it into MeshCollider
array.将其转换为
MeshCollider
数组。
MeshCollider[] allMeshes = GameObject.FindObjectsOfType(typeof(MeshCollider)) as MeshCollider[];
for (int i = 0; i < allMeshes.Length; i++)
{
allMeshes[i].enabled = false; //Optional
Destroy(allMeshes[i]);
}
With FindGameObjectsWithTag
:使用
FindGameObjectsWithTag
:
Find the GameObject that contains the Colliders with their tags.找到包含碰撞器及其标签的游戏对象。 Store to GameObject array then use
getComponent
to get the MeshCollider
from them.存储到 GameObject 数组,然后使用
getComponent
从中获取MeshCollider
。
GameObject[] tempObj = GameObject.FindGameObjectsWithTag("Untagged") as GameObject[];
MeshCollider[] allMeshes = new MeshCollider[tempObj.Length];
for (int i = 0; i < allMeshes.Length; i++)
{
allMeshes[i] = tempObj[i].GetComponent<MeshCollider>();
if (allMeshes[i] != null)
{
allMeshes[i].enabled = false; //Optional
Destroy(allMeshes[i]);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.