forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssetPostprocessor.cs
More file actions
574 lines (501 loc) · 21.5 KB
/
Copy pathAssetPostprocessor.cs
File metadata and controls
574 lines (501 loc) · 21.5 KB
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using UnityEngine;
using UnityEngine.Internal;
using UnityEngine.Scripting;
using UnityEngine.Profiling;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEditor.AssetImporters;
using Object = UnityEngine.Object;
using UnityEditor.Experimental.AssetImporters;
namespace UnityEditor
{
// AssetPostprocessor lets you hook into the import pipeline and run scripts prior or after importing assets.
public partial class AssetPostprocessor
{
private string m_PathName;
private AssetImportContext m_Context;
// The path name of the asset being imported.
public string assetPath { get { return m_PathName; } set { m_PathName = value; } }
// The context of the import, used to specify dependencies
public AssetImportContext context { get { return m_Context; } internal set { m_Context = value; } }
// Logs an import warning to the console.
[ExcludeFromDocs]
public void LogWarning(string warning)
{
Object context = null;
LogWarning(warning, context);
}
public void LogWarning(string warning, [DefaultValue("null")] Object context) { Debug.LogWarning(warning, context); }
// Logs an import error message to the console.
[ExcludeFromDocs]
public void LogError(string warning)
{
Object context = null;
LogError(warning, context);
}
public void LogError(string warning, [DefaultValue("null")] Object context) { Debug.LogError(warning, context); }
// Returns the version of the asset postprocessor.
public virtual uint GetVersion() { return 0; }
// Reference to the asset importer
public AssetImporter assetImporter { get { return AssetImporter.GetAtPath(assetPath); } }
[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
[Obsolete("To set or get the preview, call EditorUtility.SetAssetPreview or AssetPreview.GetAssetPreview instead", true)]
public Texture2D preview { get { return null; } set {} }
// Override the order in which importers are processed.
public virtual int GetPostprocessOrder() { return 0; }
}
internal class AssetPostprocessingInternal
{
static void LogPostProcessorMissingDefaultConstructor(Type type)
{
Debug.LogErrorFormat("{0} requires a default constructor to be used as an asset post processor", type);
}
[RequiredByNativeCode]
// Postprocess on all assets once an automatic import has completed
static void PostprocessAllAssets(string[] importedAssets, string[] addedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromPathAssets)
{
bool profile = Profiler.enabled;
object[] args = { importedAssets, deletedAssets, movedAssets, movedFromPathAssets };
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
MethodInfo method = assetPostprocessorClass.GetMethod("OnPostprocessAllAssets", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
if (method != null)
{
InvokeMethod(method, args);
}
}
Profiler.BeginSample("SyncVS.PostprocessSyncProject");
///@TODO: we need addedAssets for SyncVS. Make this into a proper API and write tests
CodeEditorProjectSync.PostprocessSyncProject(importedAssets, addedAssets, deletedAssets, movedAssets, movedFromPathAssets);
Profiler.EndSample();
}
[RequiredByNativeCode]
static void PreprocessAssembly(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessAssembly", new[] { pathName });
}
}
internal class CompareAssetImportPriority : IComparer
{
int IComparer.Compare(System.Object xo, System.Object yo)
{
int x = ((AssetPostprocessor)xo).GetPostprocessOrder();
int y = ((AssetPostprocessor)yo).GetPostprocessOrder();
return x.CompareTo(y);
}
}
private static string BuildHashString(SortedList<string, uint> list)
{
var hashStr = "";
foreach (var pair in list)
{
hashStr += pair.Key;
hashStr += '.';
hashStr += pair.Value;
hashStr += '|';
}
return hashStr;
}
internal class PostprocessStack
{
internal ArrayList m_ImportProcessors = null;
}
static ArrayList m_PostprocessStack = null;
static ArrayList m_ImportProcessors = null;
static Type[] m_PostprocessorClasses = null;
static string m_MeshProcessorsHashString = null;
static string m_TextureProcessorsHashString = null;
static string m_AudioProcessorsHashString = null;
static Type[] GetCachedAssetPostprocessorClasses()
{
if (m_PostprocessorClasses == null)
m_PostprocessorClasses = TypeCache.GetTypesDerivedFrom<AssetPostprocessor>().ToArray();
return m_PostprocessorClasses;
}
[RequiredByNativeCode]
static void InitPostprocessors(AssetImportContext context, string pathName)
{
m_ImportProcessors = new ArrayList();
// @TODO: This is just a temporary workaround for the import settings.
// We should add importers to the asset, persist them and show an inspector for them.
foreach (Type assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
var assetPostprocessor = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
assetPostprocessor.assetPath = pathName;
assetPostprocessor.context = context;
m_ImportProcessors.Add(assetPostprocessor);
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
m_ImportProcessors.Sort(new CompareAssetImportPriority());
// Setup postprocessing stack to support rentrancy (Import asset immediate)
PostprocessStack postStack = new PostprocessStack();
postStack.m_ImportProcessors = m_ImportProcessors;
if (m_PostprocessStack == null)
m_PostprocessStack = new ArrayList();
m_PostprocessStack.Add(postStack);
}
[RequiredByNativeCode]
static void CleanupPostprocessors()
{
if (m_PostprocessStack != null)
{
m_PostprocessStack.RemoveAt(m_PostprocessStack.Count - 1);
if (m_PostprocessStack.Count != 0)
{
PostprocessStack postStack = (PostprocessStack)m_PostprocessStack[m_PostprocessStack.Count - 1];
m_ImportProcessors = postStack.m_ImportProcessors;
}
}
}
static bool ImplementsAnyOfTheses(Type type, string[] methods)
{
foreach (var method in methods)
{
if (type.GetMethod(method, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null)
return true;
}
return false;
}
[RequiredByNativeCode]
static string GetMeshProcessorsHashString()
{
if (m_MeshProcessorsHashString != null)
return m_MeshProcessorsHashString;
var versionsByType = new SortedList<string, uint>();
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasAnyPostprocessMethod = ImplementsAnyOfTheses(type, new[]
{
"OnPreprocessModel",
"OnPostprocessMeshHierarchy",
"OnPostprocessModel",
"OnPreprocessAnimation",
"OnPostprocessAnimation",
"OnPostprocessGameObjectWithAnimatedUserProperties",
"OnPostprocessGameObjectWithUserProperties",
"OnPostprocessMaterial",
"OnAssignMaterialModel",
"OnPreprocessMaterialDescription"
});
uint version = inst.GetVersion();
if (version != 0 && hasAnyPostprocessMethod)
{
versionsByType.Add(type.FullName, version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
m_MeshProcessorsHashString = BuildHashString(versionsByType);
return m_MeshProcessorsHashString;
}
[RequiredByNativeCode]
static void PreprocessAsset()
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessAsset", null);
}
}
[RequiredByNativeCode]
static void PreprocessMesh(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessModel", null);
}
}
[RequiredByNativeCode]
static void PreprocessSpeedTree(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessSpeedTree", null);
}
}
[RequiredByNativeCode]
static void PreprocessAnimation(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessAnimation", null);
}
}
[RequiredByNativeCode]
static void PostprocessAnimation(GameObject root, AnimationClip clip)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { root, clip };
InvokeMethodIfAvailable(inst, "OnPostprocessAnimation", args);
}
}
[RequiredByNativeCode]
static Material ProcessMeshAssignMaterial(Renderer renderer, Material material)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { material, renderer };
object assignedMaterial = InvokeMethodIfAvailable(inst, "OnAssignMaterialModel", args);
if (assignedMaterial as Material)
return assignedMaterial as Material;
}
return null;
}
[RequiredByNativeCode]
static bool ProcessMeshHasAssignMaterial()
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
if (inst.GetType().GetMethod("OnAssignMaterialModel", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null)
return true;
}
return false;
}
[RequiredByNativeCode]
static void PostprocessMeshHierarchy(GameObject root)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { root };
InvokeMethodIfAvailable(inst, "OnPostprocessMeshHierarchy", args);
}
}
static void PostprocessMesh(GameObject gameObject)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { gameObject };
InvokeMethodIfAvailable(inst, "OnPostprocessModel", args);
}
}
static void PostprocessSpeedTree(GameObject gameObject)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { gameObject };
InvokeMethodIfAvailable(inst, "OnPostprocessSpeedTree", args);
}
}
[RequiredByNativeCode]
static void PostprocessMaterial(Material material)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { material };
InvokeMethodIfAvailable(inst, "OnPostprocessMaterial", args);
}
}
[RequiredByNativeCode]
static void PreprocessMaterialDescription(MaterialDescription description, Material material, AnimationClip[] animations)
{
object[] args = { description, material, animations };
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessMaterialDescription", args);
}
}
[RequiredByNativeCode]
static void PostprocessGameObjectWithUserProperties(GameObject go, string[] prop_names, object[] prop_values)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { go, prop_names, prop_values };
InvokeMethodIfAvailable(inst, "OnPostprocessGameObjectWithUserProperties", args);
}
}
[RequiredByNativeCode]
static EditorCurveBinding[] PostprocessGameObjectWithAnimatedUserProperties(GameObject go, EditorCurveBinding[] bindings)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { go, bindings };
InvokeMethodIfAvailable(inst, "OnPostprocessGameObjectWithAnimatedUserProperties", args);
}
return bindings;
}
[RequiredByNativeCode]
static string GetTextureProcessorsHashString()
{
if (m_TextureProcessorsHashString != null)
return m_TextureProcessorsHashString;
var versionsByType = new SortedList<string, uint>();
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasPreProcessMethod = type.GetMethod("OnPreprocessTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasPostProcessMethod = (type.GetMethod("OnPostprocessTexture", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null) ||
(type.GetMethod("OnPostprocessCubemap", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null);
uint version = inst.GetVersion();
if (version != 0 && (hasPreProcessMethod || hasPostProcessMethod))
{
versionsByType.Add(type.FullName, version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
m_TextureProcessorsHashString = BuildHashString(versionsByType);
return m_TextureProcessorsHashString;
}
[RequiredByNativeCode]
static void PreprocessTexture(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessTexture", null);
}
}
[RequiredByNativeCode]
static void PostprocessTexture(Texture2D tex, string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex };
InvokeMethodIfAvailable(inst, "OnPostprocessTexture", args);
}
}
[RequiredByNativeCode]
static void PostprocessCubemap(Cubemap tex, string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex };
InvokeMethodIfAvailable(inst, "OnPostprocessCubemap", args);
}
}
[RequiredByNativeCode]
static void PostprocessSprites(Texture2D tex, string pathName, Sprite[] sprites)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex, sprites };
InvokeMethodIfAvailable(inst, "OnPostprocessSprites", args);
}
}
[RequiredByNativeCode]
static string GetAudioProcessorsHashString()
{
if (m_AudioProcessorsHashString != null)
return m_AudioProcessorsHashString;
var versionsByType = new SortedList<string, uint>();
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
try
{
var inst = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
var type = inst.GetType();
bool hasPreProcessMethod = type.GetMethod("OnPreprocessAudio", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
bool hasPostProcessMethod = type.GetMethod("OnPostprocessAudio", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance) != null;
uint version = inst.GetVersion();
if (version != 0 && (hasPreProcessMethod || hasPostProcessMethod))
{
versionsByType.Add(type.FullName, version);
}
}
catch (MissingMethodException)
{
LogPostProcessorMissingDefaultConstructor(assetPostprocessorClass);
}
catch (Exception e)
{
Debug.LogException(e);
}
}
m_AudioProcessorsHashString = BuildHashString(versionsByType);
return m_AudioProcessorsHashString;
}
[RequiredByNativeCode]
static void PreprocessAudio(string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
InvokeMethodIfAvailable(inst, "OnPreprocessAudio", null);
}
}
[RequiredByNativeCode]
static void PostprocessAudio(AudioClip tex, string pathName)
{
foreach (AssetPostprocessor inst in m_ImportProcessors)
{
object[] args = { tex };
InvokeMethodIfAvailable(inst, "OnPostprocessAudio", args);
}
}
[RequiredByNativeCode]
static void PostprocessAssetbundleNameChanged(string assetPAth, string prevoiusAssetBundleName, string newAssetBundleName)
{
object[] args = { assetPAth, prevoiusAssetBundleName, newAssetBundleName };
foreach (var assetPostprocessorClass in GetCachedAssetPostprocessorClasses())
{
var assetPostprocessor = Activator.CreateInstance(assetPostprocessorClass) as AssetPostprocessor;
InvokeMethodIfAvailable(assetPostprocessor, "OnPostprocessAssetbundleNameChanged", args);
}
}
static object InvokeMethod(MethodInfo method, object[] args)
{
bool profile = Profiler.enabled;
if (profile)
Profiler.BeginSample(method.DeclaringType.FullName + "." + method.Name);
var res = method.Invoke(null, args);
if (profile)
Profiler.EndSample();
return res;
}
static object InvokeMethodIfAvailable(object target, string methodName, object[] args)
{
bool profile = Profiler.enabled;
MethodInfo method = target.GetType().GetMethod(methodName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
if (method != null)
{
if (profile)
Profiler.BeginSample(target.GetType().FullName + "." + methodName);
var res = method.Invoke(target, args);
if (profile)
Profiler.EndSample();
return res;
}
else
{
return null;
}
}
}
}