模型Mesh检测工具

在大型项目中经常遇到新旧模型替换的问题,如果没有一个好的替换习惯很容易造成模型Mesh丢失的情况。unity在切换场景的时候会因为模型丢失报错

例如:

NullReferenceException: Object reference not set to an instance of an object
UnityEngine.InternalStaticBatchingUtility.CombineGameObjects (UnityEngine.GameObject[] gos, UnityEngine.GameObject staticBatchRoot, System.Boolean isEditorPostprocessScene, UnityEngine.InternalStaticBatchingUtility+StaticBatcherGOSorter sorter) (at <2e0a48134cc845028fb11656fdc79d23>:0)
UnityEngine.InternalStaticBatchingUtility.Combine (UnityEngine.GameObject staticBatchRoot, System.Boolean combineOnlyStatic, System.Boolean isEditorPostprocessScene, UnityEngine.InternalStaticBatchingUtility+StaticBatcherGOSorter sorter) (at <2e0a48134cc845028fb11656fdc79d23>:0)
UnityEditor.UnityBuildPostprocessor.OnProcessScene (UnityEngine.SceneManagement.Scene scene, UnityEditor.Build.Reporting.BuildReport report) (at <23d96447c030422fb76b5c08cdcf57cc>:0)
UnityEditor.Build.BuildPipelineInterfaces+<>c__DisplayClass16_0.<OnSceneProcess>b__1 (UnityEditor.Build.IProcessSceneWithReport spp) (at <23d96447c030422fb76b5c08cdcf57cc>:0)
UnityEditor.Build.BuildPipelineInterfaces.InvokeCallbackInterfacesPair[T1,T2] (System.Collections.Generic.List`1[T] oneInterfaces, System.Action`1[T] invocationOne, System.Collections.Generic.List`1[T] twoInterfaces, System.Action`1[T] invocationTwo, System.Boolean exitOnFailure) (at <23d96447c030422fb76b5c08cdcf57cc>:0)
UnityEditor.Build.BuildPipelineInterfaces:OnSceneProcess(Scene, BuildReport)

虽然这种错误不是致命的,但是作为程序开发者不应该允许程序有报错而不解决的事情发生,因此我编写了一段简易检测模型Mesh是否丢失的代码。

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

public class CheckMesh : MonoBehaviour
{
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
            Check();
    }
    void Check()
    {
        GameObject[] all = Resources.FindObjectsOfTypeAll(typeof(GameObject)) as GameObject[];
        for (int i = 0; i < all.Length; i++)
        {
            var item = all[i];
            if (item.scene.isLoaded && item.activeInHierarchy && item.GetComponent<MeshFilter>()!=null)
            {
                var mesh = item.GetComponent<MeshFilter>().sharedMesh;
                if (mesh == null || mesh.vertexCount <= 0)
                {
                    print(GetTransPath(item.transform));
                }
            }
        }
    }
    /// <summary>
    /// 获得GameObject在Hierarchy中的完整路径
    /// </summary>
    public static string GetTransPath(Transform trans)
    {
        if (!trans.parent)
        {
            return trans.name;

        }
        return GetTransPath(trans.parent) + "/" + trans.name;
    }
}

将此代码放到场景任意激活物体上运行即可,场景加载完成后点击空格就会打印Mesh丢失模型的具体路径。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值