private static void ComputeDataRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren, Simplifier.ProgressDelegate progress = null)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify == null && root.m_bGenerateIncludeChildren)
        {
            if (MeshUtil.HasValidMeshData(gameObject))
            {
                meshSimplify = gameObject.AddComponent <MeshSimplify>();
                meshSimplify.m_meshSimplifyRoot = root;
                root.m_listDependentChildren.Add(meshSimplify);
            }
        }

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                meshSimplify.FreeData(false);

                MeshFilter meshFilter = meshSimplify.GetComponent <MeshFilter>();

                if (meshFilter != null && meshFilter.sharedMesh != null)
                {
                    if (meshFilter.sharedMesh.vertexCount > 0)
                    {
                        if (meshSimplify.m_originalMesh == null)
                        {
                            meshSimplify.m_originalMesh = meshFilter.sharedMesh;
                        }

                        Simplifier[] simplifiers = meshSimplify.GetComponents <Simplifier>();

                        for (int c = 0; c < simplifiers.Length; c++)
                        {
                            if (Application.isEditor && Application.isPlaying == false)
                            {
                                DestroyImmediate(simplifiers[c]);
                            }
                            else
                            {
                                Destroy(simplifiers[c]);
                            }
                        }

                        meshSimplify.m_meshSimplifier           = meshSimplify.gameObject.AddComponent <Simplifier>();
                        meshSimplify.m_meshSimplifier.hideFlags = HideFlags.HideInInspector;
                        meshSimplify.ConfigureSimplifier();

                        IEnumerator enumerator = meshSimplify.m_meshSimplifier.ProgressiveMesh(gameObject, meshSimplify.m_originalMesh, root.m_aRelevanceSpheres, meshSimplify.name, progress);

                        while (enumerator.MoveNext())
                        {
                            if (Simplifier.Cancelled)
                            {
                                return;
                            }
                        }

                        if (Simplifier.Cancelled)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    SkinnedMeshRenderer skin = meshSimplify.GetComponent <SkinnedMeshRenderer>();

                    if (skin != null)
                    {
                        if (skin.sharedMesh.vertexCount > 0)
                        {
                            if (meshSimplify.m_originalMesh == null)
                            {
                                meshSimplify.m_originalMesh = skin.sharedMesh;
                            }

                            Simplifier[] simplifiers = meshSimplify.GetComponents <Simplifier>();

                            for (int c = 0; c < simplifiers.Length; c++)
                            {
                                if (Application.isEditor && Application.isPlaying == false)
                                {
                                    DestroyImmediate(simplifiers[c]);
                                }
                                else
                                {
                                    Destroy(simplifiers[c]);
                                }
                            }

                            meshSimplify.m_meshSimplifier           = meshSimplify.gameObject.AddComponent <Simplifier>();
                            meshSimplify.m_meshSimplifier.hideFlags = HideFlags.HideInInspector;
                            meshSimplify.ConfigureSimplifier();

                            IEnumerator enumerator = meshSimplify.m_meshSimplifier.ProgressiveMesh(gameObject, meshSimplify.m_originalMesh, root.m_aRelevanceSpheres, meshSimplify.name, progress);

                            while (enumerator.MoveNext())
                            {
                                if (Simplifier.Cancelled)
                                {
                                    return;
                                }
                            }

                            if (Simplifier.Cancelled)
                            {
                                return;
                            }
                        }
                    }
                }

                meshSimplify.m_bDataDirty = false;
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                ComputeDataRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren, progress);

                if (Simplifier.Cancelled)
                {
                    return;
                }
            }
        }
    }
 public void ComputeMesh(bool bRecurseIntoChildren, Simplifier.ProgressDelegate progress = null)
 {
     ComputeMeshRecursive(this, this.gameObject, bRecurseIntoChildren, progress);
 }
    private static void ComputeMeshRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren, Simplifier.ProgressDelegate progress = null)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_meshSimplifier != null)
                {
                    if (meshSimplify.m_simplifiedMesh)
                    {
                        meshSimplify.m_simplifiedMesh.Clear();
                    }

                    float fAmount = meshSimplify.m_fVertexAmount;

                    if (meshSimplify.m_bOverrideRootSettings == false && meshSimplify.m_meshSimplifyRoot != null)
                    {
                        fAmount = meshSimplify.m_meshSimplifyRoot.m_fVertexAmount;
                    }

                    if (meshSimplify.m_simplifiedMesh == null)
                    {
                        meshSimplify.m_simplifiedMesh = CreateNewEmptyMesh(meshSimplify);
                    }

                    meshSimplify.ConfigureSimplifier();

                    meshSimplify.m_meshSimplifier.ComputeMeshWithVertexCount(gameObject, meshSimplify.m_simplifiedMesh, Mathf.RoundToInt(fAmount * meshSimplify.m_meshSimplifier.GetOriginalMeshUniqueVertexCount()));

                    if (Simplifier.Cancelled)
                    {
                        return;
                    }
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                ComputeMeshRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren, progress);

                if (Simplifier.Cancelled)
                {
                    return;
                }
            }
        }
    }