void bakeTexturesOnly()
    {
        if (!doCombinedValidate(MB_ObjsToCombineTypes.prefabOnly))
        {
            return;
        }

        MB_AtlasesAndRects[] mAndAs = null;
        try{
            MB_MeshBaker mbd = (MB_MeshBaker)target;
            mAndAs = mbd.CreateAtlases(updateProgressBar);
            if (mAndAs != null)
            {
                for (int i = 0; i < mAndAs.Length; i++)
                {
                    MB_AtlasesAndRects mAndA = mAndAs[i];
                    updateProgressBar("Created mesh saving assets", .6f);
                    Material resMat = mbd.resultMaterial;
                    if (mbd.doMultiMaterial)
                    {
                        resMat = mbd.resultMaterials[i].combinedMaterial;
                    }
                    saveAtlasesToAssetDatabase(mAndA, resMat);
                    meshBaker.SetIsDifferentCacheDirty();
                    updateProgressBar("Replacing prefab", .7f);
                }
            }
        } catch (Exception e) {
            Debug.LogError(e);
        } finally {
            EditorUtility.ClearProgressBar();
            if (mAndAs != null)
            {
                for (int j = 0; j < mAndAs.Length; j++)
                {
                    MB_AtlasesAndRects mAndA = mAndAs[j];
                    if (mAndA != null && mAndA.atlases != null)
                    {
                        for (int i = 0; i < mAndA.atlases.Length; i++)
                        {
                            if (mAndA.atlases[i] != null)
                            {
                                MB_Utility.Destroy(mAndA.atlases[i]);
                            }
                        }
                    }
                }
            }
        }
    }
    void bakeMeshesInPlace()
    {
        MB_MeshBaker mom = (MB_MeshBaker)target;

        MB_AtlasesAndRects[] mAndAs = null;
        Mesh mesh;

        if (!doCombinedValidate(MB_ObjsToCombineTypes.prefabOnly))
        {
            return;
        }

        try{
            mom.DestroyMesh();
            mAndAs = mom.CreateAtlases(updateProgressBar);
            if (mAndAs != null)
            {
                for (int i = 0; i < mAndAs.Length; i++)
                {
                    MB_AtlasesAndRects mAndA  = mAndAs[i];
                    Material           resMat = mom.resultMaterial;
                    if (mom.doMultiMaterial)
                    {
                        resMat = mom.resultMaterials[i].combinedMaterial;
                    }
                    saveAtlasesToAssetDatabase(mAndA, resMat);
                }
                GameObject[] objs = new GameObject[1];
                for (int i = 0; i < mom.objsToMesh.Count; i++)
                {
                    objs[0] = mom.objsToMesh[i];
                    mesh    = mom.AddDeleteGameObjects(objs, null, false);
                    if (mesh != null)
                    {
                        mom.ApplyAll();
                        MeshFilter mf = objs[0].GetComponent <MeshFilter>();
                        if (mf != null && mf.sharedMesh != null)
                        {
                            string baseName, folderPath, newFilename;
                            string pth = AssetDatabase.GetAssetPath(mf.sharedMesh);
                            if (pth != null && pth.Length != 0)
                            {
                                baseName    = System.IO.Path.GetFileNameWithoutExtension(pth);
                                folderPath  = System.IO.Path.GetDirectoryName(pth);
                                newFilename = System.IO.Path.Combine(folderPath, baseName + "_MB.asset");
                            }
                            else                                           //try to get the name from prefab
                            {
                                pth = AssetDatabase.GetAssetPath(objs[0]); //get prefab name
                                if (pth != null && pth.Length != 0)
                                {
                                    baseName    = System.IO.Path.GetFileNameWithoutExtension(pth);
                                    folderPath  = System.IO.Path.GetDirectoryName(pth);
                                    newFilename = System.IO.Path.Combine(folderPath, baseName + "_" + objs[0].name + "_MB.asset");
                                }
                                else                                     //save in root
                                {
                                    newFilename = System.IO.Path.Combine("Assets", objs[0].name + "mesh_MB.asset");
                                }
                            }
                            updateProgressBar("Created mesh saving asset " + newFilename, .6f);
                            if (newFilename != null && newFilename.Length != 0)
                            {
                                Debug.Log("Created mesh with adjusted UVs at: " + newFilename);
                                AssetDatabase.CreateAsset(mesh, newFilename);
                            }
                            else
                            {
                                Debug.LogWarning("Could not save mesh for " + objs[0].name);
                            }
                        }
                    }
                    mom.DestroyMesh();
                }
            }
        } catch (Exception e) {
            Debug.LogError(e);
        } finally {
            EditorUtility.ClearProgressBar();
            mom.DestroyMesh();
            if (mAndAs != null)
            {
                for (int j = 0; j < mAndAs.Length; j++)
                {
                    MB_AtlasesAndRects mAndA = mAndAs[j];
                    if (mAndA != null && mAndA.atlases != null)
                    {
                        for (int i = 0; i < mAndA.atlases.Length; i++)
                        {
                            if (mAndA.atlases[i] != null)
                            {
                                MB_Utility.Destroy(mAndA.atlases[i]);
                            }
                        }
                    }
                }
            }
        }
    }
    void bakeMeshesIntoPrefab()
    {
        MB_MeshBaker mom = (MB_MeshBaker)target;

        MB_AtlasesAndRects[] mAndAs = null;
        Mesh mesh;

        if (!doCombinedValidate(MB_ObjsToCombineTypes.sceneObjOnly))
        {
            return;
        }
        if (mom.resultPrefab == null)
        {
            Debug.LogError("Need to set the result prefab field.");
            return;
        }
        try{
            mom.DestroyMesh();
            mAndAs = mom.CreateAtlases(updateProgressBar);
            if (mAndAs != null)
            {
                for (int i = 0; i < mAndAs.Length; i++)
                {
                    MB_AtlasesAndRects mAndA  = mAndAs[i];
                    Material           resMat = mom.resultMaterial;
                    if (mom.doMultiMaterial)
                    {
                        resMat = mom.resultMaterials[i].combinedMaterial;
                    }
                    saveAtlasesToAssetDatabase(mAndA, resMat);
                }
                mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true);
                if (mesh != null)
                {
                    mom.ApplyAll();
                    updateProgressBar("Created mesh saving assets", .6f);
                    saveMeshToAssetDatabase(mesh);
                    updateProgressBar("Replacing prefab", .7f);
                    rebuildPrefab(mAndAs, mesh);
                }
            }
        } catch (Exception e) {
            Debug.LogError(e);
        } finally {
            EditorUtility.ClearProgressBar();
            mom.DestroyMesh();
            if (mAndAs != null)
            {
                for (int j = 0; j < mAndAs.Length; j++)
                {
                    MB_AtlasesAndRects mAndA = mAndAs[j];
                    if (mAndA != null && mAndA.atlases != null)
                    {
                        for (int i = 0; i < mAndA.atlases.Length; i++)
                        {
                            if (mAndA.atlases[i] != null)
                            {
                                MB_Utility.Destroy(mAndA.atlases[i]);
                            }
                        }
                    }
                }
            }
        }
    }