void bake(MB2_MeshBakerCommon mom)
        {
            try{
                if (mom.outputOption == MB2_OutputOptions.bakeIntoSceneObject)
                {
                    MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoSceneObject);
                }
                else if (mom.outputOption == MB2_OutputOptions.bakeIntoPrefab)
                {
                    MB2_MeshBakerEditorFunctions._bakeIntoCombined(mom, MB_OutputOptions.bakeIntoPrefab);
                }
                else
                {
                    if (mom is MB2_MeshBaker)
                    {
                        if (MB2_MeshCombiner.EVAL_VERSION)
                        {
                            Debug.LogError("Bake Meshes In Place is disabled in the evaluation version.");
                        }
                        else
                        {
                            if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.prefabOnly, new MB2_EditorMethods()))
                            {
                                return;
                            }

                            List <GameObject> objsToMesh = mom.objsToMesh;
                            if (mom.useObjsToMeshFromTexBaker && mom.GetComponent <MB2_TextureBaker>() != null)
                            {
                                objsToMesh = mom.GetComponent <MB2_TextureBaker>().objsToMesh;
                            }
                            ((MB2_MeshBaker)mom)._update_MB2_MeshCombiner();
                            MB2_BakeInPlace.BakeMeshesInPlace(((MB2_MeshBaker)mom).meshCombiner, objsToMesh, mom.bakeAssetsInPlaceFolderPath, updateProgressBar);
                        }
                    }
                    else
                    {
                        Debug.LogError("Multi-mesh Baker components cannot be used for Bake In Place. Use an ordinary Mesh Baker object instead.");
                    }
                }
            } catch (Exception e) {
                Debug.LogError(e);
            } finally {
                EditorUtility.ClearProgressBar();
            }
        }
    public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject)
    {
        if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
        {
            Debug.LogError("Cannot BakeIntoPrefab with evaluation version.");
            return;
        }
        if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject)
        {
            Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
            return;
        }
        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>();

        if (mom.textureBakeResults == null && tb != null)
        {
            Debug.Log("setting results");
            mom.textureBakeResults = tb.textureBakeResults;
        }

        if (mom.useObjsToMeshFromTexBaker)
        {
            if (tb != null)
            {
                mom.objsToMesh.Clear();
                mom.objsToMesh.AddRange(tb.objsToMesh);
            }
        }

        Mesh mesh;

        if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly))
        {
            return;
        }
        if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab &&
            mom.resultPrefab == null)
        {
            Debug.LogError("Need to set the Combined Mesh Prefab field.");
            return;
        }
        mom.ClearMesh();

        mesh = mom.AddDeleteGameObjects(mom.objsToMesh.ToArray(), null, false, true);
        if (mesh != null)
        {
            mom.Apply();
//			updateProgressBar("Created mesh saving assets",.6f);
            if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject)
            {
//				mom.BuildSceneMeshObject();
            }
            else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
            {
                string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
                if (prefabPth == null || prefabPth.Length == 0)
                {
                    Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
                    return;
                }
                string baseName    = Path.GetFileNameWithoutExtension(prefabPth);
                string folderPath  = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7);
                string newFilename = folderPath + baseName + "-mesh";
                mom.SaveMeshsToAssetDatabase(folderPath, newFilename);

                if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
                {
                    Debug.LogWarning("Render type is skinned mesh renderer. " +
                                     "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab +
                                     " Add the bones then drag the combined mesh object to the prefab.");
                }
                else
                {
                    mom.RebuildPrefab();
                }
            }
            else
            {
                Debug.LogError("Unknown parameter");
            }
        }
    }
예제 #3
0
    public static void _bakeIntoCombined(MB2_MeshBakerCommon mom, MB_OutputOptions prefabOrSceneObject)
    {
        if (MB2_MeshCombiner.EVAL_VERSION && prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
        {
            Debug.LogError("Cannot BakeIntoPrefab with evaluation version.");
            return;
        }
        if (prefabOrSceneObject != MB_OutputOptions.bakeIntoPrefab && prefabOrSceneObject != MB_OutputOptions.bakeIntoSceneObject)
        {
            Debug.LogError("Paramater prefabOrSceneObject must be bakeIntoPrefab or bakeIntoSceneObject");
            return;
        }
        //MB2_MeshBakerCommon mom = (MB2_MeshBakerCommon) target;
        MB2_TextureBaker tb = mom.GetComponent <MB2_TextureBaker>();

        if (mom.textureBakeResults == null && tb != null)
        {
            Debug.Log("setting results");
            mom.textureBakeResults = tb.textureBakeResults;
        }

        if (mom.useObjsToMeshFromTexBaker)
        {
            if (tb != null)
            {
                mom.GetObjectsToCombine().Clear();
                mom.GetObjectsToCombine().AddRange(tb.GetObjectsToCombine());
            }
        }

        Mesh mesh;

        if (!MB2_MeshBakerRoot.doCombinedValidate(mom, MB_ObjsToCombineTypes.sceneObjOnly, new MB2_EditorMethods()))
        {
            return;
        }
        if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab &&
            mom.resultPrefab == null)
        {
            Debug.LogError("Need to set the Combined Mesh Prefab field. Create a prefab asset, drag an empty game object into it, and drag it to the 'Combined Mesh Prefab' field.");
            return;
        }
        mom.ClearMesh();
        mesh = mom.AddDeleteGameObjects(mom.GetObjectsToCombine().ToArray(), null, false, true);
        if (mesh != null)
        {
            mom.Apply(Unwrapping.GenerateSecondaryUVSet);
            Debug.Log(String.Format("Successfully baked {0} meshes into a combined mesh", mom.GetObjectsToCombine().Count));
            if (prefabOrSceneObject == MB_OutputOptions.bakeIntoSceneObject)
            {
                PrefabType pt = PrefabUtility.GetPrefabType(mom.resultSceneObject);
                if (pt == PrefabType.Prefab || pt == PrefabType.ModelPrefab)
                {
                    Debug.LogError("Combined Mesh Object is a prefab asset. If output option bakeIntoSceneObject then this must be an instance in the scene.");
                    return;
                }
            }
            else if (prefabOrSceneObject == MB_OutputOptions.bakeIntoPrefab)
            {
                string prefabPth = AssetDatabase.GetAssetPath(mom.resultPrefab);
                if (prefabPth == null || prefabPth.Length == 0)
                {
                    Debug.LogError("Could not save result to prefab. Result Prefab value is not an Asset.");
                    return;
                }
                string baseName    = Path.GetFileNameWithoutExtension(prefabPth);
                string folderPath  = prefabPth.Substring(0, prefabPth.Length - baseName.Length - 7);
                string newFilename = folderPath + baseName + "-mesh";
                SaveMeshsToAssetDatabase(mom, folderPath, newFilename);

                if (mom.renderType == MB_RenderType.skinnedMeshRenderer)
                {
                    Debug.LogWarning("Render type is skinned mesh renderer. " +
                                     "Can't create prefab until all bones have been added to the combined mesh object " + mom.resultPrefab +
                                     " Add the bones then drag the combined mesh object to the prefab.");
                }
                else
                {
                    RebuildPrefab(mom);
                }
            }
            else
            {
                Debug.LogError("Unknown parameter");
            }
        }
    }