-
Notifications
You must be signed in to change notification settings - Fork 72
/
AnimationImporterWindow.cs
439 lines (340 loc) · 13 KB
/
AnimationImporterWindow.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
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
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
using UnityEditor;
using System.IO;
using AnimationImporter.Boomlagoon.JSON;
using UnityEditor.Animations;
using System.Linq;
namespace AnimationImporter
{
public class AnimationImporterWindow : EditorWindow
{
// ================================================================================
// private
// --------------------------------------------------------------------------------
private AnimationImporter importer
{
get
{
return AnimationImporter.Instance;
}
}
private GUIStyle _dropBoxStyle;
private GUIStyle _infoTextStyle;
private string _nonLoopingAnimationEnterValue = "";
private Vector2 _scrollPos = Vector2.zero;
// ================================================================================
// menu entry
// --------------------------------------------------------------------------------
[MenuItem("Window/Animation Importer")]
public static void ImportAnimationsMenu()
{
GetWindow(typeof(AnimationImporterWindow), false, "Anim Importer");
}
// ================================================================================
// unity methods
// --------------------------------------------------------------------------------
public void OnEnable()
{
importer.LoadOrCreateUserConfig();
}
public void OnGUI()
{
CheckGUIStyles();
if (importer.canImportAnimations)
{
_scrollPos = GUILayout.BeginScrollView(_scrollPos);
EditorGUILayout.Space();
ShowAnimationsGUI();
GUILayout.Space(25f);
ShowAnimatorControllerGUI();
GUILayout.Space(25f);
ShowAnimatorOverrideControllerGUI();
GUILayout.Space(25f);
ShowUserConfig();
GUILayout.EndScrollView();
}
else
{
EditorGUILayout.Space();
ShowHeadline("Select Aseprite Application");
EditorGUILayout.Space();
ShowAsepriteApplicationSelection();
EditorGUILayout.Space();
GUILayout.Label("Aseprite has to be installed on this machine because the Importer calls Aseprite through the command line for creating images and getting animation data.", _infoTextStyle);
}
}
// ================================================================================
// GUI methods
// --------------------------------------------------------------------------------
private void CheckGUIStyles()
{
if (_dropBoxStyle == null)
{
GetBoxStyle();
}
if (_infoTextStyle == null)
{
GetTextInfoStyle();
}
}
private void GetBoxStyle()
{
_dropBoxStyle = new GUIStyle(EditorStyles.helpBox);
_dropBoxStyle.alignment = TextAnchor.MiddleCenter;
}
private void GetTextInfoStyle()
{
_infoTextStyle = new GUIStyle(EditorStyles.label);
_infoTextStyle.wordWrap = true;
}
private void ShowUserConfig()
{
if (importer == null || importer.sharedData == null)
{
return;
}
ShowHeadline("Config");
/*
Aseprite Application
*/
ShowAsepriteApplicationSelection();
GUILayout.Space(5f);
/*
sprite values
*/
importer.sharedData.targetObjectType = (AnimationTargetObjectType)EditorGUILayout.EnumPopup("Target Object", importer.sharedData.targetObjectType);
if (importer.sharedData.targetObjectType == AnimationTargetObjectType.SpriteRenderer || importer.sharedData.targetObjectType == AnimationTargetObjectType.SpriteRendererAndImage)
importer.sharedData.pathToSpriteRendererComponent = EditorGUILayout.TextField("Path to Sprite Renderer", importer.sharedData.pathToSpriteRendererComponent);
if (importer.sharedData.targetObjectType == AnimationTargetObjectType.Image || importer.sharedData.targetObjectType == AnimationTargetObjectType.SpriteRendererAndImage)
importer.sharedData.pathToImageComponent = EditorGUILayout.TextField("Path to Image", importer.sharedData.pathToImageComponent);
importer.sharedData.spriteAlignment = (SpriteAlignment)EditorGUILayout.EnumPopup("Sprite Alignment", importer.sharedData.spriteAlignment);
if (importer.sharedData.spriteAlignment == SpriteAlignment.Custom)
{
importer.sharedData.pivotAlignmentType = (PivotAlignmentType)EditorGUILayout.EnumPopup("Pivot Alignment Type", importer.sharedData.pivotAlignmentType);
if (importer.sharedData.pivotAlignmentType == PivotAlignmentType.Normalized)
{
importer.sharedData.spriteAlignmentCustomX = EditorGUILayout.Slider("x", importer.sharedData.spriteAlignmentCustomX, 0, 1f);
importer.sharedData.spriteAlignmentCustomY = EditorGUILayout.Slider("y", importer.sharedData.spriteAlignmentCustomY, 0, 1f);
}
else
{
importer.sharedData.spriteAlignmentCustomX = EditorGUILayout.FloatField("x", importer.sharedData.spriteAlignmentCustomX);
importer.sharedData.spriteAlignmentCustomY = EditorGUILayout.FloatField("y", importer.sharedData.spriteAlignmentCustomY);
}
}
importer.sharedData.spritePixelsPerUnit = EditorGUILayout.FloatField("Sprite Pixels per Unit", importer.sharedData.spritePixelsPerUnit);
GUILayout.Space(5f);
ShowTargetLocationOptions("Sprites", importer.sharedData.spritesTargetLocation);
ShowTargetLocationOptions("Animations", importer.sharedData.animationsTargetLocation);
ShowTargetLocationOptions("AnimationController", importer.sharedData.animationControllersTargetLocation);
GUILayout.Space(5f);
importer.sharedData.spriteNamingScheme = (SpriteNamingScheme)EditorGUILayout.IntPopup("Sprite Naming Scheme",
(int)importer.sharedData.spriteNamingScheme,
SpriteNaming.namingSchemesDisplayValues, SpriteNaming.namingSchemesValues);
GUILayout.Space(25f);
ShowHeadline("Automatic Import");
EditorGUILayout.BeginHorizontal();
importer.sharedData.automaticImporting = EditorGUILayout.Toggle("Automatic Import", importer.sharedData.automaticImporting);
EditorGUILayout.EndHorizontal();
EditorGUILayout.LabelField("Looks for existing Animation Controller with same name.");
/*
animations that do not loop
*/
GUILayout.Space(25f);
ShowHeadline("Non-looping Animations");
for (int i = 0; i < importer.sharedData.animationNamesThatDoNotLoop.Count; i++)
{
GUILayout.BeginHorizontal();
GUILayout.Label(importer.sharedData.animationNamesThatDoNotLoop[i]);
bool doDelete = GUILayout.Button("Delete");
GUILayout.EndHorizontal();
if (doDelete)
{
importer.sharedData.RemoveAnimationThatDoesNotLoop(i);
break;
}
}
EditorGUILayout.Space();
GUILayout.BeginHorizontal();
GUILayout.Label("Add ");
_nonLoopingAnimationEnterValue = EditorGUILayout.TextField(_nonLoopingAnimationEnterValue);
if (GUILayout.Button("Enter"))
{
if (importer.sharedData.AddAnimationThatDoesNotLoop(_nonLoopingAnimationEnterValue))
{
_nonLoopingAnimationEnterValue = "";
}
}
GUILayout.EndHorizontal();
EditorGUILayout.LabelField("Enter Part of the Animation Name or a Regex Expression.");
if (GUI.changed)
{
EditorUtility.SetDirty(importer.sharedData);
}
}
private void ShowTargetLocationOptions(string label, AssetTargetLocation targetLocation)
{
EditorGUILayout.BeginHorizontal();
GUILayout.Label(label, GUILayout.Width(130f));
targetLocation.locationType = (AssetTargetLocationType)EditorGUILayout.EnumPopup(targetLocation.locationType, GUILayout.Width(130f));
bool prevEnabled = GUI.enabled;
GUI.enabled = targetLocation.locationType == AssetTargetLocationType.GlobalDirectory;
string globalDirectory = targetLocation.globalDirectory;
if (GUILayout.Button("Select", GUILayout.Width(50f)))
{
var path = EditorUtility.OpenFolderPanel("Select Target Location", globalDirectory, "");
if (!string.IsNullOrEmpty(path) && AssetDatabase.IsValidFolder(AssetDatabaseUtility.GetAssetPath(path)))
{
targetLocation.globalDirectory = AssetDatabaseUtility.GetAssetPath(path);
}
}
if (targetLocation.locationType == AssetTargetLocationType.GlobalDirectory)
{
string displayDirectory = "/" + globalDirectory;
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
GUILayout.Label(displayDirectory, GUILayout.MaxWidth(300f));
}
GUI.enabled = prevEnabled;
EditorGUILayout.EndHorizontal();
}
private void ShowAsepriteApplicationSelection()
{
GUILayout.BeginHorizontal();
GUILayout.Label("Aseprite Application Path");
string newPath = importer.asepritePath;
if (GUILayout.Button("Select"))
{
var path = EditorUtility.OpenFilePanel(
"Select Aseprite Application",
"",
"exe,app");
if (!string.IsNullOrEmpty(path))
{
newPath = path;
if (Application.platform == RuntimePlatform.OSXEditor)
{
newPath += "/Contents/MacOS/aseprite";
}
}
}
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
importer.asepritePath = GUILayout.TextField(newPath, GUILayout.MaxWidth(300f));
GUILayout.EndHorizontal();
if (!File.Exists(AnimationImporter.Instance.asepritePath))
{
var fileErrorMessage = string.Format(
"Cannot find Aseprite at the specified path. Use the Select button to locate the application.");
EditorGUILayout.HelpBox(fileErrorMessage, MessageType.Warning);
}
}
private void ShowAnimationsGUI()
{
ShowHeadline("Animations");
DefaultAsset[] droppedAssets = ShowDropButton<DefaultAsset>(importer.canImportAnimations, AnimationImporter.IsValidAsset);
if (droppedAssets != null && droppedAssets.Length > 0)
{
ImportAssetsOrError(droppedAssets);
}
}
private void ShowAnimatorControllerGUI()
{
ShowHeadline("Animator Controller + Animations");
DefaultAsset[] droppedAssets = ShowDropButton<DefaultAsset>(importer.canImportAnimations, AnimationImporter.IsValidAsset);
if (droppedAssets != null && droppedAssets.Length > 0)
{
ImportAssetsOrError(droppedAssets, ImportAnimatorController.AnimatorController);
}
}
private void ShowAnimatorOverrideControllerGUI()
{
ShowHeadline("Animator Override Controller + Animations");
importer.baseController = EditorGUILayout.ObjectField("Based on Controller:", importer.baseController, typeof(RuntimeAnimatorController), false) as RuntimeAnimatorController;
DefaultAsset[] droppedAssets = ShowDropButton<DefaultAsset>(importer.canImportAnimationsForOverrideController, AnimationImporter.IsValidAsset);
if (droppedAssets != null && droppedAssets.Length > 0)
{
ImportAssetsOrError(droppedAssets, ImportAnimatorController.AnimatorOverrideController);
}
}
private void ImportAssetsOrError(DefaultAsset[] assets, ImportAnimatorController importAnimatorController = ImportAnimatorController.None)
{
if (AnimationImporter.IsConfiguredForAssets(assets))
{
importer.ImportAssets(assets, importAnimatorController);
}
else
{
ShowPopupForBadAsepritePath(assets[0].name);
}
}
private void ShowPopupForBadAsepritePath(string assetName)
{
var message = string.Format(
"Cannot import Aseprite file \"{0}\" because the application cannot be found at the configured path. Use the Select button in the Config section to locate Aseprite.",
assetName);
EditorUtility.DisplayDialog("Error", message, "Ok");
}
private void ShowHeadline(string headline)
{
EditorGUILayout.LabelField(headline, EditorStyles.boldLabel, GUILayout.Height(20f));
}
// ================================================================================
// OnGUI helper
// --------------------------------------------------------------------------------
public delegate bool IsValidAssetDelegate(string path);
private T[] ShowDropButton<T>(bool isEnabled, IsValidAssetDelegate IsValidAsset) where T : UnityEngine.Object
{
T[] returnValue = null;
Rect drop_area = GUILayoutUtility.GetRect(0.0f, 80.0f, GUILayout.ExpandWidth(true));
GUI.enabled = isEnabled;
GUI.Box(drop_area, "Drop Animation files here", _dropBoxStyle);
GUI.enabled = true;
if (!isEnabled)
return null;
Event evt = Event.current;
switch (evt.type)
{
case EventType.DragUpdated:
case EventType.DragPerform:
if (!drop_area.Contains(evt.mousePosition)
|| !DraggedObjectsContainValidObject<T>(IsValidAsset))
return null;
DragAndDrop.visualMode = DragAndDropVisualMode.Copy;
if (evt.type == EventType.DragPerform)
{
DragAndDrop.AcceptDrag();
List<T> validObjects = new List<T>();
foreach (UnityEngine.Object dragged_object in DragAndDrop.objectReferences)
{
var assetPath = AssetDatabase.GetAssetPath(dragged_object);
if (dragged_object is T && IsValidAsset(assetPath))
{
validObjects.Add(dragged_object as T);
}
}
returnValue = validObjects.ToArray();
}
evt.Use();
break;
}
return returnValue;
}
private bool DraggedObjectsContainValidObject<T>(IsValidAssetDelegate IsValidAsset) where T : UnityEngine.Object
{
foreach (UnityEngine.Object dragged_object in DragAndDrop.objectReferences)
{
var assetPath = AssetDatabase.GetAssetPath(dragged_object);
if (dragged_object is T && IsValidAsset(assetPath))
{
return true;
}
}
return false;
}
}
}