public static void DrawMultipleMaterialsMappings(MB3_TextureBaker momm, SerializedObject textureBaker, MB3_TextureBakerEditorInternal tbEditor)
        {
            EditorGUILayout.BeginVertical(tbEditor.editorStyles.multipleMaterialBackgroundStyle);
            EditorGUILayout.LabelField("Source Material To Combined Mapping", EditorStyles.boldLabel);

            float oldLabelWidth = EditorGUIUtility.labelWidth;

            EditorGUIUtility.labelWidth = 300;
            EditorGUILayout.PropertyField(tbEditor.doMultiMaterialIfOBUVs, MB3_TextureBakerEditorInternal.gc_DoMultiMaterialSplitAtlasesIfOBUVs);
            EditorGUILayout.PropertyField(tbEditor.doMultiMaterialSplitAtlasesIfTooBig, MB3_TextureBakerEditorInternal.gc_DoMultiMaterialSplitAtlasesIfTooBig);
            EditorGUIUtility.labelWidth = oldLabelWidth;


            if (GUILayout.Button(MB3_TextureBakerEditorInternal.configAtlasMultiMatsFromObjsContent))
            {
                MB_TextureBakerEditorConfigureMultiMaterials.ConfigureMutiMaterialsFromObjsToCombine(momm, tbEditor.resultMaterials, textureBaker);
            }

            EditorGUILayout.BeginHorizontal();
            tbEditor.resultMaterialsFoldout = EditorGUILayout.Foldout(tbEditor.resultMaterialsFoldout, MB3_TextureBakerEditorInternal.combinedMaterialsGUIContent);

            if (GUILayout.Button(MB3_TextureBakerEditorInternal.insertContent, EditorStyles.miniButtonLeft, MB3_TextureBakerEditorInternal.buttonWidth))
            {
                if (tbEditor.resultMaterials.arraySize == 0)
                {
                    momm.resultMaterials    = new MB_MultiMaterial[1];
                    momm.resultMaterials[0] = new MB_MultiMaterial();
                    momm.resultMaterials[0].considerMeshUVs = momm.fixOutOfBoundsUVs;
                }
                else
                {
                    int idx = tbEditor.resultMaterials.arraySize - 1;
                    tbEditor.resultMaterials.InsertArrayElementAtIndex(idx);
                    tbEditor.resultMaterials.GetArrayElementAtIndex(idx + 1).FindPropertyRelative("considerMeshUVs").boolValue = momm.fixOutOfBoundsUVs;
                }
            }
            if (GUILayout.Button(MB3_TextureBakerEditorInternal.deleteContent, EditorStyles.miniButtonRight, MB3_TextureBakerEditorInternal.buttonWidth))
            {
                tbEditor.resultMaterials.DeleteArrayElementAtIndex(tbEditor.resultMaterials.arraySize - 1);
            }
            EditorGUILayout.EndHorizontal();
            if (tbEditor.resultMaterialsFoldout)
            {
                for (int i = 0; i < tbEditor.resultMaterials.arraySize; i++)
                {
                    EditorGUILayout.Separator();
                    if (i % 2 == 1)
                    {
                        EditorGUILayout.BeginVertical(tbEditor.editorStyles.multipleMaterialBackgroundStyle);
                    }
                    else
                    {
                        EditorGUILayout.BeginVertical(tbEditor.editorStyles.multipleMaterialBackgroundStyleDarker);
                    }
                    string s = "";
                    if (i < momm.resultMaterials.Length && momm.resultMaterials[i] != null && momm.resultMaterials[i].combinedMaterial != null)
                    {
                        s = momm.resultMaterials[i].combinedMaterial.shader.ToString();
                    }
                    EditorGUILayout.BeginHorizontal();
                    EditorGUILayout.LabelField("---------- submesh:" + i + " " + s, EditorStyles.boldLabel);
                    if (GUILayout.Button(MB3_TextureBakerEditorInternal.deleteContent, EditorStyles.miniButtonRight, MB3_TextureBakerEditorInternal.buttonWidth))
                    {
                        tbEditor.resultMaterials.DeleteArrayElementAtIndex(i);
                    }
                    EditorGUILayout.EndHorizontal();
                    if (i < tbEditor.resultMaterials.arraySize)
                    {
                        EditorGUILayout.Separator();
                        SerializedProperty resMat = tbEditor.resultMaterials.GetArrayElementAtIndex(i);
                        EditorGUILayout.PropertyField(resMat.FindPropertyRelative("combinedMaterial"));
                        EditorGUILayout.PropertyField(resMat.FindPropertyRelative("considerMeshUVs"));
                        SerializedProperty sourceMats = resMat.FindPropertyRelative("sourceMaterials");
                        EditorGUILayout.PropertyField(sourceMats, true);
                    }
                    EditorGUILayout.EndVertical();
                }
            }
            EditorGUILayout.EndVertical();
        }
        public void DrawGUI(SerializedObject textureBaker, MB3_TextureBaker momm, UnityEngine.Object[] targets, System.Type editorWindow)
        {
            if (textureBaker == null)
            {
                return;
            }
            textureBaker.Update();


            showInstructions = EditorGUILayout.Foldout(showInstructions, "Instructions:");
            if (showInstructions)
            {
                EditorGUILayout.HelpBox("1. Add scene objects or prefabs to combine. For best results these should use the same shader as result material.\n\n" +
                                        "2. Create Empty Assets For Combined Material(s)\n\n" +
                                        "3. Check that shader on result material(s) are correct.\n\n" +
                                        "4. Bake materials into combined material(s).\n\n" +
                                        "5. Look at warnings/errors in console. Decide if action needs to be taken.\n\n" +
                                        "6. You are now ready to build combined meshs or adjust meshes to use the combined material(s).", UnityEditor.MessageType.None);
            }

            EditorGUILayout.PropertyField(logLevel, logLevelContent);
            EditorGUILayout.Separator();

            // Selected objects
            EditorGUILayout.BeginVertical(editorStyles.editorBoxBackgroundStyle);
            EditorGUILayout.LabelField("Objects To Be Combined", EditorStyles.boldLabel);
            if (GUILayout.Button(openToolsWindowLabelContent))
            {
                MB3_MeshBakerEditorWindow mmWin = (MB3_MeshBakerEditorWindow)EditorWindow.GetWindow(editorWindow);
                mmWin.SetTarget((MB3_MeshBakerRoot)momm);
            }

            object[] objs = MB3_EditorMethods.DropZone("Drag & Drop Renderers Or Parents Here To Add Objects To Be Combined", 300, 50);
            MB3_EditorMethods.AddDroppedObjects(objs, momm);

            EditorGUILayout.PropertyField(objsToMesh, objectsToCombineGUIContent, true);
            EditorGUILayout.Separator();
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("Select Objects In Scene"))
            {
                List <MB3_TextureBaker> selectedBakers = _getBakersFromTargets(targets);
                List <GameObject>       obsToCombine   = new List <GameObject>();

                foreach (MB3_TextureBaker baker in selectedBakers)
                {
                    obsToCombine.AddRange(baker.GetObjectsToCombine());
                }
                Selection.objects = obsToCombine.ToArray();
                if (momm.GetObjectsToCombine().Count > 0)
                {
                    SceneView.lastActiveSceneView.pivot = momm.GetObjectsToCombine()[0].transform.position;
                }
            }
            if (GUILayout.Button(gc_SortAlongAxis))
            {
                MB3_MeshBakerRoot.ZSortObjects sorter = new MB3_MeshBakerRoot.ZSortObjects();
                sorter.sortAxis = sortOrderAxis.vector3Value;
                sorter.SortByDistanceAlongAxis(momm.GetObjectsToCombine());
            }
            EditorGUILayout.PropertyField(sortOrderAxis, GUIContent.none);
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.EndVertical();


            // output
            EditorGUILayout.Separator();
            EditorGUILayout.LabelField("Output", EditorStyles.boldLabel);

            EditorGUILayout.PropertyField(resultType);

            if (GUILayout.Button(createPrefabAndMaterialLabelContent))
            {
                List <MB3_TextureBaker> selectedBakers = _getBakersFromTargets(targets);
                string newPrefabPath = EditorUtility.SaveFilePanelInProject("Asset name", "", "asset", "Enter a name for the baked texture results");
                if (newPrefabPath != null)
                {
                    for (int i = 0; i < selectedBakers.Count; i++)
                    {
                        CreateCombinedMaterialAssets(selectedBakers[i], newPrefabPath, i == 0 ? true : false);
                    }
                }
            }

            EditorGUILayout.PropertyField(textureBakeResults, textureBakeResultsGUIContent);
            if (textureBakeResults.objectReferenceValue != null)
            {
                showContainsReport = EditorGUILayout.Foldout(showContainsReport, "Shaders & Materials Contained");
                if (showContainsReport)
                {
                    EditorGUILayout.HelpBox(((MB2_TextureBakeResults)textureBakeResults.objectReferenceValue).GetDescription(), MessageType.Info);
                }
            }

            if (resultType.enumValueIndex == (int)MB2_TextureBakeResults.ResultType.textureArray)
            {
                MB_TextureBakerConfigureTextureArrays.DrawTextureArrayConfiguration(momm, textureBaker, this);
            }
            else
            {
                EditorGUILayout.PropertyField(doMultiMaterial, new GUIContent("Multiple Combined Materials"));

                if (momm.doMultiMaterial)
                {
                    MB_TextureBakerEditorConfigureMultiMaterials.DrawMultipleMaterialsMappings(momm, textureBaker, this);
                }
                else
                {
                    EditorGUILayout.PropertyField(resultMaterial, new GUIContent("Combined Mesh Material"));
                }
            }

            // settings
            int labelWidth = 200;

            EditorGUILayout.Separator();
            EditorGUILayout.BeginVertical(editorStyles.editorBoxBackgroundStyle);
            EditorGUILayout.LabelField("Material Bake Options", EditorStyles.boldLabel);

            DrawPropertyFieldWithLabelWidth(atlasPadding, gc_atlasPadding, labelWidth);
            DrawPropertyFieldWithLabelWidth(maxAtlasSize, maxAtlasSizeGUIContent, labelWidth);
            DrawPropertyFieldWithLabelWidth(resizePowerOfTwoTextures, resizePowerOfTwoGUIContent, labelWidth);
            DrawPropertyFieldWithLabelWidth(maxTilingBakeSize, maxTilingBakeSizeGUIContent, labelWidth);
            EditorGUI.BeginDisabledGroup(momm.doMultiMaterial);
            DrawPropertyFieldWithLabelWidth(considerMeshUVs, fixOutOfBoundsGUIContent, labelWidth);
            EditorGUI.EndDisabledGroup();
            if (texturePackingAlgorithm.intValue == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePacker ||
                texturePackingAlgorithm.intValue == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePacker_Fast ||
                texturePackingAlgorithm.intValue == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePaker_Fast_V2_Beta)
            {
                DrawPropertyFieldWithLabelWidth(forcePowerOfTwoAtlas, forcePowerOfTwoAtlasContent, labelWidth);
            }
            DrawPropertyFieldWithLabelWidth(considerNonTextureProperties, considerNonTexturePropertiesContent, labelWidth);
            if (texturePackingAlgorithm.intValue == (int)MB2_PackingAlgorithmEnum.UnitysPackTextures)
            {
                EditorGUILayout.HelpBox("Unity's texture packer has memory problems and frequently crashes the editor.", MessageType.Warning);
            }
            EditorGUILayout.PropertyField(texturePackingAlgorithm, texturePackingAgorithmGUIContent);
            if (MB3_TextureCombinerPipeline.USE_EXPERIMENTAL_HOIZONTALVERTICAL)
            {
                if (texturePackingAlgorithm.enumValueIndex == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePacker_Horizontal)
                {
                    EditorGUILayout.PropertyField(useMaxAtlasWidthOverride, gc_useMaxAtlasWidthOverride);
                    if (!useMaxAtlasWidthOverride.boolValue)
                    {
                        EditorGUI.BeginDisabledGroup(true);
                    }
                    EditorGUILayout.PropertyField(maxAtlasWidthOverride, gc_overrideMaxAtlasWidth);
                    if (!useMaxAtlasWidthOverride.boolValue)
                    {
                        EditorGUI.EndDisabledGroup();
                    }
                }
                else if (texturePackingAlgorithm.enumValueIndex == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePacker_Vertical)
                {
                    EditorGUILayout.PropertyField(useMaxAtlasHeightOverride, gc_useMaxAtlasHeightOverride);
                    if (!useMaxAtlasHeightOverride.boolValue)
                    {
                        EditorGUI.BeginDisabledGroup(true);
                    }
                    EditorGUILayout.PropertyField(maxAtlasHeightOverride, gc_overrideMaxAtlasHeight);
                    if (!useMaxAtlasHeightOverride.boolValue)
                    {
                        EditorGUI.EndDisabledGroup();
                    }
                }
            }

            if (texturePackingAlgorithm.intValue == (int)MB2_PackingAlgorithmEnum.MeshBakerTexturePaker_Fast_V2_Beta)
            {
                // layer field
                int  newValueLayerTexturePackerFastMesh = EditorGUILayout.LayerField(layerTexturePackerFastMeshGUIContent, layerTexturePackerFastMesh.intValue);
                bool isNewValue = newValueLayerTexturePackerFastMesh == layerTexturePackerFastMesh.intValue;
                layerTexturePackerFastMesh.intValue = newValueLayerTexturePackerFastMesh;
                if (isNewValue)
                {
                    Renderer[] rs = GameObject.FindObjectsOfType <Renderer>();
                    int        numRenderersOnLayer = 0;
                    for (int i = 0; i < rs.Length; i++)
                    {
                        if (rs[i].gameObject.layer == newValueLayerTexturePackerFastMesh)
                        {
                            numRenderersOnLayer++;
                        }
                    }

                    string layerName = LayerMask.LayerToName(layerTexturePackerFastMesh.intValue);
                    if (layerName != null && layerName.Length > 0 && numRenderersOnLayer == 0)
                    {
                        layerTexturePackerFastMeshMessage = null;
                    }
                    else
                    {
                        layerTexturePackerFastMeshMessage = layerTexturePackerFastMeshGUIContent.tooltip;
                    }
                }

                string scriptDefinesErrMessage = ValidatePlayerSettingsDefineSymbols();

                if (layerTexturePackerFastMesh.intValue == -1 ||
                    (layerTexturePackerFastMeshMessage != null && layerTexturePackerFastMeshMessage.Length > 0) ||
                    (scriptDefinesErrMessage != null))
                {
                    EditorGUILayout.HelpBox(layerTexturePackerFastMeshMessage + "\n\n" + scriptDefinesErrMessage, MessageType.Error);
                }
            }

            EditorGUILayout.PropertyField(customShaderProperties, customShaderPropertyNamesGUIContent, true);
            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
            Color oldColor = GUI.backgroundColor;

            GUI.color = buttonColor;
            if (GUILayout.Button("Bake Materials Into Combined Material"))
            {
                List <MB3_TextureBaker> selectedBakers = _getBakersFromTargets(targets);
                foreach (MB3_TextureBaker tb in selectedBakers)
                {
                    tb.CreateAtlases(updateProgressBar, true, new MB3_EditorMethods());
                    EditorUtility.ClearProgressBar();
                    if (tb.textureBakeResults != null)
                    {
                        EditorUtility.SetDirty(momm.textureBakeResults);
                    }
                }
            }
            GUI.backgroundColor = oldColor;
            textureBaker.ApplyModifiedProperties();
            if (GUI.changed)
            {
                textureBaker.SetIsDifferentCacheDirty();
            }
        }
        void createAndSetupBaker(List <GameObjectFilterInfo> gaws, string pthRoot)
        {
            for (int i = gaws.Count - 1; i >= 0; i--)
            {
                if (gaws[i].go == null)
                {
                    gaws.RemoveAt(i);
                }
            }
            if (gaws.Count < 1)
            {
                Debug.LogError("No game objects.");
                return;
            }

            if (pthRoot == null || pthRoot == "")
            {
                Debug.LogError("Folder for saving created assets was not set.");
                return;
            }

            int numVerts = 0;

            for (int i = 0; i < gaws.Count; i++)
            {
                if (gaws[i].go != null)
                {
                    numVerts = gaws[i].numVerts;
                }
            }

            GameObject newMeshBaker = null;

            if (numVerts >= 65535)
            {
                newMeshBaker = MB3_MultiMeshBakerEditor.CreateNewMeshBaker();
            }
            else
            {
                newMeshBaker = MB3_MeshBakerEditor.CreateNewMeshBaker();
            }

            newMeshBaker.name = ("MeshBaker-" + gaws[0].shaderName + "-LM" + gaws[0].lightmapIndex).ToString().Replace("/", "-");

            MB3_TextureBaker    tb = newMeshBaker.GetComponent <MB3_TextureBaker>();
            MB3_MeshBakerCommon mb = tb.GetComponentInChildren <MB3_MeshBakerCommon>();

            tb.GetObjectsToCombine().Clear();
            for (int i = 0; i < gaws.Count; i++)
            {
                if (gaws[i].go != null && !tb.GetObjectsToCombine().Contains(gaws[i].go))
                {
                    tb.GetObjectsToCombine().Add(gaws[i].go);
                }
            }

            if (splitAtlasesSoMeshesFit)
            {
                tb.maxAtlasSize = atlasSize;
            }
            if (gaws[0].numMaterials > 1)
            {
                string pthMat = AssetDatabase.GenerateUniqueAssetPath(pthRoot + newMeshBaker.name + ".asset");
                MB3_TextureBakerEditorInternal.CreateCombinedMaterialAssets(tb, pthMat);
                tb.doMultiMaterial = true;
                SerializedObject   tbr             = new SerializedObject(tb);
                SerializedProperty resultMaterials = tbr.FindProperty("resultMaterials");
                MB_TextureBakerEditorConfigureMultiMaterials.ConfigureMutiMaterialsFromObjsToCombine2(tb, resultMaterials, tbr);
            }
            else
            {
                string pthMat = AssetDatabase.GenerateUniqueAssetPath(pthRoot + newMeshBaker.name + ".asset");
                MB3_TextureBakerEditorInternal.CreateCombinedMaterialAssets(tb, pthMat);
            }
            if (gaws[0].isMeshRenderer)
            {
                mb.meshCombiner.settings.renderType = MB_RenderType.meshRenderer;
            }
            else
            {
                mb.meshCombiner.settings.renderType = MB_RenderType.skinnedMeshRenderer;
            }
        }