简体   繁体   English

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

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

I attached the following script to a GameObject to determine if it is a mesh.我将以下脚本附加到 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");
        }
    }
}

This script seems to work fine as far as I've tried.就我尝试过的而言,这个脚本似乎工作正常。

However, this method does not seem to be able to determine all meshes.但是,这种方法似乎无法确定所有网格。

For example, there is a mesh with a blendShapeds.I opened and looked at the Inspector for that mesh.例如,有一个带有 blendShapeds 的网格。我打开并查看了该网格的 Inspector。 It seemed that the the mesh have "SkinMeshRenderer" and the mesh didn't have "MeshRenderer".网格似乎有“SkinMeshRenderer”,而网格没有“MeshRenderer”。 And the above script output "this is not mesh".而上面的脚本 output “这不是网格”。

The solution to this problem is to modify the script as follows:这个问题的解决方法是修改脚本如下:

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");
        }
    }
}

I'm wondering if the second script solves all the problems.我想知道第二个脚本是否解决了所有问题。 If anyone knows how to accurately determine all meshes, please let me know.如果有人知道如何准确确定所有网格,请告诉我。

You can use the Mesh Filter to avoid having all the controls via code.您可以使用网格过滤器来避免通过代码获得所有控件。

In this way instead of having以这种方式而不是拥有

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

You will check你会检查

if(MeshFilter.mesh != null)

But with the Mesh Filter you can play with multiple meshes and check between all of them with theMesh.SubMeshCount or the Mesh.setIndices or even Mesh.CombineMeshes but it's all about the usage you need for the game!但是使用网格过滤器,您可以使用多个网格并使用Mesh.SubMeshCountMesh.setIndices甚至Mesh.CombineMeshes检查所有网格,但这完全取决于游戏所需的使用!

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

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