void OnComputeMesh()
 {
     for (int i = 0; i < computeMeshGoList.Count; i++)
     {
         //1.清除原来的数据
         GameObject   go       = computeMeshGoList[i];
         MeshSimplify simplify = go.GetComponent <MeshSimplify>();
         if (go == null || simplify == null)
         {
             continue;
         }
         simplify.RestoreOriginalMesh(true, simplify.m_meshSimplifyRoot == null);
         RemoveChildMeshSimplifyComponents(simplify);
         //2.开始计算
         try
         {
             if (simplify.DataDirty || simplify.HasData() == false || simplify.HasNonMeshSimplifyGameObjectsInTree())
             {
                 simplify.RestoreOriginalMesh(true, simplify.m_meshSimplifyRoot == null);
                 bool mMeshSimplefyRoot = simplify.m_meshSimplifyRoot == null;
                 simplify.ComputeData(mMeshSimplefyRoot, null);
             }
         }
         catch (System.Exception e)
         {
             Debug.LogError("Error generating mesh: " + e.Message + " Stack: " + e.StackTrace);
             EditorUtility.ClearProgressBar();
             Simplifier.Cancelled = false;
         }
         //3.最后应用Prefab
     }
     isClickButton = true;
 }
Beispiel #2
0
    private static void DisablePrefabUsageRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.SimplifiedMesh)
                {
                    if (UnityEditor.AssetDatabase.IsMainAsset(meshSimplify.SimplifiedMesh) || UnityEditor.AssetDatabase.IsSubAsset(meshSimplify.SimplifiedMesh))
                    {
                        Mesh newMesh = Instantiate(meshSimplify.SimplifiedMesh) as Mesh;
                        meshSimplify.SimplifiedMesh = newMesh;
                    }
                }

                meshSimplify.AssetPath = null;
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                DisablePrefabUsageRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren);
            }
        }
    }
Beispiel #3
0
    private static bool HasVertexDataRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_simplifiedMesh && meshSimplify.m_simplifiedMesh.vertexCount > 0)
                {
                    return(true);
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                if (HasVertexDataRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren))
                {
                    return(true);
                }
            }
        }

        return(false);
    }
 void tUpdate()
 {
     foreach (Object targetObject in targets)
     {
         MeshSimplify meshSimplify = targetObject as MeshSimplify;
         //+ADD添加应用Job
         for (int ichild = 0; ichild < meshSimplify.transform.childCount; ichild++)
         {
             Transform    childTrans = meshSimplify.transform.GetChild(ichild);
             MeshSimplify simplify   = childTrans.GetComponent <MeshSimplify>();
             if (simplify != null && simplify.GetSimplifyComputeHeap() != null)
             {
                 ComputeHeap computeHeap = simplify.GetSimplifyComputeHeap();
                 if (!computeHeap.IsHandle())
                 {
                     EditorUtility.DisplayProgressBar("ComputeMeshJobExcute", childTrans.gameObject.name, 0f);
                 }
                 if (computeHeap.GetJobHandleIsComplete() && !computeHeap.IsHandle())
                 {
                     EditorUtility.DisplayDialog("Done", "Done", "Done");
                     EditorUtility.ClearProgressBar();
                     computeHeap.JobComputeComplete();
                 }
             }
         }
     }
 }
