![](/img/trans.png)
[英]How can I decide what objects should be detected and what should be ignored by raycast and how?
[英]How can I add a int level variable to decide from what level to start getting the objects? And what colliders should I add to them?
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
public class GetComponents : MonoBehaviour
{
public Transform parent;
public List<GameObject> allObjects = new List<GameObject>();
private void Start()
{
string path = "e:/colliders.txt";
StreamWriter writer = new StreamWriter(path, true);
allObjects = FindObjectsOfType<GameObject>().ToList();
Transform[] allChildren = parent.GetComponentsInChildren<Transform>();
foreach (Transform child in allChildren)
{
var colliders = child.GetComponents<Collider>();
int length = colliders.Length;
if (length == 0)
{
writer.WriteLine(string.Format("{0} - No Colliders", child.name));
}
else
{
//composes a list of the colliders types, this will print what you want e.g. "Wall1 - BoxCollider, MeshCollider"
string colliderTypes = string.Empty;
for (int i = 0; i < length; i++)
{
colliderTypes = string.Format("{0}{1}", colliderTypes, colliders[i].GetType().Name);
if (i != (length - 1))
{
colliderTypes = string.Format("{0}, ", colliderTypes);
}
}
//writer.WriteLine(string.Format("{0} - {1}", child.name, colliderTypes));
}
}
writer.Close();
}
}
在這種情況下,我只得到沒有碰撞器的對象。 然后我想為每個 object 添加一個對撞機。 但是由於有太多對象,我想為每個對象添加一個碰撞器,以防止玩家角色穿過它們。 我不確定是否只是向它們添加網格對撞機或其他一些對撞機?
我如何設置一個級別來開始獲取對象?
例如我有這個結構:
Parent
Child 1
Child 2
Child 3
Child 4
現在級別 0 是父級別 1 是子 1 到 4。但是在每個子 1、2、3、4 中,在許多級別(樹)中還有更多的子。
例如,我想從級別 1 中獲取子對象的變換,因此它不會將父對象和子對象 1、2、3、4 作為沒有碰撞器的對象包括在內。
有兩種方法可以實現這一點 1.檢查孩子的父母是否是你想要的。
foreach (Transform child in allChildren){
if(child.parent == parent){
//Add colliders}
2.不使用GetComponentsInChildern
foreach(Transform t in parent){
// add Colliders
}
參考:
https://answers.unity.com/questions/495079/how-to-get-first-depth-child-components-of-a-paren.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.