-
Notifications
You must be signed in to change notification settings - Fork 1
/
Exporter.cs
327 lines (308 loc) · 12.8 KB
/
Exporter.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using AssetStudio;
namespace AssetStudioCmd
{
internal static class Exporter
{
public static bool ExportTexture2D(AssetItem item, string exportPath, string format)
{
var converter = new Texture2DConverter((Texture2D)item.Asset);
ImageFormat dstFormat = ImageFormat.Png;
if (format == "recommanded" || format == "png")
{
format = "png";
dstFormat = ImageFormat.Png;
}
var oriPath = item.Asset.assetsFile.originalPath;
var exportFullName = exportPath + Path.GetFileNameWithoutExtension(oriPath) + "." + format;
if (format != "original")
{
var bitmap = converter.ConvertToBitmap(true);
if (bitmap == null)
return false;
bitmap.Save(exportFullName, dstFormat);
bitmap.Dispose();
return true;
}
else
{
File.WriteAllBytes(exportFullName, converter.ConvertToContainer());
return true;
}
}
public static bool ExportAudioClip(AssetItem item, string exportPath, string format)
{
var m_AudioClip = (AudioClip)item.Asset;
var m_AudioData = m_AudioClip.m_AudioData.Value;
if (m_AudioData == null || m_AudioData.Length == 0)
return false;
var converter = new AudioClipConverter(m_AudioClip);
if (format == "recommanded" || format == "wav")
{
format = "wav";
}
var oriPath = item.Asset.assetsFile.originalPath;
var exportFullName = exportPath + Path.GetFileNameWithoutExtension(oriPath) + "." + format;
if (format == "wav" && converter.IsFMODSupport)
{
var buffer = converter.ConvertToWav();
if (buffer == null)
return false;
File.WriteAllBytes(exportFullName, buffer);
}
else
{
File.WriteAllBytes(exportFullName, m_AudioData);
}
return true;
}
public static bool ExportShader(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".shader";
if (ExportFileExists(exportFullName))
return false;
var m_Shader = (Shader)item.Asset;
if (m_Shader.compressedBlob != null) //5.5 and up
{
var strs = ShaderConverter.ConvertMultiple(m_Shader);
for (int i = 0; i < strs.Length; i++)
{
var platformName = ShaderConverter.GetPlatformString(m_Shader.platforms[i]);
File.WriteAllText($"{exportPath}{item.Text}_{platformName}.shader", strs[i]);
}
}
else
{
var str = ShaderConverter.Convert(m_Shader);
File.WriteAllText(exportFullName, str);
}
return true;
}
public static bool ExportTextAsset(AssetItem item, string exportPath, string format)
{
if (format == "recommanded" || format == "txt")
{
format = "txt";
}
var oriPath = item.Asset.assetsFile.originalPath;
var exportFullName = exportPath + Path.GetFileNameWithoutExtension(oriPath) + "." + format;
var m_TextAsset = (TextAsset)(item.Asset);
File.WriteAllBytes(exportFullName, m_TextAsset.m_Script);
return true;
}
/*
public static bool ExportMonoBehaviour(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".txt";
if (ExportFileExists(exportFullName))
return false;
var m_MonoBehaviour = (MonoBehaviour)item.Asset;
var str = m_MonoBehaviour.Dump() ?? Studio.GetScriptString(item.Asset.reader);
File.WriteAllText(exportFullName, str);
return true;
}
*/
public static bool ExportFont(AssetItem item, string exportPath)
{
var m_Font = (Font)item.Asset;
if (m_Font.m_FontData != null)
{
var extension = ".ttf";
if (m_Font.m_FontData[0] == 79 && m_Font.m_FontData[1] == 84 && m_Font.m_FontData[2] == 84 && m_Font.m_FontData[3] == 79)
{
extension = ".otf";
}
var exportFullName = exportPath + item.Text + extension;
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, m_Font.m_FontData);
return true;
}
return false;
}
public static bool ExportMesh(AssetItem item, string exportPath)
{
var m_Mesh = (Mesh)item.Asset;
if (m_Mesh.m_VertexCount <= 0)
return false;
var exportFullName = exportPath + item.Text + ".obj";
if (ExportFileExists(exportFullName))
return false;
var sb = new StringBuilder();
sb.AppendLine("g " + m_Mesh.m_Name);
#region Vertices
if (m_Mesh.m_Vertices == null || m_Mesh.m_Vertices.Length == 0)
{
return false;
}
int c = 3;
if (m_Mesh.m_Vertices.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("v {0} {1} {2}\r\n", -m_Mesh.m_Vertices[v * c], m_Mesh.m_Vertices[v * c + 1], m_Mesh.m_Vertices[v * c + 2]);
}
#endregion
#region UV
if (m_Mesh.m_UV0 != null && m_Mesh.m_UV0.Length == m_Mesh.m_VertexCount * 2)
{
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vt {0} {1}\r\n", m_Mesh.m_UV0[v * 2], m_Mesh.m_UV0[v * 2 + 1]);
}
}
else if (m_Mesh.m_UV1 != null && m_Mesh.m_UV1.Length == m_Mesh.m_VertexCount * 2)
{
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vt {0} {1}\r\n", m_Mesh.m_UV1[v * 2], m_Mesh.m_UV1[v * 2 + 1]);
}
}
#endregion
#region Normals
if (m_Mesh.m_Normals != null && m_Mesh.m_Normals.Length > 0)
{
if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 3)
{
c = 3;
}
else if (m_Mesh.m_Normals.Length == m_Mesh.m_VertexCount * 4)
{
c = 4;
}
for (int v = 0; v < m_Mesh.m_VertexCount; v++)
{
sb.AppendFormat("vn {0} {1} {2}\r\n", -m_Mesh.m_Normals[v * c], m_Mesh.m_Normals[v * c + 1], m_Mesh.m_Normals[v * c + 2]);
}
}
#endregion
#region Face
int sum = 0;
for (var i = 0; i < m_Mesh.m_SubMeshes.Length; i++)
{
sb.AppendLine($"g {m_Mesh.m_Name}_{i}");
int indexCount = (int)m_Mesh.m_SubMeshes[i].indexCount;
var end = sum + indexCount / 3;
for (int f = sum; f < end; f++)
{
sb.AppendFormat("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\r\n", m_Mesh.m_Indices[f * 3 + 2] + 1, m_Mesh.m_Indices[f * 3 + 1] + 1, m_Mesh.m_Indices[f * 3] + 1);
}
sum = end;
}
#endregion
sb.Replace("NaN", "0");
File.WriteAllText(exportFullName, sb.ToString());
return true;
}
public static bool ExportVideoClip(AssetItem item, string exportPath, string format)
{
if (format == "recommanded" || format == "mp4")
{
format = "mp4";
}
var oriPath = item.Asset.assetsFile.originalPath;
var exportFullName = exportPath + Path.GetFileNameWithoutExtension(oriPath) + "." + format;
var m_TextAsset = (TextAsset)(item.Asset);
var m_VideoClip = (VideoClip)item.Asset;
var m_VideoData = m_VideoClip.m_VideoData.Value;
if (m_VideoData != null && m_VideoData.Length != 0)
{
File.WriteAllBytes(exportFullName, m_VideoData);
return true;
}
return false;
}
public static bool ExportMovieTexture(AssetItem item, string exportPath)
{
var m_MovieTexture = (MovieTexture)item.Asset;
var exportFullName = exportPath + item.Text + ".ogv";
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, m_MovieTexture.m_MovieData);
return true;
}
public static bool ExportSprite(AssetItem item, string exportPath, string format)
{
ImageFormat dstFormat = null;
if (format == "recommanded")
{
format = "png";
}
var oriPath = item.Asset.assetsFile.originalPath;
var exportFullName = exportPath + Path.GetFileNameWithoutExtension(oriPath) + "." + format;
var m_TextAsset = (TextAsset)(item.Asset);
switch (format)
{
case "bmp":
dstFormat = ImageFormat.Bmp;
break;
case "png":
dstFormat = ImageFormat.Png;
break;
case "jpg":
dstFormat = ImageFormat.Jpeg;
break;
}
var bitmap = SpriteHelper.GetImageFromSprite((Sprite)item.Asset);
if (bitmap != null)
{
bitmap.Save(exportFullName, dstFormat);
bitmap.Dispose();
return true;
}
return false;
}
public static bool ExportRawFile(AssetItem item, string exportPath)
{
var exportFullName = exportPath + item.Text + ".dat";
if (ExportFileExists(exportFullName))
return false;
File.WriteAllBytes(exportFullName, item.Asset.GetRawData());
return true;
}
private static bool ExportFileExists(string filename)
{
if (File.Exists(filename))
{
return true;
}
Directory.CreateDirectory(Path.GetDirectoryName(filename));
return false;
}
/*
public static bool ExportAnimator(AssetItem item, string exportPath, List<AssetItem> animationList = null)
{
var m_Animator = (Animator)item.Asset;
var convert = animationList != null ? new ModelConverter(m_Animator, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(m_Animator);
exportPath = $"{exportPath}{item.Text}\\{item.Text}.fbx";
return ExportFbx(convert, exportPath);
}
public static bool ExportGameObject(GameObject gameObject, string exportPath, List<AssetItem> animationList = null)
{
var convert = animationList != null ? new ModelConverter(gameObject, animationList.Select(x => (AnimationClip)x.Asset).ToArray()) : new ModelConverter(gameObject);
exportPath = exportPath + Studio.FixFileName(gameObject.m_Name) + ".fbx";
return ExportFbx(convert, exportPath);
}
private static bool ExportFbx(IImported convert, string exportPath)
{
var eulerFilter = (bool)Properties.Settings.Default["eulerFilter"];
var filterPrecision = (float)(decimal)Properties.Settings.Default["filterPrecision"];
var allFrames = (bool)Properties.Settings.Default["allFrames"];
var allBones = (bool)Properties.Settings.Default["allBones"];
var skins = (bool)Properties.Settings.Default["skins"];
var boneSize = (int)(decimal)Properties.Settings.Default["boneSize"];
var scaleFactor = (float)(decimal)Properties.Settings.Default["scaleFactor"];
var fbxVersion = (int)Properties.Settings.Default["fbxVersion"];
var fbxFormat = (int)Properties.Settings.Default["fbxFormat"];
ModelExporter.ExportFbx(exportPath, convert, eulerFilter, filterPrecision, allFrames, allBones, skins, boneSize, scaleFactor, fbxVersion, fbxFormat == 1);
return true;
}
*/
}
}