Beispiel #5
0
    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 = new Mesh();
                    }

                    meshSimplify.ConfigureSimplifier();

                    IEnumerator enumerator = meshSimplify.m_meshSimplifier.ComputeMeshWithVertexCount(gameObject, meshSimplify.m_simplifiedMesh, Mathf.RoundToInt(fAmount * meshSimplify.m_meshSimplifier.GetOriginalMeshUniqueVertexCount()), meshSimplify.name + " Simplified", progress);

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

                    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;
                }
            }
        }
    }
    public static bool IsRootOrBelongsToTree(MeshSimplify meshSimplify, MeshSimplify root)
    {
        if (meshSimplify == null)
        {
            return(false);
        }

        return((meshSimplify.m_bExcludedFromTree == false) && ((meshSimplify.m_meshSimplifyRoot == null) || (meshSimplify.m_meshSimplifyRoot == root) || (meshSimplify == root) || (meshSimplify.m_meshSimplifyRoot == root.m_meshSimplifyRoot)));
    }
    bool SaveMeshAssetsRecursive(GameObject root, GameObject gameObject, string strFile, bool bRecurseIntoChildren, bool bAssetAlreadyCreated, ref int nProgressElementsCounter)
    {
        if (gameObject == null || Simplifier.Cancelled)
        {
            return(bAssetAlreadyCreated);
        }

        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null && meshSimplify.HasData() && (meshSimplify.MeshSimplifyRoot == null || meshSimplify.MeshSimplifyRoot.gameObject == root))
        {
            int nTotalProgressElements = meshSimplify.MeshSimplifyRoot != null ? (meshSimplify.MeshSimplifyRoot.ListDependentChildren.Count + 1) : 1;

            if (meshSimplify.SimplifiedMesh != null && MeshUtil.HasValidMeshData(meshSimplify.gameObject))
            {
                float fT = (float)nProgressElementsCounter / (float)nTotalProgressElements;
                Progress("Saving meshes to asset file", meshSimplify.name, fT);

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

                if (bAssetAlreadyCreated == false /* && UnityEditor.AssetDatabase.Contains(meshSimplify.m_simplifiedMesh) == false*/)
                {
                    //Debug.Log("Creating asset " + meshSimplify.m_simplifiedMesh.name);

                    UnityEditor.AssetDatabase.CreateAsset(meshSimplify.SimplifiedMesh, strFile);
                    Resources.UnloadAsset(meshSimplify.SimplifiedMesh);
                    meshSimplify.SimplifiedMesh = null;
                    bAssetAlreadyCreated        = true;
                }
                else
                {
                    //if (UnityEditor.AssetDatabase.Contains(meshSimplify.m_simplifiedMesh) == false)
                    {
                        //Debug.Log("Adding asset " + meshSimplify.m_simplifiedMesh.name);

                        UnityEditor.AssetDatabase.AddObjectToAsset(meshSimplify.SimplifiedMesh, strFile);
                        UnityEditor.AssetDatabase.ImportAsset(UnityEditor.AssetDatabase.GetAssetPath(meshSimplify.SimplifiedMesh));
                    }
                }

                nProgressElementsCounter++;
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                bAssetAlreadyCreated = SaveMeshAssetsRecursive(root, gameObject.transform.GetChild(nChild).gameObject, strFile, bRecurseIntoChildren, bAssetAlreadyCreated, ref nProgressElementsCounter);
            }
        }

        return(bAssetAlreadyCreated);
    }
Beispiel #8
0
 public void CreateLODMeshes()
 {
     if (mf == null)
     {
         mf = GetComponent <MeshFilter>();
     }
     LODMesh_High = mf.sharedMesh;
     MeshSimplify.Simplify(mf.sharedMesh, mf.sharedMesh.triangles.Length / 2, out LODMesh_Medium);
     MeshSimplify.Simplify(mf.sharedMesh, mf.sharedMesh.triangles.Length / 6, out LODMesh_Low);
 }
    void SetHideFlags()
    {
        foreach (Object targetObject in targets)
        {
            MeshSimplify meshSimplify = targetObject as MeshSimplify;

            if (meshSimplify.MeshSimplifyRoot == null)
            {
                SetHideFlagsRecursive(meshSimplify.gameObject, meshSimplify.gameObject, true);
            }
        }
    }
    private static Mesh CreateNewEmptyMesh(MeshSimplify meshSimplify)
    {
        if (meshSimplify.m_originalMesh == null)
        {
            return(new Mesh());
        }

        Mesh meshOut = Mesh.Instantiate(meshSimplify.m_originalMesh);

        meshOut.Clear();
        return(meshOut);
    }
 void OnDisable()
 {
     if (m_bPreviewOriginalMesh)
     {
         foreach (Object targetObject in targets)
         {
             if (targetObject != null)
             {
                 MeshSimplify meshSimplify = targetObject as MeshSimplify;
                 meshSimplify.AssignSimplifiedMesh(true);
             }
         }
     }
 }
    private IEnumerator ComputeMeshWithVertices(float fAmount)
    {
        foreach (KeyValuePair <GameObject, Material[]> pair in m_objectMaterials)
        {
            MeshSimplify        meshSimplify = pair.Key.GetComponent <MeshSimplify>();
            MeshFilter          meshFilter   = pair.Key.GetComponent <MeshFilter>();
            SkinnedMeshRenderer skin         = pair.Key.GetComponent <SkinnedMeshRenderer>();

            if (meshSimplify && (meshFilter != null || skin != null))
            {
                Mesh newMesh = null;

                if (meshFilter != null)
                {
                    newMesh = Mesh.Instantiate(meshFilter.sharedMesh);
                }
                else if (skin != null)
                {
                    newMesh = Mesh.Instantiate(skin.sharedMesh);
                }

                if (meshSimplify.GetMeshSimplifier() != null)
                {
                    meshSimplify.GetMeshSimplifier().CoroutineEnded = false;

                    StartCoroutine(meshSimplify.GetMeshSimplifier().ComputeMeshWithVertexCount(pair.Key, newMesh, Mathf.RoundToInt(fAmount * meshSimplify.GetMeshSimplifier().GetOriginalMeshUniqueVertexCount()), meshSimplify.name, Progress));

                    while (meshSimplify.GetMeshSimplifier().CoroutineEnded == false)
                    {
                        yield return(null);
                    }

                    if (meshFilter != null)
                    {
                        meshFilter.mesh = newMesh;
                    }
                    else if (skin != null)
                    {
                        skin.sharedMesh = newMesh;
                    }

                    meshSimplify.m_simplifiedMesh = newMesh;
                }
            }
        }

        m_strLastTitle   = "";
        m_strLastMessage = "";
        m_nLastProgress  = 0;
    }
