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