繁体   English   中英

如何知道一个 GameObject 在 Unity 中是否有网格?

[英]How to know if a GameObject has a mesh in Unity?

我将以下脚本附加到 GameObject 以确定它是否是网格。

using UnityEngine;

public class MeshCheck : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var meshChecker = this.GetComponent<MeshRenderer>();
        if(meshChecker != null)
        {
            Debug.Log("this is mesh");
        }
        else
        {
            Debug.Log("this is not mesh");
        }
    }
}

就我尝试过的而言,这个脚本似乎工作正常。

但是,这种方法似乎无法确定所有网格。

例如,有一个带有 blendShapeds 的网格。我打开并查看了该网格的 Inspector。 网格似乎有“SkinMeshRenderer”,而网格没有“MeshRenderer”。 而上面的脚本 output “这不是网格”。

这个问题的解决方法是修改脚本如下:

using UnityEngine;

public class MeshCheck : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var meshChecker = this.GetComponent<MeshRenderer>();
        var blendShapeChecker = this.GetComponent<SkinnedMeshRenderer>();
        if (meshChecker != null || blendShapeChecker != null)
        {
            Debug.Log("this is mesh");
        }
        else
        {
            Debug.Log("this is not mesh");
        }
    }
}

我想知道第二个脚本是否解决了所有问题。 如果有人知道如何准确确定所有网格,请告诉我。

您可以使用网格过滤器来避免通过代码获得所有控件。

以这种方式而不是拥有

if (meshChecker != null || blendShapeChecker != null || othermeshes...)

你会检查

if(MeshFilter.mesh != null)

但是使用网格过滤器,您可以使用多个网格并使用Mesh.SubMeshCountMesh.setIndices甚至Mesh.CombineMeshes检查所有网格,但这完全取决于游戏所需的使用!

暂无
暂无

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

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