public override void OnInspectorGUI()
    {
        Meshcombinervtwo myScript = (Meshcombinervtwo)target;
        GUIStyle         style    = new GUIStyle(GUI.skin.box);

        style.normal.background = MakeTex(2, 2, new Color(1, 1, 0, .6f));

        serializedObject.Update();

        EditorGUILayout.LabelField("");

        if (SeeInspector.boolValue)
        {
            DrawDefaultInspector();
        }

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("See Variables : ", GUILayout.Width(90));
        EditorGUILayout.PropertyField(SeeInspector, new GUIContent(""));
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.LabelField("");

        EditorGUILayout.HelpBox("\n" + "Combine Meshes : " +
                                "\n" +
                                "\n1 - GameObject inside this gameObject are combine." +
                                "\n" +
                                "\nAll the gameObjects with the same material are combine in a single mesh." +
                                "\n" +
                                "\nCombining Process could take time if there are a lots of gameObjects to combine." +
                                "\n" +
                                "\n2 - Choose the ScaleInLightmap then Press button Combine to start the process" +
                                "\n", MessageType.Info);


        EditorGUILayout.BeginVertical(style);
        EditorGUILayout.LabelField("");
        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Scale In Lightmap : ", GUILayout.Width(120));
        EditorGUILayout.PropertyField(f_ScaleInLightmap, new GUIContent(""));
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("Show more options : ", GUILayout.Width(120));
        EditorGUILayout.PropertyField(moreOptions, new GUIContent(""));
        EditorGUILayout.EndHorizontal();

        if (moreOptions.boolValue)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Hard Angle : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(_HardAngle, new GUIContent(""));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Pack Margin : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(_PackMargin, new GUIContent(""));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Angle Error : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(_AngleError, new GUIContent(""));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Area Error : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(_AreaError, new GUIContent(""));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Stitch Seams : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(b_StitchSeams, new GUIContent(""));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Keep Shadow Mode : ", GUILayout.Width(120));
            EditorGUILayout.PropertyField(keepShadowMode, new GUIContent(""));
            EditorGUILayout.EndHorizontal();
        }


        if (!CombineDone.boolValue)
        {
            if (GUILayout.Button("Combine"))
            {
                b_Combine = true;
            }
        }
        else
        {
            if (GUILayout.Button("Reset"))
            {
                b_Uncombine = true;
            }
        }
        EditorGUILayout.LabelField("");
        EditorGUILayout.EndVertical();
        //

        EditorGUILayout.HelpBox("3 - After the combining process gameObjects are created for each material inside this folder.", MessageType.Info);
        EditorGUILayout.HelpBox("INFO 1 : Mesh renderer are disabled for the gameObjects that have been used in the combining process." +
                                "\nINFO 2 : After combinig process, colliders stay activated.", MessageType.Info);


        EditorGUILayout.LabelField("");
        EditorGUILayout.LabelField("");
        style.normal.background = MakeTex(2, 2, new Color(.5f, .8f, 0, .3f));
        EditorGUILayout.BeginVertical(style);
        EditorGUILayout.HelpBox("(Optional) : Exclude gameObjects with specific TAG", MessageType.Info);
        EditorGUILayout.LabelField("");

        if (GUILayout.Button("Add new tag"))
        {
            b_AddTag = true;
        }
        EditorGUILayout.LabelField("");

        EditorGUILayout.LabelField("Exclude gameObjects with these Tags :");

        for (int i = 0; i < myScript.list_Tags.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();

            EditorGUILayout.PropertyField(list_Tags.GetArrayElementAtIndex(i), new GUIContent(""));
            if (GUILayout.Button("-", GUILayout.Width(20)))
            {
                b_DeleteTag = true;
                deleteNum   = i;
                break;
            }
            EditorGUILayout.EndHorizontal();
        }
        EditorGUILayout.EndVertical();


        serializedObject.ApplyModifiedProperties();

        if (b_Combine)
        {
            Undo.RegisterCompleteObjectUndo(myScript, "MeshCombiner" + myScript.gameObject.name);
            myScript.CombineDone = true;
            Component[] ChildrenMesh = myScript.GetComponentsInChildren(typeof(MeshRenderer), true);

            myScript.list_Materials.Clear();

            foreach (MeshRenderer child in ChildrenMesh)                                        // Find all the different materials
            {
                myScript.list_Materials.Add(child.sharedMaterial);
            }

            for (int i = 0; i < myScript.list_Materials.Count; i++)                                     // remove materials using multiple time
            {
                for (int k = 0; k < myScript.list_Materials.Count; k++)
                {
                    if (k != i && myScript.list_Materials[i] == myScript.list_Materials[k])
                    {
                        myScript.list_Materials[k] = null;
                    }
                }
            }

            List <Material> Tmp_list_Materials = new List <Material>();                                 // List of materials

            for (int i = 0; i < myScript.list_Materials.Count; i++)                                     // Update Materials List
            {
                if (myScript.list_Materials[i])
                {
                    Tmp_list_Materials.Add(myScript.list_Materials[i]);
                }
            }

            myScript.list_Materials.Clear();

            for (int i = 0; i < Tmp_list_Materials.Count; i++)                                  // Update Materials List
            {
                myScript.list_Materials.Add(Tmp_list_Materials[i]);
            }

            myScript.list_CreatedObjects.Clear();
            myScript.list_CombineObjects.Clear();

            Quaternion oldRot = myScript.transform.rotation;                                                            // Save the original position and rotation of obj
            Vector3    oldPos = myScript.transform.position;

            myScript.transform.rotation = Quaternion.identity;
            myScript.transform.position = new Vector3(0, 0, 0);

            for (int i = 0; i < myScript.list_Materials.Count; i++)
            {
                CombineMeshes(myScript.list_Materials[i]);
            }

            myScript.transform.rotation = oldRot;
            myScript.transform.position = oldPos;

            b_Combine = false;
        }

        if (b_Uncombine)
        {
            Undo.RegisterCompleteObjectUndo(myScript, "MeshCombiner" + myScript.gameObject.name);
            myScript.CombineDone = false;

            for (int i = 0; i < myScript.list_CombineObjects.Count; i++)
            {
                if (myScript.list_CombineObjects[i] != null)
                {
                    SerializedObject serializedObject3 = new UnityEditor.SerializedObject(myScript.list_CombineObjects[i].gameObject.GetComponents <Renderer>());
                    serializedObject3.Update();
                    SerializedProperty tmpSer2 = serializedObject3.FindProperty("m_Enabled");
                    tmpSer2.boolValue = true;
                    serializedObject3.ApplyModifiedProperties();
                }
            }

            for (int i = 0; i < myScript.list_CreatedObjects.Count; i++)
            {
                if (myScript.list_CreatedObjects[i] != null)
                {
                    Undo.DestroyObjectImmediate(myScript.list_CreatedObjects[i]);
                }
            }

            myScript.list_CreatedObjects.Clear();
            myScript.list_CombineObjects.Clear();
            b_Uncombine = false;
        }

        if (b_AddTag)
        {
            Undo.RegisterCompleteObjectUndo(myScript, "Save" + myScript.gameObject.name);
            myScript.list_Tags.Add("Your Tag");
            b_AddTag = false;
        }

        if (b_DeleteTag)
        {
            Undo.RegisterCompleteObjectUndo(myScript, "Delete" + myScript.gameObject.name);
            myScript.list_Tags.RemoveAt(deleteNum);
            b_DeleteTag = false;
        }
    }
