using System; using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEditor; using UnityEngine; [CustomEditor(typeof(CombineMeshs))] public class CombineMeshsEditor : Editor { CombineMeshs inst; private void OnEnable() { inst = (CombineMeshs)target; } public override void OnInspectorGUI() { base.OnInspectorGUI(); // if(GUILayout.Button("CombineMeshs", GUILayout.Height(50))) { if(!inst.isCombineSubmesh) { AAA(); } else { AAB(); } } } void AAA() { List< CombineInstance > combineInstances = new List< CombineInstance >(); List materials = new List< Material >(); foreach(Transform t in inst.root.GetComponent()) { if(t.gameObject.activeSelf && t.GetComponent()) { CombineInstance combineInstance = new CombineInstance(); combineInstance.mesh = t.GetComponent().sharedMesh; combineInstance.transform = inst.root.worldToLocalMatrix * t.localToWorldMatrix; combineInstances.Add(combineInstance); materials.AddRange(t.GetComponent().sharedMaterials); t.gameObject.SetActive(false); } } Mesh mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; mesh.CombineMeshes(combineInstances.ToArray(), false); GameObject obj = new GameObject(); obj.transform.position = inst.root.transform.position; obj.transform.SetParent(inst.root.transform); obj.AddComponent(); obj.AddComponent(); obj.GetComponent().sharedMesh = mesh; obj.GetComponent().sharedMaterials = materials.ToArray(); } void AAB() { List combineInstances = new List(); List materials = new List(); foreach (Transform t in inst.root.GetComponentsInChildren()) { if (t.gameObject.activeSelf && t.GetComponent() && t.GetComponent().sharedMesh) { CombineInstance combineInstance = new CombineInstance(); for (int i = 0; i < t.GetComponent().sharedMesh.subMeshCount; ++i) { combineInstance.mesh = t.GetComponent().sharedMesh; combineInstance.transform = inst.root.worldToLocalMatrix * t.localToWorldMatrix; combineInstance.subMeshIndex = i; combineInstances.Add(combineInstance); } // for (int i = 0; i < t.GetComponent().sharedMaterials.Length; ++i) { if (!materials.Contains(t.GetComponent().sharedMaterials[i])) { materials.Add(t.GetComponent().sharedMaterials[i]); } } t.gameObject.SetActive(false); } } Mesh mesh = new Mesh(); mesh.indexFormat = UnityEngine.Rendering.IndexFormat.UInt32; mesh.CombineMeshes(combineInstances.ToArray(), true); GameObject obj = new GameObject(); obj.transform.position = inst.root.transform.position; obj.transform.SetParent(inst.root.transform); obj.AddComponent(); obj.AddComponent(); obj.GetComponent().sharedMesh = mesh; obj.GetComponent().sharedMaterials = materials.ToArray(); Debug.Log("Done!"); } }