summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/MeshBaker/Editor/MB3_MeshBakerEditor.cs
blob: 136afc566e73beaaf2ae2e226b2cd08a0db217bb (plain)
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
//----------------------------------------------
//            MeshBaker
// Copyright © 2011-2012 Ian Deane
//----------------------------------------------
using UnityEngine;
using System.Collections;
using System.IO;
using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Text.RegularExpressions;

using DigitalOpus.MB.Core;
using UnityEditor;

namespace DigitalOpus.MB.MBEditor
{
    [CustomEditor(typeof(MB3_MeshBaker))]
    [CanEditMultipleObjects]
    public class MB3_MeshBakerEditor : Editor
    {
        MB3_MeshBakerEditorInternal mbe = new MB3_MeshBakerEditorInternal();

        [MenuItem("GameObject/Create Other/Mesh Baker/TextureBaker and MeshBaker", false, 100)]
        public static GameObject CreateNewMeshBaker()
        {
            MB3_TextureBaker[] mbs = (MB3_TextureBaker[])GameObject.FindObjectsOfType(typeof(MB3_TextureBaker));
            Regex regex = new Regex(@"\((\d+)\)$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
            int largest = 0;
            try
            {
                for (int i = 0; i < mbs.Length; i++)
                {
                    Match match = regex.Match(mbs[i].name);
                    if (match.Success)
                    {
                        int val = Convert.ToInt32(match.Groups[1].Value);
                        if (val >= largest)
                            largest = val + 1;
                    }
                }
            }
            catch (Exception e)
            {
                if (e == null) e = null; //Do nothing supress compiler warning
            }
            GameObject nmb = new GameObject("TextureBaker (" + largest + ")");
            nmb.transform.position = Vector3.zero;
            MB3_TextureBaker tb = nmb.AddComponent<MB3_TextureBaker>();
            tb.packingAlgorithm = MB2_PackingAlgorithmEnum.MeshBakerTexturePacker;
            MB3_MeshBakerGrouper mbg = nmb.AddComponent<MB3_MeshBakerGrouper>();
            GameObject meshBaker = new GameObject("MeshBaker");
            MB3_MeshBaker mb = meshBaker.AddComponent<MB3_MeshBaker>();
            meshBaker.transform.parent = nmb.transform;
            mb.meshCombiner.settingsHolder = mbg;
            return nmb.gameObject;
        }

        [MenuItem("GameObject/Create Other/Mesh Baker/MeshBaker", false, 100)]
        public static GameObject CreateNewMeshBakerOnly()
        {
            MB3_MeshBaker[] mbs = (MB3_MeshBaker[])GameObject.FindObjectsOfType(typeof(MB3_MeshBaker));
            Regex regex = new Regex(@"\((\d+)\)$", RegexOptions.Compiled | RegexOptions.CultureInvariant);
            int largest = 0;
            try
            {
                for (int i = 0; i < mbs.Length; i++)
                {
                    Match match = regex.Match(mbs[i].name);
                    if (match.Success)
                    {
                        int val = Convert.ToInt32(match.Groups[1].Value);
                        if (val >= largest)
                            largest = val + 1;
                    }
                }
            }
            catch (Exception e)
            {
                if (e == null) e = null; //Do nothing supress compiler warning
            }
            GameObject meshBaker = new GameObject("MeshBaker (" + largest + ")");
            meshBaker.AddComponent<MB3_MeshBaker>();
            return meshBaker.gameObject;
        }

        void OnEnable()
        {
            mbe.OnEnable(serializedObject);
        }

        void OnDisable()
        {
            mbe.OnDisable();
        }

        public override void OnInspectorGUI()
        {
            mbe.OnInspectorGUI(serializedObject, (MB3_MeshBakerCommon)target, targets, typeof(MB3_MeshBakerEditorWindow));
        }


    }
}