Пример #2
0
    /*
     * public void Test(){
     *  Meshcombinervtwo myScript = (Meshcombinervtwo)target;
     *
     *  Debug.Log("Ok " + myScript.gameObject.name);
     * }
     */



    private void calculatesVertices(Meshcombinervtwo myScript)
    {
        myScript.list_MaterialsForVerticesCheck.Clear();
        Component[] ChildrenMesh = myScript.GetComponentsInChildren(typeof(MeshRenderer), true);



        foreach (MeshRenderer child in ChildrenMesh)
        {               // Find all the different materials
            myScript.list_MaterialsForVerticesCheck.Add(child.sharedMaterial);
        }

        for (int i = 0; i < myScript.list_MaterialsForVerticesCheck.Count; i++)
        {               // remove materials using multiple time
            for (int k = 0; k < myScript.list_MaterialsForVerticesCheck.Count; k++)
            {
                if (k != i && myScript.list_MaterialsForVerticesCheck[i] == myScript.list_MaterialsForVerticesCheck[k])
                {
                    myScript.list_MaterialsForVerticesCheck[k] = null;
                }
            }
        }

        List <Material> Tmp_list_Materials_2 = new List <Material>();               // List of materials


        for (int i = 0; i < myScript.list_MaterialsForVerticesCheck.Count; i++)
        {               // Update Materials List
            if (myScript.list_MaterialsForVerticesCheck[i])
            {
                Tmp_list_Materials_2.Add(myScript.list_MaterialsForVerticesCheck[i]);
            }
        }

        List <int> Tmp_list_Vertices_2 = new List <int>();               // List of materials

        for (int i = 0; i < Tmp_list_Materials_2.Count; i++)
        {
            Tmp_list_Vertices_2.Add(0);
            foreach (MeshRenderer child in ChildrenMesh)
            {
                if (child.GetComponent <Renderer>().sharedMaterial == Tmp_list_Materials_2[i] &&
                    child.gameObject.name != "Combine_" + Tmp_list_Materials_2[i].name)
                {
                    if (child.GetComponent <MeshFilter>() && child.GetComponent <MeshFilter>().sharedMesh != null)
                    {
                        Tmp_list_Vertices_2[i] += child.GetComponent <MeshFilter>().sharedMesh.vertexCount;
                    }
                }
            }
        }



        // Display list of material and the number of vertices
        for (int i = 0; i < Tmp_list_Materials_2.Count; i++)
        {               // Update Materials List
            //myScript.list_MaterialsForVerticesCheck.Add(Tmp_list_Materials[i]);
            EditorGUILayout.BeginHorizontal();
            if (GUILayout.Button("", GUILayout.Width(20)))
            {
                if (currentMatSelected.intValue == i)
                {
                    currentMatSelected.intValue = -1;
                }
                else
                {
                    currentMatSelected.intValue = i;
                }
            }

            EditorGUILayout.LabelField("Mat : " + Tmp_list_Materials_2[i].name, GUILayout.Width(120));

            EditorGUILayout.LabelField(" Vertices : " + Tmp_list_Vertices_2[i], GUILayout.Width(120));

            if (Tmp_list_Vertices_2[i] > _vertexCountMax.intValue)
            {
                EditorGUILayout.LabelField("Warning !!!", EditorStyles.boldLabel);
            }
            EditorGUILayout.EndHorizontal();


            if (currentMatSelected.intValue == i)
            {
                EditorGUILayout.BeginVertical();
                foreach (MeshRenderer child in ChildrenMesh)
                {               // Find all the different materials
                    if (child.GetComponent <Renderer>().sharedMaterial == Tmp_list_Materials_2[i] &&
                        child.gameObject.name != "Combine_" + Tmp_list_Materials_2[i].name)
                    {
                        EditorGUILayout.BeginHorizontal();
                        EditorGUILayout.LabelField("", GUILayout.Width(60));
                        if (GUILayout.Button("", GUILayout.Width(20)))
                        {
                            Selection.activeGameObject = child.gameObject;
                        }
                        EditorGUILayout.LabelField(child.name + " : " + child.GetComponent <MeshFilter>().sharedMesh.vertexCount);
                        EditorGUILayout.EndHorizontal();
                    }
                }
                EditorGUILayout.EndVertical();
            }
        }

        bool AlloWCombine = true;

        for (int i = 0; i < Tmp_list_Vertices_2.Count; i++)
        {
            if (Tmp_list_Vertices_2[i] > _vertexCountMax.intValue)
            {
                AlloWCombine = false;
                break;
            }
        }

        if (AlloWCombine)
        {
            b_CombineAvailable.boolValue = true;
        }
        else
        {
            b_CombineAvailable.boolValue = false;
        }
    }