Beispiel #13
0
    private void AddMaterials(GameObject theGameObject, Dictionary <GameObject, Material[]> dicMaterials)
    {
        Renderer     theRenderer  = theGameObject.GetComponent <Renderer>();
        MeshSimplify meshSimplify = theGameObject.GetComponent <MeshSimplify>();

        if (theRenderer != null && theRenderer.sharedMaterials != null && meshSimplify != null)
        {
            dicMaterials.Add(theGameObject, theRenderer.sharedMaterials);
        }

        for (int i = 0; i < theGameObject.transform.childCount; i++)
        {
            AddMaterials(theGameObject.transform.GetChild(i).gameObject, dicMaterials);
        }
    }
    void SaveMeshAssets()
    {
        try
        {
            foreach (Object targetObject in targets)
            {
                MeshSimplify meshSimplify = targetObject as MeshSimplify;
                GameObject   gameObject   = meshSimplify.gameObject;

                if (meshSimplify.m_meshSimplifyRoot == null && meshSimplify.m_bEnablePrefabUsage)
                {
                    string strMeshAssetPath = meshSimplify.m_strAssetPath;

                    if (string.IsNullOrEmpty(strMeshAssetPath))
                    {
                        //Debug.Log("Showing file selection panel");

                        strMeshAssetPath = UnityEditor.EditorUtility.SaveFilePanelInProject("Save mesh asset(s)", "mesh_" + gameObject.name + gameObject.GetInstanceID().ToString() + ".asset", "asset", "Please enter a file name to save the mesh asset(s) to");

                        if (strMeshAssetPath.Length == 0)
                        {
                            //Debug.LogWarning("strMeshAssetPath.Length == 0. User cancelled?");
                            return;
                        }

                        //Debug.Log("User selected " + strMeshAssetPath + " using panel.");

                        meshSimplify.m_strAssetPath = strMeshAssetPath;
                    }

                    int nCounter = 0;

                    //Debug.Log("Saving files to " + strMeshAssetPath + ". Exists previously?: " + System.IO.File.Exists(strMeshAssetPath));
                    SaveMeshAssetsRecursive(gameObject, gameObject, strMeshAssetPath, true, System.IO.File.Exists(strMeshAssetPath), ref nCounter);
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError("Error saving mesh assets to disk: " + e.Message + " Stack: " + e.StackTrace);
            EditorUtility.ClearProgressBar();
            Simplifier.Cancelled = false;
        }

        EditorUtility.ClearProgressBar();
        UnityEditor.AssetDatabase.Refresh();
        Simplifier.Cancelled = false;
    }
Beispiel #15
0
    private void SetActiveObject(int index)
    {
        m_nSelectedIndex = index;

        MeshSimplify meshSimplify = Instantiate(ShowcaseObjects[index].m_meshSimplify);

        meshSimplify.transform.position = ShowcaseObjects[index].m_position;
        meshSimplify.transform.rotation = Quaternion.Euler(ShowcaseObjects[index].m_angles);

        m_selectedMeshSimplify = meshSimplify;

        m_objectMaterials = new Dictionary <GameObject, Material[]>();
        AddMaterials(meshSimplify.gameObject, m_objectMaterials);

        m_bWireframe = false;
    }
Beispiel #16
0
    private static bool HasOriginalMeshActiveRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        bool bHasOriginalMeshActive = false;

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_originalMesh != null)
                {
                    MeshFilter meshFilter = meshSimplify.GetComponent <MeshFilter>();

                    if (meshFilter != null)
                    {
                        if (meshFilter.sharedMesh == meshSimplify.m_originalMesh)
                        {
                            bHasOriginalMeshActive = true;
                        }
                    }
                    else
                    {
                        SkinnedMeshRenderer skin = meshSimplify.GetComponent <SkinnedMeshRenderer>();

                        if (skin != null)
                        {
                            if (skin.sharedMesh == meshSimplify.m_originalMesh)
                            {
                                bHasOriginalMeshActive = true;
                            }
                        }
                    }
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                bHasOriginalMeshActive = bHasOriginalMeshActive || HasOriginalMeshActiveRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren);
            }
        }

        return(bHasOriginalMeshActive);
    }
