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();
    }
Esempio n. 2
0
    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));
            }
        }
    }
Esempio n. 3
0
    public override void OnInspectorGUI()
    {
        MB3_MeshBakerGrouper tbg = (MB3_MeshBakerGrouper)target;
        MB3_TextureBaker     tb  = ((MB3_MeshBakerGrouper)target).GetComponent <MB3_TextureBaker>();

        DrawGrouperInspector();
        if (GUILayout.Button("Generate Mesh Bakers"))
        {
            if (tb == null)
            {
                Debug.LogError("There must be an MB3_TextureBaker attached to this game object.");
                return;
            }
            if (tb.GetObjectsToCombine().Count == 0)
            {
                Debug.LogError("The MB3_MeshBakerGrouper creates clusters based on the objects to combine in the MB3_TextureBaker component. There were no objects in this list.");
                return;
            }
            if (tb.transform.childCount > 0)
            {
                Debug.LogWarning("This MB3_TextureBaker had some existing child objects. You may want to delete these before 'Generating Mesh Bakers' since your source objects may be included in the List Of Objects To Combine of multiple MeshBaker objects.");
            }
            if (tb != null)
            {
                ((MB3_MeshBakerGrouper)target).grouper.DoClustering(tb);
            }
            else
            {
                Debug.LogError("MB3_MeshBakerGrouper needs to be attached to an MB3_TextureBaker");
            }
        }
        if (GUILayout.Button("Bake All Child MeshBakers"))
        {
            try
            {
                MB3_MeshBakerCommon[] mBakers = tbg.GetComponentsInChildren <MB3_MeshBakerCommon>();
                for (int i = 0; i < mBakers.Length; i++)
                {
                    //if (mBakers[i].textureBakeResults != null)
                    //{
                    //MB3_MeshBakerEditorFunctions.BakeIntoCombined(mBakers[i]);
                    MB3_MeshBakerEditorInternal.bake(mBakers[i]);
                    //} else
                    //{
                    //    Debug.LogError("Material Bake Resul was not set for baker: " + mBakers[i]);
                    //}
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
        string buttonTextEnableRenderers = "Disable Renderers On All Child MeshBaker Source Objs";
        bool   enableRenderers           = false;
        MB3_MeshBakerCommon bc           = tbg.GetComponentInChildren <MB3_MeshBakerCommon>();

        if (bc != null && bc.GetObjectsToCombine().Count > 0)
        {
            GameObject go = bc.GetObjectsToCombine()[0];
            if (go != null && go.GetComponent <Renderer>() != null && go.GetComponent <Renderer>().enabled == false)
            {
                buttonTextEnableRenderers = "Enable Renderers On All Child MeshBaker Source Objs";
                enableRenderers           = true;
            }
        }
        if (GUILayout.Button(buttonTextEnableRenderers))
        {
            try
            {
                MB3_MeshBakerCommon[] mBakers = tbg.GetComponentsInChildren <MB3_MeshBakerCommon>();
                for (int i = 0; i < mBakers.Length; i++)
                {
                    mBakers[i].EnableDisableSourceObjectRenderers(enableRenderers);
                }
            }
            catch (Exception e)
            {
                Debug.LogError(e);
            }
            finally
            {
                EditorUtility.ClearProgressBar();
            }
        }
        if (DateTime.UtcNow.Ticks - lastBoundsCheckRefreshTime > 10000000 && tb != null)
        {
            List <GameObject> gos = tb.GetObjectsToCombine();
            Bounds            b   = new Bounds(Vector3.zero, Vector3.one);
            if (gos.Count > 0 && gos[0] != null && gos[0].GetComponent <Renderer>() != null)
            {
                b = gos[0].GetComponent <Renderer>().bounds;
            }
            for (int i = 0; i < gos.Count; i++)
            {
                if (gos[i] != null && gos[i].GetComponent <Renderer>() != null)
                {
                    b.Encapsulate(gos[i].GetComponent <Renderer>().bounds);
                }
            }
            tbg.sourceObjectBounds     = b;
            lastBoundsCheckRefreshTime = DateTime.UtcNow.Ticks;
        }
        grouper.ApplyModifiedProperties();
    }