public _GameObjectAndWarning(GameObject g, string w, LightMapOption ls)
        {
            go = g;
            lightmapSetting = ls;
            Renderer r = MB_Utility.GetRenderer(g);

            material = r.sharedMaterial;
            if (material != null)
            {
                shader = material.shader;
            }
            materials = r.sharedMaterials;
            shaders   = new Shader[materials.Length];
            for (int i = 0; i < shaders.Length; i++)
            {
                if (materials[i] != null)
                {
                    shaders[i] = materials[i].shader;
                }
            }
            lightmapIndex = r.lightmapIndex;
            Mesh mesh = MB_Utility.GetMesh(g);

            numVerts = 0;
            if (mesh != null)
            {
                numVerts = mesh.vertexCount;
            }
            isStatic         = go.isStatic;
            numMaterials     = materials.Length;
            warning          = w;
            outOfBoundsUVs   = false;
            submeshesOverlap = false;
        }
    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();
    }
	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{
					MB2_TextureBaker[] texBakers = (MB2_TextureBaker[]) FindObjectsOfType(typeof(MB2_TextureBaker));
					for (int i = 0; i < texBakers.Length; i++){
						texBakers[i].CreateAtlases(updateProgressBar, true, new MB2_EditorMethods());	
					}
					MB2_MeshBaker[] mBakers = (MB2_MeshBaker[]) FindObjectsOfType(typeof(MB2_MeshBaker));
					for (int i = 0; i < mBakers.Length; i++){
						if (mBakers[i].textureBakeResults != null){
					    	MB2_MeshBakerEditorFunctions._bakeIntoCombined(mBakers[i], MB_OutputOptions.bakeIntoSceneObject);	
						}
					}					
				} 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 = (MB2_MeshBakerRoot) EditorGUILayout.ObjectField("Target to add objects to",target,typeof(MB2_MeshBakerRoot),true);
		
		if (target != null){
			targetGO = target.gameObject;
		} else {
			targetGO = null;	
		}
			
		if (targetGO != oldTargetGO){
			textureBaker = targetGO.GetComponent<MB2_TextureBaker>();
			meshBaker = targetGO.GetComponent<MB2_MeshBaker>();
			tbe = new MB2_TextureBakerEditorInternal();
			mbe = new MB2_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((MB2_TextureBaker) textureBaker, typeof(MB_MeshBakerEditorWindow));
			}
			
		}
		if (meshBaker != null){
			MB_EditorUtil.DrawSeparator();
			mbFoldout = EditorGUILayout.Foldout(mbFoldout,"Mesh Baker");
			if (mbFoldout){
				mbe.DrawGUI((MB2_MeshBaker) meshBaker, typeof(MB_MeshBakerEditorWindow));
			}
		}
		EditorGUILayout.EndScrollView();
	}
		public _GameObjectAndWarning(GameObject g, string w, LightMapOption ls){
			go = g;
			lightmapSetting = ls;
			Renderer r = MB_Utility.GetRenderer(g);
			material = r.sharedMaterial;
			if (material != null) shader = material.shader;
			materials = r.sharedMaterials;
			shaders = new Shader[materials.Length];
			for (int i = 0; i < shaders.Length; i++){
				if (materials[i] != null) shaders[i] = materials[i].shader;
			}
			lightmapIndex = r.lightmapIndex;
			Mesh mesh = MB_Utility.GetMesh(g);
			numVerts = 0;
			if (mesh != null) numVerts = mesh.vertexCount;
			isStatic = go.isStatic;
			numMaterials = materials.Length;
			warning = w;
			outOfBoundsUVs = false;
			submeshesOverlap = false;			
		}