Beispiel #17
0
    private void AddMaterials(GameObject theGameObject, Dictionary <GameObject, Material[]> dicMaterials)
    {
        Renderer theRenderer = theGameObject.GetComponent <Renderer>();

        if (theRenderer != null && theRenderer.sharedMaterials != null && (MeshSimplify.HasValidMeshData(theGameObject) || theGameObject.GetComponent <MeshSimplify>() != null))
        {
            dicMaterials.Add(theGameObject, theRenderer.sharedMaterials);
        }

        if (m_selectedMeshSimplify.RecurseIntoChildren)
        {
            for (int i = 0; i < theGameObject.transform.childCount; i++)
            {
                AddMaterials(theGameObject.transform.GetChild(i).gameObject, dicMaterials);
            }
        }
    }
    void SetHideFlagsRecursive(GameObject root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify && meshSimplify.MeshSimplifier)
        {
            meshSimplify.MeshSimplifier.hideFlags = HideFlags.HideInInspector;
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                SetHideFlagsRecursive(root, gameObject.transform.GetChild(nChild).gameObject, true);
            }
        }
    }
Beispiel #19
0
    private void ComputeMeshWithVertices(float fAmount)
    {
        foreach (KeyValuePair <GameObject, Material[]> pair in m_objectMaterials)
        {
            GameObject          go           = pair.Key;
            MeshSimplify        meshSimplify = go.GetComponent <MeshSimplify>();
            MeshFilter          meshFilter   = null;
            SkinnedMeshRenderer skin;

            if (meshSimplify && ((skin = go.GetComponent <SkinnedMeshRenderer>()) != null || (meshFilter = go.GetComponent <MeshFilter>()) != null))
            {
                Mesh newMesh = null;
                if (skin != null)
                {
                    newMesh = new Mesh();//Mesh.Instantiate(skin.sharedMesh);
                }
                else// if (meshFilter != null)
                {
                    newMesh = new Mesh(); //Mesh.Instantiate(meshFilter.sharedMesh);
                }

                if (meshSimplify.MeshSimplifier != null)
                {
                    Profiler.BeginSample("ComputeMeshWithVertexCount");
                    meshSimplify.MeshSimplifier.ComputeMeshWithVertexCount(go, newMesh, Mathf.RoundToInt(fAmount * meshSimplify.MeshSimplifier.GetOriginalMeshUniqueVertexCount()));
                    Profiler.EndSample();

                    if (skin != null)
                    {
                        skin.sharedMesh = newMesh;
                    }
                    else// if (meshFilter != null)
                    {
                        meshFilter.mesh = newMesh;
                    }

                    meshSimplify.m_simplifiedMesh = newMesh;
                }
            }
        }

        m_strLastTitle   = "";
        m_strLastMessage = "";
        m_nLastProgress  = 0;
    }
    private static bool HasNonMeshSimplifyGameObjectsInTreeRecursive(MeshSimplify root, GameObject gameObject)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify == null && MeshUtil.HasValidMeshData(gameObject))
        {
            return(true);
        }

        for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
        {
            if (HasNonMeshSimplifyGameObjectsInTreeRecursive(root, gameObject.transform.GetChild(nChild).gameObject))
            {
                return(true);
            }
        }

        return(false);
    }
    private static void RestoreOriginalMeshRecursive(MeshSimplify root, GameObject gameObject, bool bDeleteData, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_originalMesh != null)
                {
                    MeshFilter meshFilter = meshSimplify.GetComponent <MeshFilter>();

                    if (meshFilter != null)
                    {
                        meshFilter.sharedMesh = meshSimplify.m_originalMesh;
                    }
                    else
                    {
                        SkinnedMeshRenderer skin = meshSimplify.GetComponent <SkinnedMeshRenderer>();

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

                if (bDeleteData)
                {
                    meshSimplify.FreeData(false);
                    meshSimplify.m_listDependentChildren.Clear();
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                RestoreOriginalMeshRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bDeleteData, bRecurseIntoChildren);
            }
        }
    }
    private static void FreeDataRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

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

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

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

                meshSimplify.m_bDataDirty = true;
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                FreeDataRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren);
            }
        }
    }
    void RemoveChildMeshSimplifyComponentsRecursive(GameObject root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null && meshSimplify.m_meshSimplifyRoot != null)
        {
            if (Application.isEditor && Application.isPlaying == false)
            {
                UnityEngine.Object.DestroyImmediate(meshSimplify, true);
            }
            else
            {
                UnityEngine.Object.Destroy(meshSimplify);
            }
        }
        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                RemoveChildMeshSimplifyComponentsRecursive(root, gameObject.transform.GetChild(nChild).gameObject, true);
            }
        }
    }
    private static void GetSimplifiedTriangleCountRecursive(MeshSimplify root, GameObject gameObject, ref int nTriangleCount, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_simplifiedMesh != null)
                {
                    nTriangleCount += meshSimplify.m_simplifiedMesh.triangles.Length / 3;
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                GetSimplifiedTriangleCountRecursive(root, gameObject.transform.GetChild(nChild).gameObject, ref nTriangleCount, bRecurseIntoChildren);
            }
        }
    }
    //private static bool HasOriginalMeshActiveRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    //{
    //    MeshSimplify meshSimplify = gameObject.GetComponent<MeshSimplify>();

    //    bool bHasOriginalMeshActive = false;

    //    if (meshSimplify != null)
    //    {
    //        if (IsRootOrBelongsToTree(meshSimplify, root))
    //        {
    //            if (meshSimplify.m_originalMesh != null)
    //            {
    //                MeshFilter meshFilter = meshSimplify.GetComponent<MeshFilter>();

    //                if (meshFilter != null)
    //                {
    //                    if (meshFilter.sharedMesh == meshSimplify.m_originalMesh)
    //                    {
    //                        bHasOriginalMeshActive = true;
    //                    }
    //                }
    //                else
    //                {
    //                    SkinnedMeshRenderer skin = meshSimplify.GetComponent<SkinnedMeshRenderer>();

    //                    if (skin != null)
    //                    {
    //                        if (skin.sharedMesh == meshSimplify.m_originalMesh)
    //                        {
    //                            bHasOriginalMeshActive = true;
    //                        }
    //                    }
    //                }
    //            }
    //        }
    //    }

    //    if (bRecurseIntoChildren)
    //    {
    //        for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
    //        {
    //            bHasOriginalMeshActive = bHasOriginalMeshActive || HasOriginalMeshActiveRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren);
    //        }
    //    }

    //    return bHasOriginalMeshActive;
    //}
    //private static bool HasVertexDataRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    //{
    //    MeshSimplify meshSimplify = gameObject.GetComponent<MeshSimplify>();

    //    if (meshSimplify != null)
    //    {
    //        if (IsRootOrBelongsToTree(meshSimplify, root))
    //        {
    //            if (meshSimplify.m_simplifiedMesh && meshSimplify.m_simplifiedMesh.vertexCount > 0)
    //            {
    //                return true;
    //            }
    //        }
    //    }

    //    if (bRecurseIntoChildren)
    //    {
    //        for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
    //        {
    //            if (HasVertexDataRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren))
    //            {
    //                return true;
    //            }
    //        }
    //    }

    //    return false;
    //}
    private static void GetOriginalVertexCountRecursive(MeshSimplify root, GameObject gameObject, ref int nVertexCount, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_originalMesh != null)
                {
                    nVertexCount += meshSimplify.m_originalMesh.vertexCount;
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                GetOriginalVertexCountRecursive(root, gameObject.transform.GetChild(nChild).gameObject, ref nVertexCount, bRecurseIntoChildren);
            }
        }
    }
    private static void AssignSimplifiedMeshRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_simplifiedMesh != null)
                {
                    MeshFilter meshFilter = meshSimplify.GetComponent <MeshFilter>();

                    if (meshFilter != null)
                    {
                        meshFilter.sharedMesh = meshSimplify.m_simplifiedMesh;
                    }
                    else
                    {
                        SkinnedMeshRenderer skin = meshSimplify.GetComponent <SkinnedMeshRenderer>();

                        if (skin != null)
                        {
                            skin.sharedMesh = meshSimplify.m_simplifiedMesh;
                        }
                    }
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                AssignSimplifiedMeshRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren);
            }
        }
    }
 void Update()
 {
     if (isClickButton)
     {
         for (int i = 0; i < computeMeshGoList.Count; i++)
         {
             EditorUtility.DisplayProgressBar("OnComputeMesh", "mesh|" + computeMeshGoList[i] + " " + index, (float)index / computeMeshGoList.Count);
             GameObject go = computeMeshGoList[i];
             if (go == null)
             {
                 continue;
             }
             for (int ichild = 0; ichild < go.transform.childCount; ichild++)
             {
                 Transform    childTrans = go.transform.GetChild(ichild);
                 MeshSimplify simplify   = childTrans.GetComponent <MeshSimplify>();
                 if (simplify != null && simplify.GetSimplifyComputeHeap() != null)
                 {
                     ComputeHeap computeHeap = simplify.GetSimplifyComputeHeap();
                     if (computeHeap.GetJobHandleIsComplete())
                     {
                         computeHeap.JobComputeComplete();
                         index++;
                     }
                 }
             }
         }
     }
     if (index != 0 && index == computeMeshGoList.Count)
     {
         isClickButton = false;
         index         = 0;
         EditorUtility.ClearProgressBar();
         Debug.Log("<color=yellow>所有的模型数据计算完成————————</color>");
     }
 }
    void OnSceneGUI()
    {
        MeshSimplify meshSimplify = target as MeshSimplify;

        bool bDrawSpheres = true;

        if (meshSimplify.MeshSimplifyRoot != null)
        {
            if (meshSimplify.MeshSimplifyRoot.ExpandRelevanceSpheres == false)
            {
                bDrawSpheres = false;
            }
        }
        else
        {
            if (meshSimplify.ExpandRelevanceSpheres == false)
            {
                bDrawSpheres = false;
            }
        }

        if (meshSimplify.RelevanceSpheres != null && bDrawSpheres)
        {
            for (int nSphere = 0; nSphere < meshSimplify.RelevanceSpheres.Length; nSphere++)
            {
                if (meshSimplify.RelevanceSpheres[nSphere].Expanded == false)
                {
                    continue;
                }

                RelevanceSphere relevanceSphere = meshSimplify.RelevanceSpheres[nSphere];

                if (Tools.current == Tool.Move)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 v3Position = Handles.PositionHandle(relevanceSphere.Position, relevanceSphere.Rotation);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Move Relevance Sphere");
                        relevanceSphere.Position = v3Position;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }
                else if (Tools.current == Tool.Rotate)
                {
                    EditorGUI.BeginChangeCheck();
                    Quaternion qRotation = Handles.RotationHandle(relevanceSphere.Rotation, relevanceSphere.Position);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Rotate Relevance Sphere");
                        relevanceSphere.Rotation = qRotation;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }
                else if (Tools.current == Tool.Scale)
                {
                    EditorGUI.BeginChangeCheck();
                    Vector3 v3Scale = Handles.ScaleHandle(relevanceSphere.Scale, relevanceSphere.Position, relevanceSphere.Rotation, HandleUtility.GetHandleSize(relevanceSphere.Position) * 1.0f);
                    if (EditorGUI.EndChangeCheck())
                    {
                        Undo.RecordObject(meshSimplify, "Scale Relevance Sphere");
                        relevanceSphere.Scale = v3Scale;
                        meshSimplify.RestoreOriginalMesh(false, true);
                        meshSimplify.DataDirty = true;
                        EditorUtility.SetDirty(target);
                    }
                }

                if (Event.current.type == EventType.Repaint)
                {
                    Matrix4x4 mtxHandles = Handles.matrix;
                    Handles.matrix = Matrix4x4.TRS(relevanceSphere.Position, relevanceSphere.Rotation, relevanceSphere.Scale);
                    Handles.color  = new Color(0.0f, 0.0f, 1.0f, 0.5f);
                    Handles.SphereHandleCap(0, Vector3.zero, Quaternion.identity, 1.0f, EventType.Repaint);
                    Handles.matrix = mtxHandles;
                }
            }
        }
    }
    void SaveMeshAssets()
    {
        try
        {
            foreach (Object targetObject in targets)
            {
                MeshSimplify meshSimplify = targetObject as MeshSimplify;
                GameObject   gameObject   = meshSimplify.gameObject;

                if (meshSimplify.MeshSimplifyRoot == null /* && meshSimplify.m_bEnablePrefabUsage*/)
                {
                    string strMeshAssetPath = meshSimplify.AssetPath;

                    //if (string.IsNullOrEmpty(strMeshAssetPath))
                    {
                        //Debug.Log("Showing file selection panel");
                        string path;
                        string name;
                        if (string.IsNullOrEmpty(strMeshAssetPath))
                        {
                            path = AssetDatabase.GetAssetPath(gameObject);
                            name = Path.GetFileNameWithoutExtension(path);
                            path = Path.GetDirectoryName(path);
                        }
                        else
                        {
                            path = strMeshAssetPath;
                            name = Path.GetFileNameWithoutExtension(path);
                            path = Path.GetDirectoryName(path);
                            int idx = name.LastIndexOf('_');
                            if (idx >= 0)
                            {
                                name = name.Remove(idx, name.Length - idx);
                            }
                        }
                        if (path.StartsWith("Assets/"))
                        {
                            path = path.Remove(0, 7);
                        }
                        strMeshAssetPath = UnityEditor.EditorUtility.SaveFilePanelInProject("Save mesh asset(s)", name + "_" + meshSimplify.GetSimplifiedTriangleCount(true).ToString() + ".asset", "asset", "Please enter a file name to save the mesh asset(s) to", path);

                        if (strMeshAssetPath.Length == 0)
                        {
                            //Debug.LogWarning("strMeshAssetPath.Length == 0. User cancelled?");
                            return;
                        }

                        //Debug.Log("User selected " + strMeshAssetPath + " using panel.");

                        meshSimplify.AssetPath = strMeshAssetPath;
                    }

                    int nCounter = 0;

                    //Debug.Log("Saving files to " + strMeshAssetPath + ". Exists previously?: " + System.IO.File.Exists(strMeshAssetPath));
                    SaveMeshAssetsRecursive(gameObject, gameObject, strMeshAssetPath, true, System.IO.File.Exists(strMeshAssetPath), ref nCounter);
                }
            }
        }
        catch (System.Exception e)
        {
            Debug.LogError("Error saving mesh assets to disk: " + e.Message + " Stack: " + e.StackTrace);
            EditorUtility.ClearProgressBar();
            Simplifier.Cancelled = false;
        }

        EditorUtility.ClearProgressBar();
        UnityEditor.AssetDatabase.Refresh();
        Simplifier.Cancelled = false;
    }
 void RemoveChildMeshSimplifyComponents(MeshSimplify meshSimplify)
 {
     RemoveChildMeshSimplifyComponentsRecursive(meshSimplify.gameObject, meshSimplify.gameObject, true);
 }