如何检查GameObject是否包含MeshRenderer但不包含任何对撞机,然后向其添加对撞机?

时间:2019-05-10 19:38:30

标签: c# unity3d

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddColliders : MonoBehaviour
{
    public List<GameObject> objectsToAddCollider = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        AddDescendantsWithTag(transform, objectsToAddCollider);
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
    {
        foreach (Transform child in parent)
        {
            if (child.gameObject.GetComponent<MeshRenderer>() != null
                && child.gameObject.GetComponent<)
            {
                list.Add(child.gameObject);
            }
            AddDescendantsWithTag(child, list);
        }
    }
}

在这一行中,我正在检查是否有附加到游戏对象的网格渲染器,但是如何检查它是否未附加任何碰撞类型?然后如何向其添加网格碰撞器?

这是我到目前为止尝试过的:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AddColliders : MonoBehaviour
{
    public List<GameObject> objectsToAddCollider = new List<GameObject>();

    // Start is called before the first frame update
    void Start()
    {
        AddDescendantsWithTag(transform, objectsToAddCollider);
    }

    // Update is called once per frame
    void Update()
    {

    }

    private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
    {
        foreach (Transform child in parent)
        {
            if (child.gameObject.GetComponent<MeshRenderer>() != null
                && child.gameObject.GetComponent<Collider>() == null)
            {
                child.gameObject.AddComponent<MeshCollider>();
                list.Add(child.gameObject);
            }
            AddDescendantsWithTag(child, list);
        }
    }
}

但是最后在行上添加断点时:

AddDescendantsWithTag(transform, objectsToAddCollider);

我看到Collider消息中的List objectsToAddCollider中的游戏对象:

collider = System.NotSupportedException:collider属性已被弃用

Collider

2 个答案:

答案 0 :(得分:4)

GameObject.collider在版本2019.1.0中已弃用并删除。

您不能再将其用于调试。


要检查是否使用任何类型的Collider

var collider = child.GetComponent<Collider>();

只需检查它是否存在,您也可以

if(child.GetComponent<Collider>())
{
    Debug.Log("Collider found");
}

再次是因为Collider(或更确切地说是从其继承的Unity类型Object)实现了等于!= null的{​​{3}}。


因此,如果该组件不存在于一行中,那么添加组件的一种很好的方法是

Collider collider = child.GetComponent<Collider>() ? collider : child.gameObject.AddComponent<Collider>();

甚至更短

Collider collider = child.GetComponent<Collider>() ?? child.gameObject.AddComponent<Collider>();

注意:请在智能手机上输入内容,因此没有保修,但我希望这个想法会清楚。

答案 1 :(得分:1)

您可以使用Collider,因为它是所有对撞机的基类。

    private void AddDescendantsWithTag(Transform parent, List<GameObject> list)
{
    foreach (Transform child in parent)
    {
        if (child.gameObject.GetComponent<MeshRenderer>() != null
            && child.gameObject.GetComponent<Collider>()) // You can modify this line according to your requirement. 
        {
            list.Add(child.gameObject);
        }
        AddDescendantsWithTag(child, list);
    }
}