-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathAssetGraphPostprocessor.cs
More file actions
219 lines (175 loc) · 7.64 KB
/
AssetGraphPostprocessor.cs
File metadata and controls
219 lines (175 loc) · 7.64 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
using UnityEditor;
using System;
using System.Linq;
using System.Collections.Generic;
using Model=UnityEngine.AssetGraph.DataModel.Version2;
namespace UnityEngine.AssetGraph {
class AssetGraphPostprocessor : AssetPostprocessor
{
private Stack<AssetGraphController> m_controllers;
private Stack<AssetPostprocessorContext> m_contexts;
private Queue<AssetPostprocessorContext> m_ppQueue;
private static AssetGraphPostprocessor s_postprocessor;
public static AssetGraphPostprocessor Postprocessor {
get {
if (s_postprocessor == null) {
s_postprocessor = new AssetGraphPostprocessor ();
s_postprocessor.Init ();
}
return s_postprocessor;
}
}
private void Init() {
m_controllers = new Stack<AssetGraphController> ();
m_contexts = new Stack<AssetPostprocessorContext> ();
m_ppQueue = new Queue<AssetPostprocessorContext> ();
EditorApplication.update += this.OnEditorUpdate;
}
private void OnEditorUpdate() {
if (m_controllers.Count != 0 || m_contexts.Count != 0) {
return;
}
// double check
if (m_ppQueue.Count > 0 && m_contexts.Count == 0) {
var ctx = m_ppQueue.Dequeue ();
DoPostprocessWithContext (ctx);
}
}
public void PushController(AssetGraphController c) {
m_controllers.Push (c);
}
public void PushContext(AssetPostprocessorContext c) {
m_contexts.Push (c);
}
public AssetGraphController GetCurrentGraphController() {
if (m_controllers.Count == 0) {
return null;
}
return m_controllers.Peek ();
}
public void PopController () {
m_controllers.Pop ();
}
public void PopContext () {
m_contexts.Pop ();
if (m_contexts.Count == 1 && m_contexts.Peek().IsAdhoc) {
m_contexts.Pop ();
}
}
public void AddModifiedAsset(AssetReference a) {
AssetPostprocessorContext ctx = null;
if (m_contexts.Count == 0) {
ctx = new AssetPostprocessorContext ();
m_contexts.Push (ctx);
} else {
ctx = m_contexts.Peek ();
}
if (!ctx.ImportedAssets.Contains (a)) {
ctx.ImportedAssets.Add (a);
}
AssetProcessEventRecord.GetRecord ().LogModify (a.assetDatabaseId);
}
static void OnPostprocessAllAssets (string[] importedAssets,
string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
Postprocessor.HandleAllAssets (importedAssets, deletedAssets, movedAssets, movedFromAssetPaths);
}
private void HandleAllAssets(string[] importedAssets,
string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
for (int i=0; i<movedFromAssetPaths.Length; i++)
{
if (movedFromAssetPaths[i] == DataModel.Version2.Settings.Path.BasePath) {
DataModel.Version2.Settings.Path.ResetBasePath (movedAssets[i]);
}
}
foreach (string str in importedAssets)
{
AssetReferenceDatabase.GetReference(str)?.InvalidateTypeCache();
}
foreach (string str in deletedAssets)
{
AssetReferenceDatabase.DeleteReference(str);
}
for (int i=0; i<movedAssets.Length; i++)
{
AssetReferenceDatabase.MoveReference(movedFromAssetPaths[i], movedAssets[i]);
}
var ctx = new AssetPostprocessorContext (importedAssets, deletedAssets, movedAssets, movedFromAssetPaths, m_contexts);
if (!ctx.HasValidAssetToPostprocess()) {
return;
}
// if modification happens inside graph, record it.
if (m_controllers.Count > 0) {
m_ppQueue.Enqueue (ctx);
return;
}
DoPostprocessWithContext (ctx);
}
private void DoPostprocessWithContext(AssetPostprocessorContext ctx) {
m_contexts.Push (ctx);
NotifyAssetPostprocessorGraphs (ctx);
AssetGraphEditorWindow.NotifyAssetsReimportedToAllWindows(ctx);
m_contexts.Pop ();
}
private void NotifyAssetPostprocessorGraphs(AssetPostprocessorContext ctx)
{
var guids = AssetDatabase.FindAssets(Model.Settings.GRAPH_SEARCH_CONDITION, new string[] { "Assets" });
var executingGraphs = new List<Model.ConfigGraph> ();
foreach(var guid in guids) {
string path = AssetDatabase.GUIDToAssetPath(guid);
var graph = AssetDatabase.LoadAssetAtPath<Model.ConfigGraph>(path);
if (graph != null && graph.UseAsAssetPostprocessor) {
bool isAnyNodeAffected = false;
foreach(var n in graph.Nodes) {
isAnyNodeAffected |= n.Operation.Object.OnAssetsReimported(n, null, EditorUserBuildSettings.activeBuildTarget, ctx, true);
}
if (isAnyNodeAffected) {
executingGraphs.Add (graph);
}
}
}
if (executingGraphs.Count > 0) {
if (executingGraphs.Count > 1) {
executingGraphs.Sort ((l, r) => l.ExecuteOrderPriority - r.ExecuteOrderPriority);
}
float currentCount = 0f;
float totalCount = (float)executingGraphs.Sum (g => g.Nodes.Count);
Model.NodeData lastNode = null;
float graphStartTime = Time.realtimeSinceStartup;
bool progressbarDisplayed = false;
float progressBarShowDelaySec = 0.3f;
Action<Model.NodeData, string, float> updateHandler = (node, message, progress) => {
if(lastNode != node) {
// do not add count on first node visit to
// calcurate percantage correctly
if(lastNode != null) {
++currentCount;
}
lastNode = node;
if(!progressbarDisplayed) {
if(Time.realtimeSinceStartup - graphStartTime > progressBarShowDelaySec) {
progressbarDisplayed = true;
}
}
}
if(progressbarDisplayed) {
var graphName = GetCurrentGraphController().TargetGraph.GetGraphName();
float currentNodeProgress = progress * (1.0f / totalCount);
float currentTotalProgress = (currentCount/totalCount) + currentNodeProgress;
string title = string.Format("Building {2}[{0}/{1}]", currentCount, totalCount, graphName);
string info = $"Processing {node.Name}:{message}";
EditorUtility.DisplayProgressBar(title, info, currentTotalProgress);
}
};
foreach (var g in executingGraphs) {
AssetGraphUtility.ExecuteGraph (g, false, updateHandler);
}
if (progressbarDisplayed) {
EditorUtility.ClearProgressBar ();
}
AssetDatabase.Refresh();
}
}
}
}