/
ImageSequenceAssetEditor.cs
218 lines (189 loc) · 9.59 KB
/
ImageSequenceAssetEditor.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
using UnityEngine;
namespace UnityEditor.VFXToolbox.ImageSequencer
{
[CustomEditor(typeof(ImageSequence))]
internal class ImageSequenceAssetEditor : Editor
{
private bool m_PreviewInput = false;
private bool m_PreviewOutput = false;
private bool m_RequireConstantRepaint = false;
public override bool RequiresConstantRepaint()
{
return m_RequireConstantRepaint;
}
protected override void OnHeaderGUI()
{
base.OnHeaderGUI();
}
public override void OnInspectorGUI()
{
serializedObject.Update();
m_RequireConstantRepaint = false;
using (new EditorGUILayout.VerticalScope())
{
if (GUILayout.Button(VFXToolboxGUIUtility.Get("Edit Sequence"), GUILayout.Height(40)))
{
ImageSequencer toolbox = EditorWindow.GetWindow<ImageSequencer>();
toolbox.LoadAsset((ImageSequence)Selection.activeObject);
}
VFXToolboxGUIUtility.ToggleableHeader(true, false, "Input Frames");
{
var inputFrames = serializedObject.FindProperty("inputFrameGUIDs");
int inputFrameCount = inputFrames.arraySize;
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Label("Input sequence contains " + inputFrameCount + " frame(s).");
GUILayout.FlexibleSpace();
m_PreviewInput = GUILayout.Toggle(m_PreviewInput, VFXToolboxGUIUtility.Get("Preview"), EditorStyles.miniButton);
}
if(inputFrameCount > 0 && m_PreviewInput)
{
int index;
if(inputFrameCount > 1)
{
m_RequireConstantRepaint = true;
float time = (float)EditorApplication.timeSinceStartup;
index = (int)Mathf.Floor((time * 30) % inputFrameCount);
}
else
{
index = 0;
}
var frame = inputFrames.GetArrayElementAtIndex(index);
string guid = frame.stringValue;
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(AssetDatabase.GUIDToAssetPath(guid));
DrawAnimatedPreviewLayout(texture, ((float)index / inputFrameCount));
}
else
{
m_PreviewInput = false;
}
}
GUILayout.Space(24);
VFXToolboxGUIUtility.ToggleableHeader(true, false, "Processors");
{
var processors = serializedObject.FindProperty("processorInfos");
int processorsCount = processors.arraySize;
EditorGUILayout.LabelField("Asset contains " + processorsCount + " Processor (s).");
EditorGUI.indentLevel++;
for(int i = 0; i < processorsCount; i++)
{
var item = processors.GetArrayElementAtIndex(i).objectReferenceValue as ProcessorInfo;
EditorGUILayout.LabelField("#"+i+" - " + item.ProcessorName + (item.Enabled?"":" (Disabled)"));
}
EditorGUI.indentLevel--;
}
GUILayout.Space(24);
VFXToolboxGUIUtility.ToggleableHeader(true, false, "Export Settings");
var exportSettings = serializedObject.FindProperty("exportSettings");
string fileName = exportSettings.FindPropertyRelative("fileName").stringValue;
var mode = (ImageSequence.ExportMode)exportSettings.FindPropertyRelative("exportMode").enumValueIndex;
var frameCount = exportSettings.FindPropertyRelative("frameCount");
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.EnumPopup(VFXToolboxGUIUtility.Get("Export Format"), mode);
EditorGUI.EndDisabledGroup();
if (fileName != "")
{
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.TextField("Exporting to ", fileName);
EditorGUI.EndDisabledGroup();
Rect r = GUILayoutUtility.GetLastRect();
r.width += EditorGUIUtility.fieldWidth;
if (Event.current.rawType == EventType.MouseDown && r.Contains(Event.current.mousePosition))
{
ImageSequencer.PingOutputTexture(fileName);
}
string dir = System.IO.Path.GetDirectoryName(fileName);
string file = System.IO.Path.GetFileNameWithoutExtension(fileName);
string[] assets;
if(!fileName.StartsWith("Assets/"))
{
EditorGUILayout.HelpBox("The output sequence has been exported outside the project, preview will be unavailable", MessageType.Warning);
return;
}
if(fileName.Contains("#"))
{
if(System.IO.Directory.Exists(dir))
{
string[] guids = AssetDatabase.FindAssets(file.Replace('#', '*'), new string[] { dir });
assets = new string[guids.Length];
for(int i = 0; i < guids.Length; i++)
{
assets[i] = AssetDatabase.GUIDToAssetPath(guids[i]);
}
}
else
assets = new string[] { };
}
else
{
assets = new string[] { fileName };
}
int outputFrameCount;
if (frameCount.intValue == assets.Length)
outputFrameCount = frameCount.intValue;
else
outputFrameCount = 0; // Something went wrong
if(outputFrameCount > 0)
{
if(outputFrameCount > 1)
{
using (new EditorGUILayout.HorizontalScope())
{
GUILayout.Label("Output sequence contains " + assets.Length + " frame(s).");
GUILayout.FlexibleSpace();
m_PreviewOutput = GUILayout.Toggle(m_PreviewOutput, VFXToolboxGUIUtility.Get("Preview"), EditorStyles.miniButton);
}
if(m_PreviewOutput)
{
m_RequireConstantRepaint = true;
float time = (float)EditorApplication.timeSinceStartup;
int index = (int)Mathf.Floor((time * 30) % outputFrameCount);
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assets[index]);
DrawAnimatedPreviewLayout(texture, ((float)index / outputFrameCount));
}
else
{
m_PreviewOutput = false;
}
}
else // Only one frame
{
var texture = AssetDatabase.LoadAssetAtPath<Texture2D>(assets[0]);
if (texture != null)
DrawAnimatedPreviewLayout(texture, 0.0f);
else
EditorGUILayout.HelpBox("Output Texture could not be loaded, maybe the file was deleted. Please export again using the editor", MessageType.Error);
}
}
else
{
EditorGUILayout.HelpBox("The output sequence does not match the number of files on disk, you probably need to export your sequence again", MessageType.Warning);
}
}
else
{
EditorGUILayout.HelpBox("This asset has not yet been exported. Please open editor and export it to generate a sequence.",MessageType.None);
}
}
}
private void DrawAnimatedPreviewLayout(Texture2D texture, float progress)
{
float ratio = (float)texture.height / (float)texture.width;
using (new EditorGUILayout.HorizontalScope())
{
float width = EditorGUIUtility.currentViewWidth-32;
float height = 240;
GUILayout.FlexibleSpace();
Rect texture_rect;
if(ratio >= 1)
texture_rect = GUILayoutUtility.GetRect(height / ratio, height);
else
texture_rect = GUILayoutUtility.GetRect(width, width * ratio);
GUILayout.FlexibleSpace();
EditorGUI.DrawTextureTransparent(texture_rect, texture);
EditorGUI.DrawRect(new Rect(texture_rect.x, texture_rect.y, progress * 200.0f / ratio, 4.0f), new Color(0.3f, 0.5f, 1.0f));
}
}
}
}