如何知道游戏对象在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。网状物似乎有";SkinMashRenderer";并且网格没有";网格渲染器";。并且上面的脚本输出";这不是网格";。

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

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.SubMeshCount或Mesh.setIndices甚至Mesh.CombineMeshes在所有网格之间进行检查,但这一切都与游戏所需的用法有关!

最新更新