forked from atteneder/glTFast
-
Notifications
You must be signed in to change notification settings - Fork 19
/
MenuEntries.cs
207 lines (180 loc) · 6.51 KB
/
MenuEntries.cs
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// SPDX-FileCopyrightText: 2023 Unity Technologies and the glTFast authors
// SPDX-License-Identifier: Apache-2.0
using System.IO;
using System.Linq;
using GLTFast.Export;
using GLTFast.Utils;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
#if GLTF_VALIDATOR
using Unity.glTF.Validator;
#endif
namespace GLTFast.Editor
{
using Logging;
static class MenuEntries
{
const string k_GltfExtension = "gltf";
const string k_GltfBinaryExtension = "glb";
static string SaveFolderPath
{
get
{
var saveFolderPath = EditorUserSettings.GetConfigValue("glTF.saveFilePath");
if (string.IsNullOrEmpty(saveFolderPath))
{
saveFolderPath = Application.streamingAssetsPath;
}
return saveFolderPath;
}
set => EditorUserSettings.SetConfigValue("glTF.saveFilePath", value);
}
[MenuItem("Assets/Export glTF/glTF (.gltf)", true)]
static bool ExportSelectionValidate()
{
return TryGetExportNameAndGameObjects(out _, out _);
}
[MenuItem("Assets/Export glTF/glTF (.gltf)", false, 31)]
static void ExportSelectionMenu()
{
ExportSelection(false);
}
[MenuItem("Assets/Export glTF/glTF-Binary (.glb)", true)]
static bool ExportSelectionBinaryValidate()
{
return TryGetExportNameAndGameObjects(out _, out _);
}
[MenuItem("Assets/Export glTF/glTF-Binary (.glb)", false, 32)]
static void ExportSelectionBinaryMenu()
{
ExportSelection(true);
}
[MenuItem("GameObject/Export glTF/glTF (.gltf)", true)]
static bool ExportGameObjectValidate()
{
return TryGetExportNameAndGameObjects(out _, out _);
}
[MenuItem("GameObject/Export glTF/glTF (.gltf)", false, 32)]
static void ExportGameObjectMenu(MenuCommand command)
{
ExportGameObject(command, false);
}
[MenuItem("GameObject/Export glTF/glTF-Binary (.glb)", true)]
static bool ExportGameObjectBinaryValidate()
{
return TryGetExportNameAndGameObjects(out _, out _);
}
[MenuItem("GameObject/Export glTF/glTF-Binary (.glb)", false, 31)]
static void ExportGameObjectBinaryMenu(MenuCommand command)
{
ExportGameObject(command, true);
}
static void ExportGameObject(MenuCommand command, bool binary)
{
var go = command.context as GameObject;
if (go != null)
{
Export(binary, go.name, new[] { go });
}
else if (TryGetExportNameAndGameObjects(out var name, out var gameObjects))
{
Export(binary, name, gameObjects);
}
}
static void ExportSelection(bool binary)
{
if (TryGetExportNameAndGameObjects(out var name, out var gameObjects))
{
Export(binary, name, gameObjects);
}
else
{
Debug.LogError("Can't export glTF: selection is empty");
}
}
static void Export(bool binary, string name, GameObject[] gameObjects)
{
var extension = binary ? k_GltfBinaryExtension : k_GltfExtension;
var path = EditorUtility.SaveFilePanel(
"glTF Export Path",
SaveFolderPath,
$"{name}.{extension}",
extension
);
if (!string.IsNullOrEmpty(path))
{
SaveFolderPath = Directory.GetParent(path)?.FullName;
var settings = GetDefaultSettings(binary);
var goSettings = new GameObjectExportSettings { OnlyActiveInHierarchy = false };
var export = new GameObjectExport(settings, gameObjectExportSettings: goSettings, logger: new ConsoleLogger());
export.AddScene(gameObjects, name);
var success = AsyncHelpers.RunSync(() => export.SaveToFileAndDispose(path));
#if GLTF_VALIDATOR
if (success)
{
var report = Validator.Validate(path);
report.Log();
}
#endif
}
}
static ExportSettings GetDefaultSettings(bool binary)
{
var settings = new ExportSettings
{
Format = binary ? GltfFormat.Binary : GltfFormat.Json
};
return settings;
}
[MenuItem("File/Export Scene/glTF (.gltf)", false, 173)]
static void ExportSceneMenu()
{
ExportScene(false);
}
[MenuItem("File/Export Scene/glTF-Binary (.glb)", false, 174)]
static void ExportSceneBinaryMenu()
{
ExportScene(true);
}
static void ExportScene(bool binary)
{
var scene = SceneManager.GetActiveScene();
var gameObjects = scene.GetRootGameObjects();
var extension = binary ? k_GltfBinaryExtension : k_GltfExtension;
var path = EditorUtility.SaveFilePanel(
"glTF Export Path",
SaveFolderPath,
$"{scene.name}.{extension}",
extension
);
if (!string.IsNullOrEmpty(path))
{
SaveFolderPath = Directory.GetParent(path)?.FullName;
var settings = GetDefaultSettings(binary);
var export = new GameObjectExport(settings, logger: new ConsoleLogger());
export.AddScene(gameObjects, scene.name);
AsyncHelpers.RunSync(() => export.SaveToFileAndDispose(path));
#if GLTF_VALIDATOR
var report = Validator.Validate(path);
report.Log();
#endif
}
}
static bool TryGetExportNameAndGameObjects(out string name, out GameObject[] gameObjects)
{
var transforms = Selection.GetTransforms(SelectionMode.Assets | SelectionMode.TopLevel);
if (transforms.Length > 0)
{
name = transforms.Length > 1
? SceneManager.GetActiveScene().name
: Selection.activeObject.name;
gameObjects = transforms.Select(x => x.gameObject).ToArray();
return true;
}
name = null;
gameObjects = null;
return false;
}
}
}