public void BuildAllSource(MB3_TextureBaker mom) { SerializedObject tbr = new SerializedObject(mom); SerializedProperty resultMaterials = tbr.FindProperty("resultMaterials"); MB3_TextureBakerEditorInternal.ConfigureMutiMaterialsFromObjsToCombine(mom, resultMaterials, tbr); tbr.UpdateIfDirtyOrScript(); }
void drawTabAddObjectsToBakers() { if (helpBoxString == null) { helpBoxString = ""; } EditorGUILayout.HelpBox("To add, select one or more objects in the hierarchy view. Child Game Objects with MeshRender or SkinnedMeshRenderer will be added. Use the fields below to filter what is added." + "To remove, use the fields below to filter what is removed.\n" + helpBoxString, UnityEditor.MessageType.None); target = (MB3_MeshBakerRoot)EditorGUILayout.ObjectField("Target to add objects to", target, typeof(MB3_MeshBakerRoot), true); if (target != null) { targetGO = target.gameObject; } else { targetGO = null; } if (targetGO != oldTargetGO && targetGO != null) { textureBaker = targetGO.GetComponent <MB3_TextureBaker>(); meshBaker = targetGO.GetComponent <MB3_MeshBaker>(); tbe = new MB3_TextureBakerEditorInternal(); mbe = new MB3_MeshBakerEditorInternal(); oldTargetGO = targetGO; if (textureBaker != null) { serializedObject = new SerializedObject(textureBaker); tbe.OnEnable(serializedObject); } else if (meshBaker != null) { serializedObject = new SerializedObject(meshBaker); mbe.OnEnable(serializedObject); } } EditorGUIUtility.labelWidth = 300; onlyStaticObjects = EditorGUILayout.Toggle("Only Static Objects", onlyStaticObjects); onlyEnabledObjects = EditorGUILayout.Toggle("Only Enabled Objects", onlyEnabledObjects); excludeMeshesWithOBuvs = EditorGUILayout.Toggle("Exclude meshes with out-of-bounds UVs", excludeMeshesWithOBuvs); excludeMeshesAlreadyAddedToBakers = EditorGUILayout.Toggle("Exclude GameObjects already added to bakers", excludeMeshesAlreadyAddedToBakers); mat = (Material)EditorGUILayout.ObjectField("Using Material", mat, typeof(Material), true); shaderMat = (Material)EditorGUILayout.ObjectField("Using Shader", shaderMat, typeof(Material), true); string[] lightmapDisplayValues = new string[257]; int[] lightmapValues = new int[257]; lightmapValues[0] = -2; lightmapValues[1] = -1; lightmapDisplayValues[0] = "don't filter on lightmapping"; lightmapDisplayValues[1] = "not lightmapped"; for (int i = 2; i < lightmapDisplayValues.Length; i++) { lightmapDisplayValues[i] = "" + i; lightmapValues[i] = i; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Using Lightmap Index "); lightmapIndex = EditorGUILayout.IntPopup(lightmapIndex, lightmapDisplayValues, lightmapValues); EditorGUILayout.EndHorizontal(); if (regExParseError != null && regExParseError.Length > 0) { EditorGUILayout.HelpBox("Error In Regular Expression:\n" + regExParseError, MessageType.Error); } searchRegEx = EditorGUILayout.TextField(GUIContentRegExpression, searchRegEx); EditorGUILayout.Separator(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add Selected Meshes To Target")) { addSelectedObjects(); } if (GUILayout.Button("Remove Matching Meshes From Target")) { removeSelectedObjects(); } EditorGUILayout.EndHorizontal(); if (textureBaker != null) { MB_EditorUtil.DrawSeparator(); tbFoldout = EditorGUILayout.Foldout(tbFoldout, "Texture Baker"); if (tbFoldout) { tbe.DrawGUI(serializedObject, (MB3_TextureBaker)textureBaker, typeof(MB3_MeshBakerEditorWindow)); } } if (meshBaker != null) { MB_EditorUtil.DrawSeparator(); mbFoldout = EditorGUILayout.Foldout(mbFoldout, "Mesh Baker"); if (mbFoldout) { mbe.DrawGUI(serializedObject, (MB3_MeshBaker)meshBaker, typeof(MB3_MeshBakerEditorWindow)); } } }
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().Add(gaws[i].go); } } 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"); MB3_TextureBakerEditorInternal.ConfigureMutiMaterialsFromObjsToCombine(tb, resultMaterials, tbr); } else { string pthMat = AssetDatabase.GenerateUniqueAssetPath(pthRoot + newMeshBaker.name + ".asset"); MB3_TextureBakerEditorInternal.CreateCombinedMaterialAssets(tb, pthMat); } if (gaws[0].isMeshRenderer) { mb.meshCombiner.renderType = MB_RenderType.meshRenderer; } else { mb.meshCombiner.renderType = MB_RenderType.skinnedMeshRenderer; } }
void OnGUI() { scrollPos = EditorGUILayout.BeginScrollView(scrollPos, GUILayout.Width(position.width), GUILayout.Height(position.height)); EditorGUILayout.LabelField("Generate Report", EditorStyles.boldLabel); EditorGUILayout.HelpBox("List shaders in scene prints a report to the console of shaders and which objects use them. This is useful for planning which objects to combine.", UnityEditor.MessageType.None); if (GUILayout.Button("List Shaders In Scene")) { listMaterialsInScene(false); } EditorGUILayout.Separator(); MB_EditorUtil.DrawSeparator(); EditorGUILayout.HelpBox("This feature is experimental. It should be safe to use as all it does is generate game objects with Mesh and Material bakers " + "on them and assets for the combined materials.\n\n" + "Creates bakers and combined material assets in your scene based on the groupings in 'Generate Report'." + "Some configuration may still be required after bakers are generated. Groups are created for objects that " + "use the same material(s), shader(s) and lightmap. These groups should produce good results when baked.\n\n" + "This feature groups objects conservatively so bakes almost always work. This is not the only way to group objects. Objects with different shaders can also be grouped but results are" + " less preditable. Meshes with submeshes are only" + "grouped if all meshes use the same set of shaders.", UnityEditor.MessageType.None); EditorGUILayout.LabelField(autoGenerateGUIContent, EditorStyles.boldLabel); autoGenerateMeshBakers = EditorGUILayout.Foldout(autoGenerateMeshBakers, "Show Tools"); if (autoGenerateMeshBakers) { EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Select Folder For Combined Material Assets")) { generate_AssetsFolder = EditorUtility.SaveFolderPanel("Create Combined Material Assets In Folder", "", ""); generate_AssetsFolder = "Assets" + generate_AssetsFolder.Replace(Application.dataPath, "") + "/"; } EditorGUILayout.LabelField("Folder: " + generate_AssetsFolder); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Included Objects Must Be Static", GUILayout.Width(200)); generate_IncludeStaticObjects = EditorGUILayout.Toggle(GUIContent.none, generate_IncludeStaticObjects); EditorGUILayout.EndHorizontal(); generate_LightmapOption = (LightMapOption)EditorGUILayout.EnumPopup("Lightmapping", generate_LightmapOption); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Generate Mesh Bakers")) { listMaterialsInScene(true); } if (GUILayout.Button("Bake Every MeshBaker In Scene")) { try{ MB3_TextureBaker[] texBakers = (MB3_TextureBaker[])FindObjectsOfType(typeof(MB3_TextureBaker)); for (int i = 0; i < texBakers.Length; i++) { texBakers[i].CreateAtlases(updateProgressBar, true, new MB3_EditorMethods()); } MB3_MeshBaker[] mBakers = (MB3_MeshBaker[])FindObjectsOfType(typeof(MB3_MeshBaker)); for (int i = 0; i < mBakers.Length; i++) { if (mBakers[i].textureBakeResults != null) { MB3_MeshBakerEditorFunctions.BakeIntoCombined(mBakers[i]); } } } catch (Exception e) { Debug.LogError(e); }finally{ EditorUtility.ClearProgressBar(); } } EditorGUILayout.EndHorizontal(); } MB_EditorUtil.DrawSeparator(); EditorGUILayout.Separator(); EditorGUILayout.LabelField("Add Selected Meshes To Bakers", EditorStyles.boldLabel); EditorGUILayout.HelpBox("Select one or more objects in the hierarchy view. Child Game Objects with MeshRender will be added. Use the fields below to filter what is added.", UnityEditor.MessageType.None); target = (MB3_MeshBakerRoot)EditorGUILayout.ObjectField("Target to add objects to", target, typeof(MB3_MeshBakerRoot), true); if (target != null) { targetGO = target.gameObject; } else { targetGO = null; } if (targetGO != oldTargetGO) { textureBaker = targetGO.GetComponent <MB3_TextureBaker>(); meshBaker = targetGO.GetComponent <MB3_MeshBaker>(); tbe = new MB3_TextureBakerEditorInternal(); mbe = new MB3_MeshBakerEditorInternal(); oldTargetGO = targetGO; } onlyStaticObjects = EditorGUILayout.Toggle("Only Static Objects", onlyStaticObjects); onlyEnabledObjects = EditorGUILayout.Toggle("Only Enabled Objects", onlyEnabledObjects); excludeMeshesWithOBuvs = EditorGUILayout.Toggle("Exclude meshes with out-of-bounds UVs", excludeMeshesWithOBuvs); mat = (Material)EditorGUILayout.ObjectField("Using Material", mat, typeof(Material), true); shaderMat = (Material)EditorGUILayout.ObjectField("Using Shader", shaderMat, typeof(Material), true); string[] lightmapDisplayValues = new string[257]; int[] lightmapValues = new int[257]; lightmapValues[0] = -2; lightmapValues[1] = -1; lightmapDisplayValues[0] = "don't filter on lightmapping"; lightmapDisplayValues[1] = "not lightmapped"; for (int i = 2; i < lightmapDisplayValues.Length; i++) { lightmapDisplayValues[i] = "" + i; lightmapValues[i] = i; } EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Using Lightmap Index "); lightmapIndex = EditorGUILayout.IntPopup(lightmapIndex, lightmapDisplayValues, lightmapValues); EditorGUILayout.EndHorizontal(); if (GUILayout.Button("Add Selected Meshes")) { addSelectedObjects(); } /* * if (GUILayout.Button("Add LOD To Selected")){ * addLODToSelected(); * } * * if (GUILayout.Button("Remove LOD From All")){ * LODInternal[] lods = (LODInternal[]) FindObjectsOfType(typeof(LODInternal)); * for (int i = 0; i < lods.Length; i++){ * DestroyImmediate(lods[i]); * } * } */ if (textureBaker != null) { MB_EditorUtil.DrawSeparator(); tbFoldout = EditorGUILayout.Foldout(tbFoldout, "Texture Baker"); if (tbFoldout) { tbe.DrawGUI((MB3_TextureBaker)textureBaker, typeof(MB3_MeshBakerEditorWindow)); } } if (meshBaker != null) { MB_EditorUtil.DrawSeparator(); mbFoldout = EditorGUILayout.Foldout(mbFoldout, "Mesh Baker"); if (mbFoldout) { mbe.DrawGUI((MB3_MeshBaker)meshBaker, typeof(MB3_MeshBakerEditorWindow)); } } EditorGUILayout.EndScrollView(); }