Advertisement
marcb152

Combine Meshes for Unity (Editor)

Jun 6th, 2020
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.95 KB | None | 0 0
  1. //this is the editor script of the mesh combiner, put this script in a Editor folder, the combiner will not work if this script is missing
  2. using UnityEditor;
  3. using UnityEngine;
  4.  
  5. [CustomEditor(typeof(MeshMerge))]
  6. [CanEditMultipleObjects]
  7. public class MeshMergeEditor : Editor
  8. {
  9.     SerializedProperty material;
  10.     SerializedProperty combineType;
  11.     SerializedProperty subMeshesType;
  12.     SerializedProperty use32BitsBuffer;
  13.     SerializedProperty mesh;
  14.     SerializedProperty colors;
  15.     SerializedProperty range;
  16.     SerializedProperty gridOffset;
  17.     SerializedProperty gridMaxSize;
  18.     SerializedProperty groupedItems;
  19.     SerializedProperty items;
  20.     SerializedProperty atlasTexture;
  21.     SerializedProperty path;
  22.     SerializedProperty newMat;
  23.  
  24.     bool showCombiner = false;
  25.     bool showAtlas = false;
  26.     bool showItems = false;
  27.     bool showAtlasAndCombine = false;
  28.  
  29.     void OnEnable()
  30.     {
  31.         // Setup the SerializedProperties.
  32.         material = serializedObject.FindProperty("material");
  33.         combineType = serializedObject.FindProperty("combineType");
  34.         subMeshesType = serializedObject.FindProperty("subMeshesType");
  35.         use32BitsBuffer = serializedObject.FindProperty("use32BitsBuffer");
  36.         mesh = serializedObject.FindProperty("mesh");
  37.         colors = serializedObject.FindProperty("colors");
  38.         range = serializedObject.FindProperty("range");
  39.         gridOffset = serializedObject.FindProperty("gridOffset");
  40.         gridMaxSize = serializedObject.FindProperty("gridMaxSize");
  41.         groupedItems = serializedObject.FindProperty("groupedItems");
  42.         items = serializedObject.FindProperty("items");
  43.         atlasTexture = serializedObject.FindProperty("atlasTexture");
  44.         path = serializedObject.FindProperty("path");
  45.         newMat = serializedObject.FindProperty("newMat");
  46.     }
  47.     public override void OnInspectorGUI()
  48.     {
  49.         serializedObject.Update();
  50.         MeshMerge meshMerge = (MeshMerge)target;
  51.  
  52.         GUILayout.Space(10);
  53.         showCombiner = EditorGUILayout.Foldout(showCombiner, "COMBINER SETTINGS", true, new GUIStyle(EditorStyles.foldout) { fontStyle = FontStyle.Bold });
  54.         if (showCombiner)
  55.         {
  56.             EditorGUILayout.BeginHorizontal();
  57.             EditorGUILayout.PropertyField(material, new GUIContent("Material"));
  58.             if (GUILayout.Button("Set with first mat."))
  59.                 material.objectReferenceValue = meshMerge.items[0].GetComponent<MeshRenderer>().sharedMaterial;
  60.             if (GUILayout.Button("X")) material.objectReferenceValue = null;
  61.             EditorGUILayout.EndHorizontal();
  62.  
  63.             EditorGUILayout.PropertyField(combineType, new GUIContent("Combine Type"));
  64.             EditorGUILayout.PropertyField(subMeshesType, new GUIContent("SubMeshes Type"));
  65.             EditorGUILayout.PropertyField(use32BitsBuffer, new GUIContent("Use 32 bits Buffer ?"));
  66.             if (combineType.enumValueIndex == 1)
  67.                 EditorGUILayout.PropertyField(mesh, new GUIContent("Hexagon Mesh"));
  68.             if (combineType.enumValueIndex == 0 || combineType.enumValueIndex == 1)
  69.             {
  70.                 EditorGUILayout.PropertyField(colors, new GUIContent("Colors"));
  71.                 EditorGUILayout.BeginHorizontal();
  72.                 EditorGUILayout.PropertyField(range, new GUIContent("Range"));
  73.                 if (GUILayout.Button("Calculate perfect cell size")) meshMerge.CalculatePerfectCellSize();
  74.                 EditorGUILayout.EndHorizontal();
  75.  
  76.                 EditorGUILayout.PropertyField(gridOffset, new GUIContent("Grid Offset"));
  77.                 if (GUILayout.Button("Set offset"))
  78.                 {
  79.                     gridOffset.vector3Value = new Vector3(range.intValue / 2, 0, range.intValue / 2);
  80.                     SceneView.RepaintAll();
  81.                 }
  82.                 EditorGUILayout.PropertyField(gridMaxSize, new GUIContent("Grid Max Size"));
  83.                 EditorGUILayout.PropertyField(groupedItems, new GUIContent("Items Into Cells"), true);
  84.                 if (GUILayout.Button("Group items to grid"))
  85.                 {
  86.                     meshMerge.SetGroupedList();
  87.                     meshMerge.GroupItems();
  88.                 }
  89.             }
  90.             GUILayout.Space(5);
  91.             if (GUILayout.Button("Merge meshes")) meshMerge.CombineMeshes();
  92.         }
  93.  
  94.         GUILayout.Space(10);
  95.         showAtlas = EditorGUILayout.Foldout(showAtlas, "ATLAS SETTINGS", true, new GUIStyle(EditorStyles.foldout) { fontStyle = FontStyle.Bold });
  96.         if (showAtlas)
  97.         {
  98.             EditorGUILayout.PropertyField(atlasTexture, new GUIContent("Final Atlas Texture"));
  99.             path.stringValue = EditorGUILayout.TextField("Save Path", path.stringValue);
  100.             EditorGUILayout.PropertyField(newMat, new GUIContent("Final Atlas Material"));
  101.  
  102.             EditorGUILayout.HelpBox("Warning, Atlas will not work correctly if one of the 3d objects have several submeshes/materials.\n Consider using \"Atlas & Combine\" property in this case.", MessageType.Warning);
  103.             if (GUILayout.Button("Atlas")) meshMerge.AtlasUVS();
  104.         }
  105.  
  106.         GUILayout.Space(10);
  107.         EditorGUILayout.PropertyField(items, new GUIContent("Items"), true);
  108.         EditorGUILayout.BeginHorizontal();
  109.         if (GUILayout.Button("Get children"))
  110.             for (int i = 0; i < meshMerge.transform.childCount; i++)
  111.                 meshMerge.items.Add(meshMerge.transform.GetChild(i).gameObject);
  112.         if (GUILayout.Button("Clear list")) meshMerge.items.Clear();
  113.         EditorGUILayout.EndHorizontal();
  114.  
  115.         GUILayout.Space(10);
  116.         EditorGUILayout.BeginHorizontal();
  117.         EditorGUILayout.LabelField("Atlas & Combine", EditorStyles.boldLabel);
  118.         if (GUILayout.Button("Atlas & Combine")) meshMerge.AtlasAndCombine();
  119.         EditorGUILayout.EndHorizontal();
  120.         if (GUILayout.Button("Undo all")) meshMerge.Undo();
  121.  
  122.         serializedObject.ApplyModifiedProperties();
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement