You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.9 KiB

5 years ago
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. public class SimpleMeshCombineMaster : MonoBehaviour {
  6. public bool generateLightmapUV;
  7. }
  8. #if UNITY_EDITOR
  9. [CustomEditor(typeof(SimpleMeshCombineMaster))]
  10. public class SimpleMeshCombineMasterEditor : Editor {
  11. SimpleMeshCombineMaster masterTarget;
  12. void AttachCombineScriptToChildren() {
  13. for (int i = 0; i < masterTarget.transform.childCount; i++) {
  14. Transform t = masterTarget.transform.GetChild(i);
  15. SimpleMeshCombine smc = t.GetComponent<SimpleMeshCombine>();
  16. if (!smc) {
  17. t.gameObject.AddComponent<SimpleMeshCombine>();
  18. }
  19. }
  20. }
  21. void CombineAll(bool combine, SimpleMeshCombine[] arr) {
  22. for (int i = 0; i < arr.Length; i++) {
  23. SimpleMeshCombine smc = arr[i];
  24. if (combine && !smc.combined) {
  25. if (masterTarget.generateLightmapUV) smc.generateLightmapUV = true;
  26. smc.CombineMeshes();
  27. } else if (!combine && smc.combined) {
  28. smc.EnableRenderers(true);
  29. if (smc.combined != null) DestroyImmediate(smc.combined);
  30. smc.combinedGameOjects = null;
  31. }
  32. }
  33. }
  34. void OnEnable() {
  35. masterTarget = target as SimpleMeshCombineMaster;
  36. }
  37. public override void OnInspectorGUI() {
  38. if (GUILayout.Button("Attach SMC to Children")) AttachCombineScriptToChildren();
  39. GUILayout.Space(25);
  40. masterTarget.generateLightmapUV = EditorGUILayout.Toggle("Create Lightmap UV", masterTarget.generateLightmapUV);
  41. if (GUILayout.Button("Combine All Children"))
  42. CombineAll(true, masterTarget.transform.GetComponentsInChildren<SimpleMeshCombine>());
  43. if (GUILayout.Button("Release All Children"))
  44. CombineAll(false, masterTarget.transform.GetComponentsInChildren<SimpleMeshCombine>());
  45. GUILayout.Space(25);
  46. if (GUILayout.Button("Combine All in Scene"))
  47. CombineAll(true, FindObjectsOfType<SimpleMeshCombine>());
  48. if (GUILayout.Button("Release All in Scene"))
  49. CombineAll(false, FindObjectsOfType<SimpleMeshCombine>());
  50. }
  51. }
  52. #endif