少年修仙传客户端基础资源
hch
2025-02-14 822513a5dff92368ef8dfe5d12a3544b67bd747a
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
/******************************************************************************
 * Spine Runtimes License Agreement
 * Last updated July 28, 2023. Replaces all prior versions.
 *
 * Copyright (c) 2013-2023, Esoteric Software LLC
 *
 * Integration of the Spine Runtimes into software or otherwise creating
 * derivative works of the Spine Runtimes is permitted under the terms and
 * conditions of Section 2 of the Spine Editor License Agreement:
 * http://esotericsoftware.com/spine-editor-license
 *
 * Otherwise, it is permitted to integrate the Spine Runtimes into software or
 * otherwise create derivative works of the Spine Runtimes (collectively,
 * "Products"), provided that each user of the Products must obtain their own
 * Spine Editor license and redistribution of the Products in any form must
 * include this license and copyright notice.
 *
 * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
 * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THE
 * SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *****************************************************************************/
 
#define SPINE_SKELETON_MECANIM
 
#if (UNITY_2017_4 || UNITY_2018_1_OR_NEWER)
#define SPINE_UNITY_2018_PREVIEW_API
#endif
 
 
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using CompatibilityProblemInfo = Spine.Unity.SkeletonDataCompatibility.CompatibilityProblemInfo;
 
namespace Spine.Unity.Editor {
    using Animation = Spine.Animation;
    using Event = UnityEngine.Event;
    using Icons = SpineEditorUtilities.Icons;
 
    [CustomEditor(typeof(SkeletonDataAsset)), CanEditMultipleObjects]
    public class SkeletonDataAssetInspector : UnityEditor.Editor {
        internal static bool showAnimationStateData = true;
        internal static bool showAnimationList = true;
        internal static bool showSlotList = false;
        internal static bool showAttachments = false;
 
        SerializedProperty atlasAssets, skeletonJSON, scale, fromAnimation, toAnimation, duration, defaultMix;
        SerializedProperty skeletonDataModifiers;
        SerializedProperty blendModeMaterials;
#if SPINE_TK2D
        SerializedProperty spriteCollection;
#endif
 
#if SPINE_SKELETON_MECANIM
        static bool isMecanimExpanded = false;
        SerializedProperty controller;
#endif
 
        SkeletonDataAsset targetSkeletonDataAsset;
        SkeletonData targetSkeletonData;
 
        readonly List<string> warnings = new List<string>();
        CompatibilityProblemInfo compatibilityProblemInfo = null;
        readonly SkeletonInspectorPreview preview = new SkeletonInspectorPreview();
        bool requiresReload = false;
 
        GUIStyle activePlayButtonStyle, idlePlayButtonStyle;
        readonly GUIContent DefaultMixLabel = new GUIContent("Default Mix Duration", "Sets 'SkeletonDataAsset.defaultMix' in the asset and 'AnimationState.data.defaultMix' at runtime load time.");
 
        string TargetAssetGUID { get { return AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(targetSkeletonDataAsset)); } }
        string LastSkinKey { get { return TargetAssetGUID + "_lastSkin"; } }
        string LastSkinName { get { return EditorPrefs.GetString(LastSkinKey, ""); } }
 
        void OnEnable () {
            InitializeEditor();
        }
 
        void OnDestroy () {
            HandleOnDestroyPreview();
            AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
            EditorApplication.update -= preview.HandleEditorUpdate;
        }
 
        private void OnDomainUnload (object sender, EventArgs e) {
            OnDestroy();
        }
 
        public void UpdateSkeletonData () {
            preview.Clear();
            InitializeEditor();
            if (targetSkeletonDataAsset)
                EditorUtility.SetDirty(targetSkeletonDataAsset);
        }
 
        void InitializeEditor () {
            SpineEditorUtilities.ConfirmInitialization();
            targetSkeletonDataAsset = (SkeletonDataAsset)target;
 
            bool newAtlasAssets = atlasAssets == null;
            if (newAtlasAssets) atlasAssets = serializedObject.FindProperty("atlasAssets");
            skeletonJSON = serializedObject.FindProperty("skeletonJSON");
            scale = serializedObject.FindProperty("scale");
            fromAnimation = serializedObject.FindProperty("fromAnimation");
            toAnimation = serializedObject.FindProperty("toAnimation");
            duration = serializedObject.FindProperty("duration");
            defaultMix = serializedObject.FindProperty("defaultMix");
 
            skeletonDataModifiers = serializedObject.FindProperty("skeletonDataModifiers");
            blendModeMaterials = serializedObject.FindProperty("blendModeMaterials");
 
#if SPINE_SKELETON_MECANIM
            controller = serializedObject.FindProperty("controller");
#endif
 
#if SPINE_TK2D
            if (newAtlasAssets) atlasAssets.isExpanded = false;
            spriteCollection = serializedObject.FindProperty("spriteCollection");
#else
            // Analysis disable once ConvertIfToOrExpression
            if (newAtlasAssets) atlasAssets.isExpanded = true;
#endif
 
            // This handles the case where the managed editor assembly is unloaded before recompilation when code changes.
            AppDomain.CurrentDomain.DomainUnload -= OnDomainUnload;
            AppDomain.CurrentDomain.DomainUnload += OnDomainUnload;
 
            EditorApplication.update -= preview.HandleEditorUpdate;
            EditorApplication.update += preview.HandleEditorUpdate;
            preview.OnSkinChanged -= HandlePreviewSkinChanged;
            preview.OnSkinChanged += HandlePreviewSkinChanged;
 
            PopulateWarnings();
            if (targetSkeletonDataAsset.skeletonJSON == null) {
                targetSkeletonData = null;
                return;
            }
 
            targetSkeletonData = NoProblems() ? targetSkeletonDataAsset.GetSkeletonData(false) : null;
 
            if (targetSkeletonData != null && NoProblems()) {
                preview.Initialize(this.Repaint, targetSkeletonDataAsset, this.LastSkinName);
            }
 
        }
 
        void Clear () {
            preview.Clear();
            SpineEditorUtilities.ClearSkeletonDataAsset(targetSkeletonDataAsset);
            targetSkeletonData = null;
        }
 
        override public void OnInspectorGUI () {
            // Multi-Editing
            if (serializedObject.isEditingMultipleObjects) {
                OnInspectorGUIMulti();
                return;
            }
 
            { // Lazy initialization because accessing EditorStyles values in OnEnable during a recompile causes UnityEditor to throw null exceptions. (Unity 5.3.5)
                idlePlayButtonStyle = idlePlayButtonStyle ?? new GUIStyle(EditorStyles.miniButton);
                if (activePlayButtonStyle == null) {
                    activePlayButtonStyle = new GUIStyle(idlePlayButtonStyle);
                    activePlayButtonStyle.normal.textColor = Color.red;
                }
            }
 
            serializedObject.Update();
 
            // Header
            EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(target.name + " (SkeletonDataAsset)", Icons.spine), EditorStyles.whiteLargeLabel);
            if (targetSkeletonData != null) EditorGUILayout.LabelField("(Drag and Drop to instantiate.)", EditorStyles.miniLabel);
 
            // Main Serialized Fields
            using (var changeCheck = new EditorGUI.ChangeCheckScope()) {
                using (new SpineInspectorUtility.BoxScope())
                    DrawSkeletonDataFields();
 
                if (compatibilityProblemInfo != null)
                    return;
 
                using (new SpineInspectorUtility.BoxScope()) {
                    DrawAtlasAssetsFields();
                    HandleAtlasAssetsNulls();
                }
 
                if (changeCheck.changed) {
                    if (serializedObject.ApplyModifiedProperties() || requiresReload) {
                        this.Clear();
                        this.InitializeEditor();
 
                        if (SpineEditorUtilities.Preferences.autoReloadSceneSkeletons && NoProblems())
                            SpineEditorUtilities.DataReloadHandler.ReloadSceneSkeletonComponents(targetSkeletonDataAsset);
                        return;
                    }
                }
            }
 
            // Unity Quirk: Some code depends on valid preview. If preview is initialized elsewhere, this can cause contents to change between Layout and Repaint events, causing GUILayout control count errors.
            if (NoProblems())
                preview.Initialize(this.Repaint, targetSkeletonDataAsset, this.LastSkinName);
 
            if (targetSkeletonData != null) {
                GUILayout.Space(20f);
 
                using (new SpineInspectorUtility.BoxScope(false)) {
                    EditorGUILayout.LabelField(SpineInspectorUtility.TempContent("Mix Settings", Icons.animationRoot), EditorStyles.boldLabel);
                    DrawAnimationStateInfo();
                    EditorGUILayout.Space();
                }
 
                EditorGUILayout.LabelField("Preview", EditorStyles.boldLabel);
                DrawAnimationList();
                if (targetSkeletonData.Animations.Count > 0) {
                    const string AnimationReferenceButtonText = "Create Animation Reference Assets";
                    const string AnimationReferenceTooltipText = "AnimationReferenceAsset acts as Unity asset for a reference to a Spine.Animation. This can be used in inspectors.\n\nIt serializes a reference to a SkeletonData asset and an animationName.\n\nAt runtime, a reference to its Spine.Animation is loaded and cached into the object to be used as needed. This skips the need to find and cache animation references in individual MonoBehaviours.";
                    if (GUILayout.Button(SpineInspectorUtility.TempContent(AnimationReferenceButtonText, Icons.animationRoot, AnimationReferenceTooltipText), GUILayout.Width(250), GUILayout.Height(26))) {
                        CreateAnimationReferenceAssets();
                    }
                }
                EditorGUILayout.Space();
                DrawSlotList();
                EditorGUILayout.Space();
 
                DrawUnityTools();
 
            } else {
#if !SPINE_TK2D
                // Draw Reimport Button
                using (new EditorGUI.DisabledGroupScope(skeletonJSON.objectReferenceValue == null)) {
                    if (GUILayout.Button(SpineInspectorUtility.TempContent("Attempt Reimport", Icons.warning)))
                        DoReimport();
                }
#else
                EditorGUILayout.HelpBox("Couldn't load SkeletonData.", MessageType.Error);
#endif
 
                DrawWarningList();
            }
 
            if (!Application.isPlaying)
                serializedObject.ApplyModifiedProperties();
        }
 
        void CreateAnimationReferenceAssets () {
            const string AssetFolderName = "ReferenceAssets";
            string parentFolder = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(targetSkeletonDataAsset));
            string dataPath = parentFolder + "/" + AssetFolderName;
            if (!AssetDatabase.IsValidFolder(dataPath)) {
                AssetDatabase.CreateFolder(parentFolder, AssetFolderName);
            }
 
            FieldInfo nameField = typeof(AnimationReferenceAsset).GetField("animationName", BindingFlags.NonPublic | BindingFlags.Instance);
            FieldInfo skeletonDataAssetField = typeof(AnimationReferenceAsset).GetField("skeletonDataAsset", BindingFlags.NonPublic | BindingFlags.Instance);
            foreach (Animation animation in targetSkeletonData.Animations) {
                string assetPath = string.Format("{0}/{1}.asset", dataPath, AssetUtility.GetPathSafeName(animation.Name));
                AnimationReferenceAsset existingAsset = AssetDatabase.LoadAssetAtPath<AnimationReferenceAsset>(assetPath);
                if (existingAsset == null) {
                    AnimationReferenceAsset newAsset = ScriptableObject.CreateInstance<AnimationReferenceAsset>();
                    skeletonDataAssetField.SetValue(newAsset, targetSkeletonDataAsset);
                    nameField.SetValue(newAsset, animation.Name);
                    AssetDatabase.CreateAsset(newAsset, assetPath);
                }
            }
 
            UnityEngine.Object folderObject = AssetDatabase.LoadAssetAtPath(dataPath, typeof(UnityEngine.Object));
            if (folderObject != null) {
                Selection.activeObject = folderObject;
                EditorGUIUtility.PingObject(folderObject);
            }
        }
 
        void OnInspectorGUIMulti () {
 
            // Skeleton data file field.
            using (new SpineInspectorUtility.BoxScope()) {
                EditorGUILayout.LabelField("SkeletonData", EditorStyles.boldLabel);
                EditorGUILayout.PropertyField(skeletonJSON, SpineInspectorUtility.TempContent(skeletonJSON.displayName, Icons.spine));
                EditorGUILayout.DelayedFloatField(scale); //EditorGUILayout.PropertyField(scale);
                EditorGUILayout.Space();
                EditorGUILayout.PropertyField(skeletonDataModifiers, true);
 
                DrawBlendModeMaterialProperties();
            }
 
            // Texture source field.
            using (new SpineInspectorUtility.BoxScope()) {
                EditorGUILayout.LabelField("Atlas", EditorStyles.boldLabel);
#if !SPINE_TK2D
                EditorGUILayout.PropertyField(atlasAssets, true);
#else
                using (new EditorGUI.DisabledGroupScope(spriteCollection.objectReferenceValue != null)) {
                    EditorGUILayout.PropertyField(atlasAssets, true);
                }
                EditorGUILayout.LabelField("spine-tk2d", EditorStyles.boldLabel);
                EditorGUILayout.PropertyField(spriteCollection, true);
#endif
            }
 
            // Mix settings.
            using (new SpineInspectorUtility.BoxScope()) {
                EditorGUILayout.LabelField("Mix Settings", EditorStyles.boldLabel);
                SpineInspectorUtility.PropertyFieldWideLabel(defaultMix, DefaultMixLabel, 160);
                EditorGUILayout.Space();
            }
 
        }
 
        void DrawBlendModeMaterialProperties () {
            if (skeletonDataModifiers.arraySize > 0) {
                EditorGUILayout.BeginHorizontal(GUILayout.Height(EditorGUIUtility.singleLineHeight + 5));
                EditorGUILayout.PrefixLabel("Blend Modes");
                if (GUILayout.Button(new GUIContent("Upgrade", "Upgrade BlendModeMaterialAsset to built-in BlendModeMaterials."), EditorStyles.miniButton, GUILayout.Width(65f))) {
                    foreach (SkeletonDataAsset skeletonData in targets) {
                        BlendModeMaterialsUtility.UpgradeBlendModeMaterials(skeletonData);
                    }
                }
                EditorGUILayout.EndHorizontal();
            }
            EditorGUI.BeginChangeCheck();
            EditorGUILayout.PropertyField(blendModeMaterials, true);
            if (EditorGUI.EndChangeCheck()) {
                serializedObject.ApplyModifiedProperties();
                foreach (SkeletonDataAsset skeletonData in targets) {
                    BlendModeMaterialsUtility.UpdateBlendModeMaterials(skeletonData);
                }
            }
        }
 
        void DrawSkeletonDataFields () {
            using (new EditorGUILayout.HorizontalScope()) {
                EditorGUILayout.LabelField("SkeletonData", EditorStyles.boldLabel);
                if (targetSkeletonData != null) {
                    SkeletonData sd = targetSkeletonData;
                    string m = string.Format("{8} - {0} {1}\nBones: {2}\nConstraints: \n {5} IK \n {6} Path \n {7} Transform\n\nSlots: {3}\nSkins: {4}\n\nAnimations: {9}",
                        sd.Version, string.IsNullOrEmpty(sd.Version) ? "" : "export          ", sd.Bones.Count, sd.Slots.Count, sd.Skins.Count, sd.IkConstraints.Count, sd.PathConstraints.Count, sd.TransformConstraints.Count, skeletonJSON.objectReferenceValue.name, sd.Animations.Count);
                    EditorGUILayout.LabelField(GUIContent.none, new GUIContent(Icons.info, m), GUILayout.Width(30f));
                }
            }
            EditorGUILayout.PropertyField(skeletonJSON, SpineInspectorUtility.TempContent(skeletonJSON.displayName, Icons.spine));
 
            if (compatibilityProblemInfo != null) {
                EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(compatibilityProblemInfo.DescriptionString(), Icons.warning), GUILayout.Height(52));
                return;
            }
 
            EditorGUILayout.DelayedFloatField(scale); //EditorGUILayout.PropertyField(scale);
            EditorGUILayout.Space();
            EditorGUILayout.PropertyField(skeletonDataModifiers, true);
 
            DrawBlendModeMaterialProperties();
        }
 
        void DrawAtlasAssetsFields () {
            EditorGUILayout.LabelField("Atlas", EditorStyles.boldLabel);
 
            using (var changeCheck = new EditorGUI.ChangeCheckScope()) {
#if !SPINE_TK2D
                EditorGUILayout.PropertyField(atlasAssets, true);
#else
                using (new EditorGUI.DisabledGroupScope(spriteCollection.objectReferenceValue != null)) {
                    EditorGUILayout.PropertyField(atlasAssets, true);
                }
                EditorGUILayout.LabelField("spine-tk2d", EditorStyles.boldLabel);
                EditorGUILayout.PropertyField(spriteCollection, true);
#endif
                if (atlasAssets.arraySize == 0)
                    EditorGUILayout.HelpBox("AtlasAssets array is empty. Skeleton's attachments will load without being mapped to images.", MessageType.Info);
 
                if (changeCheck.changed) {
                    requiresReload = true;
                }
            }
        }
 
        void HandleAtlasAssetsNulls () {
            bool hasNulls = false;
            foreach (AtlasAssetBase a in targetSkeletonDataAsset.atlasAssets) {
                if (a == null) {
                    hasNulls = true;
                    break;
                }
            }
            if (hasNulls) {
                if (targetSkeletonDataAsset.atlasAssets.Length == 1) {
                    EditorGUILayout.HelpBox("Atlas array cannot have null entries!", MessageType.None);
                } else {
                    EditorGUILayout.HelpBox("Atlas array should not have null entries!", MessageType.Error);
                    if (SpineInspectorUtility.CenteredButton(SpineInspectorUtility.TempContent("Remove null entries"))) {
                        List<AtlasAssetBase> trimmedAtlasAssets = new List<AtlasAssetBase>();
                        foreach (AtlasAssetBase a in targetSkeletonDataAsset.atlasAssets) {
                            if (a != null)
                                trimmedAtlasAssets.Add(a);
                        }
                        targetSkeletonDataAsset.atlasAssets = trimmedAtlasAssets.ToArray();
                        serializedObject.Update();
                    }
                }
            }
        }
 
        void DrawAnimationStateInfo () {
            using (new SpineInspectorUtility.IndentScope())
                showAnimationStateData = EditorGUILayout.Foldout(showAnimationStateData, "Animation State Data");
 
            if (!showAnimationStateData)
                return;
 
            using (var cc = new EditorGUI.ChangeCheckScope()) {
                using (new SpineInspectorUtility.IndentScope())
                    SpineInspectorUtility.PropertyFieldWideLabel(defaultMix, DefaultMixLabel, 160);
 
 
                if (fromAnimation.arraySize > 0) {
                    using (new SpineInspectorUtility.IndentScope()) {
                        EditorGUILayout.LabelField("Custom Mix Durations", EditorStyles.boldLabel);
                    }
 
                    for (int i = 0; i < fromAnimation.arraySize; i++) {
                        SerializedProperty from = fromAnimation.GetArrayElementAtIndex(i);
                        SerializedProperty to = toAnimation.GetArrayElementAtIndex(i);
                        SerializedProperty durationProp = duration.GetArrayElementAtIndex(i);
                        using (new EditorGUILayout.HorizontalScope()) {
                            GUILayout.Space(16f); // Space instead of EditorGUIUtility.indentLevel. indentLevel will add the space on every field.
                            EditorGUILayout.PropertyField(from, GUIContent.none);
                            //EditorGUILayout.LabelField(">", EditorStyles.miniLabel, GUILayout.Width(9f));
                            EditorGUILayout.PropertyField(to, GUIContent.none);
                            //GUILayout.Space(5f);
                            durationProp.floatValue = EditorGUILayout.FloatField(durationProp.floatValue, GUILayout.MinWidth(25f), GUILayout.MaxWidth(60f));
                            if (GUILayout.Button("Delete", EditorStyles.miniButton)) {
                                duration.DeleteArrayElementAtIndex(i);
                                toAnimation.DeleteArrayElementAtIndex(i);
                                fromAnimation.DeleteArrayElementAtIndex(i);
                            }
                        }
                    }
                }
 
                using (new EditorGUILayout.HorizontalScope()) {
                    EditorGUILayout.Space();
                    if (GUILayout.Button("Add Custom Mix")) {
                        duration.arraySize++;
                        toAnimation.arraySize++;
                        fromAnimation.arraySize++;
                    }
                    EditorGUILayout.Space();
                }
 
                if (cc.changed) {
                    targetSkeletonDataAsset.FillStateData(quiet: true);
                    EditorUtility.SetDirty(targetSkeletonDataAsset);
                    serializedObject.ApplyModifiedProperties();
                }
            }
        }
 
        void DrawAnimationList () {
            showAnimationList = EditorGUILayout.Foldout(showAnimationList, SpineInspectorUtility.TempContent(string.Format("Animations [{0}]", targetSkeletonData.Animations.Count), Icons.animationRoot));
            if (!showAnimationList)
                return;
 
            bool isPreviewWindowOpen = preview.IsValid;
 
            if (isPreviewWindowOpen) {
                if (GUILayout.Button(SpineInspectorUtility.TempContent("Setup Pose", Icons.skeleton), GUILayout.Width(105), GUILayout.Height(18))) {
                    preview.ClearAnimationSetupPose();
                    preview.RefreshOnNextUpdate();
                }
            } else {
                EditorGUILayout.HelpBox("Animations can be previewed if you expand the Preview window below.", MessageType.Info);
            }
 
            EditorGUILayout.LabelField("Name", "      Duration");
            //bool nonessential = targetSkeletonData.ImagesPath != null; // Currently the only way to determine if skeleton data has nonessential data. (Spine 3.6)
            //float fps = targetSkeletonData.Fps;
            //if (nonessential && fps == 0) fps = 30;
 
            TrackEntry activeTrack = preview.ActiveTrack;
            foreach (Animation animation in targetSkeletonData.Animations) {
                using (new GUILayout.HorizontalScope()) {
                    if (isPreviewWindowOpen) {
                        bool active = activeTrack != null && activeTrack.Animation == animation;
                        //bool sameAndPlaying = active && activeTrack.TimeScale > 0f;
                        if (GUILayout.Button("\u25BA", active ? activePlayButtonStyle : idlePlayButtonStyle, GUILayout.Width(24))) {
                            preview.PlayPauseAnimation(animation.Name, true);
                            activeTrack = preview.ActiveTrack;
                        }
                    } else {
                        GUILayout.Label("-", GUILayout.Width(24));
                    }
                    //string frameCountString = (fps > 0) ? ("(" + (Mathf.RoundToInt(animation.Duration * fps)) + ")").PadLeft(12, ' ') : string.Empty;
                    //EditorGUILayout.LabelField(new GUIContent(animation.Name, Icons.animation), SpineInspectorUtility.TempContent(animation.Duration.ToString("f3") + "s" + frameCountString));
                    string durationString = animation.Duration.ToString("f3");
                    EditorGUILayout.LabelField(new GUIContent(animation.Name, Icons.animation), SpineInspectorUtility.TempContent(durationString + "s", tooltip: string.Format("{0} seconds\n{1} timelines", durationString, animation.Timelines.Count)));
                }
            }
        }
 
        void DrawSlotList () {
            showSlotList = EditorGUILayout.Foldout(showSlotList, SpineInspectorUtility.TempContent("Slots", Icons.slotRoot));
 
            if (!showSlotList) return;
            if (!preview.IsValid) return;
 
            Skin defaultSkin = targetSkeletonData.DefaultSkin;
            Skin skin = preview.Skeleton.Skin ?? defaultSkin;
 
            using (new SpineInspectorUtility.IndentScope()) {
 
                using (new EditorGUILayout.HorizontalScope()) {
                    showAttachments = EditorGUILayout.ToggleLeft("Show Attachments", showAttachments, GUILayout.MaxWidth(150f));
                    if (showAttachments) {
                        if (skin != null) {
                            int attachmentCount = skin.Attachments.Count;
                            EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(string.Format("{0} ({1} attachment{2})", skin.Name, attachmentCount, SpineInspectorUtility.PluralThenS(attachmentCount)), Icons.skin));
                        }
 
                    }
                }
 
                List<Skin.SkinEntry> slotAttachments = new List<Skin.SkinEntry>();
                List<Skin.SkinEntry> defaultSkinAttachments = new List<Skin.SkinEntry>();
                Slot[] slotsItems = preview.Skeleton.Slots.Items;
                for (int i = preview.Skeleton.Slots.Count - 1; i >= 0; i--) {
                    Slot slot = slotsItems[i];
                    EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(slot.Data.Name, Icons.slot));
                    if (showAttachments) {
                        slotAttachments.Clear();
                        defaultSkinAttachments.Clear();
 
                        using (new SpineInspectorUtility.IndentScope()) {
                            {
                                skin.GetAttachments(i, slotAttachments);
                                if (defaultSkin != null) {
                                    if (skin != defaultSkin) {
                                        defaultSkin.GetAttachments(i, slotAttachments);
                                        defaultSkin.GetAttachments(i, defaultSkinAttachments);
                                    } else {
                                        defaultSkin.GetAttachments(i, defaultSkinAttachments);
                                    }
                                }
                            }
 
                            for (int a = 0; a < slotAttachments.Count; a++) {
                                Skin.SkinEntry skinEntry = slotAttachments[a];
                                Attachment attachment = skinEntry.Attachment;
                                string attachmentName = skinEntry.Name;
                                bool attachmentIsFromSkin = !defaultSkinAttachments.Contains(skinEntry);
 
                                Texture2D attachmentTypeIcon = Icons.GetAttachmentIcon(attachment);
                                bool initialState = slot.Attachment == attachment;
 
                                Texture2D iconToUse = attachmentIsFromSkin ? Icons.skinPlaceholder : attachmentTypeIcon;
                                bool toggled = EditorGUILayout.ToggleLeft(SpineInspectorUtility.TempContent(attachmentName, iconToUse), slot.Attachment == attachment, GUILayout.MinWidth(150f));
 
                                if (attachmentIsFromSkin) {
                                    Rect extraIconRect = GUILayoutUtility.GetLastRect();
                                    extraIconRect.x += extraIconRect.width - (attachmentTypeIcon.width * 2f);
                                    extraIconRect.width = attachmentTypeIcon.width;
                                    extraIconRect.height = attachmentTypeIcon.height;
                                    GUI.DrawTexture(extraIconRect, attachmentTypeIcon);
                                }
 
                                if (toggled != initialState) {
                                    slot.Attachment = toggled ? attachment : null;
                                    preview.RefreshOnNextUpdate();
                                }
                            }
                        }
 
                    }
                }
            }
 
        }
 
        void DrawUnityTools () {
#if SPINE_SKELETON_MECANIM
            using (new SpineInspectorUtility.BoxScope()) {
                isMecanimExpanded = EditorGUILayout.Foldout(isMecanimExpanded, SpineInspectorUtility.TempContent("SkeletonMecanim", SpineInspectorUtility.UnityIcon<SceneAsset>()));
                if (isMecanimExpanded) {
                    using (new SpineInspectorUtility.IndentScope()) {
                        EditorGUILayout.PropertyField(controller, SpineInspectorUtility.TempContent("Controller", SpineInspectorUtility.UnityIcon<Animator>()));
                        if (controller.objectReferenceValue == null) {
 
                            // Generate Mecanim Controller Button
                            using (new GUILayout.HorizontalScope()) {
                                GUILayout.Space(EditorGUIUtility.labelWidth);
                                if (GUILayout.Button(SpineInspectorUtility.TempContent("Generate Mecanim Controller"), GUILayout.Height(20)))
                                    SkeletonBaker.GenerateMecanimAnimationClips(targetSkeletonDataAsset);
                            }
                            EditorGUILayout.HelpBox("SkeletonMecanim is the Mecanim alternative to SkeletonAnimation.\nIt is not required.", MessageType.Info);
 
                        } else {
 
                            // Update AnimationClips button.
                            using (new GUILayout.HorizontalScope()) {
                                GUILayout.Space(EditorGUIUtility.labelWidth);
                                if (GUILayout.Button(SpineInspectorUtility.TempContent("Force Update AnimationClips"), GUILayout.Height(20)))
                                    SkeletonBaker.GenerateMecanimAnimationClips(targetSkeletonDataAsset);
                            }
 
                        }
                    }
                }
            }
#endif
        }
 
        void DrawWarningList () {
            foreach (string line in warnings)
                EditorGUILayout.LabelField(SpineInspectorUtility.TempContent(line, Icons.warning));
        }
 
        void PopulateWarnings () {
            warnings.Clear();
            compatibilityProblemInfo = null;
 
            if (skeletonJSON.objectReferenceValue == null) {
                warnings.Add("Missing Skeleton JSON");
            } else {
                TextAsset fieldValue = (TextAsset)skeletonJSON.objectReferenceValue;
                string problemDescription = null;
                if (!AssetUtility.IsSpineData(fieldValue, out compatibilityProblemInfo, ref problemDescription)) {
                    if (problemDescription != null)
                        warnings.Add(problemDescription);
                    else
                        warnings.Add("Skeleton data file is not a valid Spine JSON or binary file.");
                } else {
#if SPINE_TK2D
                    bool searchForSpineAtlasAssets = (compatibilityProblemInfo == null);
                    bool isSpriteCollectionNull = spriteCollection.objectReferenceValue == null;
                    if (!isSpriteCollectionNull) searchForSpineAtlasAssets = false;
#else
                    // Analysis disable once ConvertToConstant.Local
                    bool searchForSpineAtlasAssets = (compatibilityProblemInfo == null);
#endif
 
                    if (searchForSpineAtlasAssets) {
                        bool detectedNullAtlasEntry = false;
                        List<Atlas> atlasList = new List<Atlas>();
                        AtlasAssetBase[] actualAtlasAssets = targetSkeletonDataAsset.atlasAssets;
 
                        for (int i = 0; i < actualAtlasAssets.Length; i++) {
                            if (actualAtlasAssets[i] == null) {
                                detectedNullAtlasEntry = true;
                                break;
                            } else {
                                if (actualAtlasAssets[i].MaterialCount > 0)
                                    atlasList.Add(actualAtlasAssets[i].GetAtlas());
                            }
                        }
 
                        if (detectedNullAtlasEntry) {
                            warnings.Add("AtlasAsset elements should not be null.");
                        } else {
                            List<string> missingPaths = null;
                            if (atlasAssets.arraySize > 0) {
                                missingPaths = AssetUtility.GetRequiredAtlasRegions(AssetDatabase.GetAssetPath(skeletonJSON.objectReferenceValue));
                                foreach (Atlas atlas in atlasList) {
                                    if (atlas == null)
                                        continue;
                                    for (int i = 0; i < missingPaths.Count; i++) {
                                        if (atlas.FindRegionIgnoringNumberSuffix(missingPaths[i]) != null) {
                                            missingPaths.RemoveAt(i);
                                            i--;
                                        }
                                    }
                                }
 
#if SPINE_TK2D
                                if (missingPaths.Count > 0)
                                    warnings.Add("Missing regions. SkeletonData asset requires tk2DSpriteCollectionData or Spine AtlasAssets.");
#endif
                            }
 
                            if (missingPaths != null) {
                                foreach (string missingRegion in missingPaths)
                                    warnings.Add(string.Format("Missing Region: '{0}'", missingRegion));
                            }
 
                        }
                    }
 
                }
            }
        }
 
        void DoReimport () {
            AssetUtility.ImportSpineContent(new[] { AssetDatabase.GetAssetPath(skeletonJSON.objectReferenceValue) }, null, true);
            preview.Clear();
            InitializeEditor();
            EditorUtility.SetDirty(targetSkeletonDataAsset);
        }
 
        void HandlePreviewSkinChanged (string skinName) {
            EditorPrefs.SetString(LastSkinKey, skinName);
        }
 
        bool NoProblems () {
            return warnings.Count == 0 && compatibilityProblemInfo == null;
        }
 
        #region Preview Handlers
        void HandleOnDestroyPreview () {
            EditorApplication.update -= preview.HandleEditorUpdate;
            preview.OnDestroy();
        }
 
        override public bool HasPreviewGUI () {
            if (serializedObject.isEditingMultipleObjects)
                return false;
 
            for (int i = 0; i < atlasAssets.arraySize; i++) {
                SerializedProperty prop = atlasAssets.GetArrayElementAtIndex(i);
                if (prop.objectReferenceValue == null)
                    return false;
            }
 
            return skeletonJSON.objectReferenceValue != null;
        }
 
        override public void OnInteractivePreviewGUI (Rect r, GUIStyle background) {
            if (NoProblems()) {
                preview.Initialize(this.Repaint, targetSkeletonDataAsset, this.LastSkinName);
                preview.HandleInteractivePreviewGUI(r, background);
            }
        }
 
        override public GUIContent GetPreviewTitle () { return SpineInspectorUtility.TempContent("Preview"); }
        public override void OnPreviewSettings () { preview.HandleDrawSettings(); }
        public override Texture2D RenderStaticPreview (string assetPath, UnityEngine.Object[] subAssets, int width, int height) { return preview.GetStaticPreview(width, height); }
        #endregion
    }
 
    internal class SkeletonInspectorPreview {
        Color OriginColor = new Color(0.3f, 0.3f, 0.3f, 1);
        static readonly int SliderHash = "Slider".GetHashCode();
 
        SkeletonDataAsset skeletonDataAsset;
        SkeletonData skeletonData;
 
        SkeletonAnimation skeletonAnimation;
        GameObject previewGameObject;
        internal bool requiresRefresh;
        float animationLastTime;
 
        static float CurrentTime { get { return (float)EditorApplication.timeSinceStartup; } }
 
        Action Repaint;
        public event Action<string> OnSkinChanged;
 
        Texture previewTexture;
        PreviewRenderUtility previewRenderUtility;
        Camera PreviewUtilityCamera {
            get {
                if (previewRenderUtility == null) return null;
#if UNITY_2017_1_OR_NEWER
                return previewRenderUtility.camera;
#else
                return previewRenderUtility.m_Camera;
#endif
            }
        }
 
        static Vector3 lastCameraPositionGoal;
        static float lastCameraOrthoGoal;
        float cameraOrthoGoal = 1;
        Vector3 cameraPositionGoal = new Vector3(0, 0, -10);
        double cameraAdjustEndFrame = 0;
 
        List<Spine.Event> currentAnimationEvents = new List<Spine.Event>();
        List<float> currentAnimationEventTimes = new List<float>();
        List<SpineEventTooltip> currentAnimationEventTooltips = new List<SpineEventTooltip>();
 
        public bool IsValid { get { return skeletonAnimation != null && skeletonAnimation.valid; } }
 
        public Skeleton Skeleton { get { return IsValid ? skeletonAnimation.Skeleton : null; } }
 
        public float TimeScale {
            get { return IsValid ? skeletonAnimation.timeScale : 1f; }
            set { if (IsValid) skeletonAnimation.timeScale = value; }
        }
 
        public bool IsPlayingAnimation {
            get {
                if (!IsValid) return false;
                TrackEntry currentTrack = skeletonAnimation.AnimationState.GetCurrent(0);
                return currentTrack != null && currentTrack.TimeScale > 0;
            }
        }
 
        public TrackEntry ActiveTrack { get { return IsValid ? skeletonAnimation.AnimationState.GetCurrent(0) : null; } }
 
        public Vector3 PreviewCameraPosition {
            get { return PreviewUtilityCamera.transform.position; }
            set { PreviewUtilityCamera.transform.position = value; }
        }
 
        public void HandleDrawSettings () {
            const float SliderWidth = 150;
            const float SliderSnap = 0.25f;
            const float SliderMin = 0f;
            const float SliderMax = 2f;
 
            if (IsValid) {
                float timeScale = GUILayout.HorizontalSlider(TimeScale, SliderMin, SliderMax, GUILayout.MaxWidth(SliderWidth));
                timeScale = Mathf.RoundToInt(timeScale / SliderSnap) * SliderSnap;
                TimeScale = timeScale;
            }
        }
 
        public void HandleEditorUpdate () {
            AdjustCamera();
            if (IsPlayingAnimation) {
                RefreshOnNextUpdate();
                Repaint();
            } else if (requiresRefresh) {
                Repaint();
            }
        }
 
        public void Initialize (Action repaintCallback, SkeletonDataAsset skeletonDataAsset, string skinName = "") {
            if (skeletonDataAsset == null) return;
            if (skeletonDataAsset.GetSkeletonData(false) == null) {
                DestroyPreviewGameObject();
                return;
            }
 
            this.Repaint = repaintCallback;
            this.skeletonDataAsset = skeletonDataAsset;
            this.skeletonData = skeletonDataAsset.GetSkeletonData(false);
 
            if (skeletonData == null) {
                DestroyPreviewGameObject();
                return;
            }
 
            const int PreviewLayer = 30;
            const int PreviewCameraCullingMask = 1 << PreviewLayer;
 
            if (previewRenderUtility == null) {
                previewRenderUtility = new PreviewRenderUtility(true);
                animationLastTime = CurrentTime;
 
                {
                    Camera c = this.PreviewUtilityCamera;
                    c.orthographic = true;
                    c.cullingMask = PreviewCameraCullingMask;
                    c.nearClipPlane = 0.01f;
                    c.farClipPlane = 1000f;
                    c.orthographicSize = lastCameraOrthoGoal;
                    c.transform.position = lastCameraPositionGoal;
                }
 
                DestroyPreviewGameObject();
            }
 
            if (previewGameObject == null) {
                try {
                    previewGameObject = EditorInstantiation.InstantiateSkeletonAnimation(skeletonDataAsset, skinName, useObjectFactory: false).gameObject;
 
                    if (previewGameObject != null) {
                        previewGameObject.hideFlags = HideFlags.HideAndDontSave;
                        previewGameObject.layer = PreviewLayer;
                        skeletonAnimation = previewGameObject.GetComponent<SkeletonAnimation>();
                        skeletonAnimation.initialSkinName = skinName;
                        skeletonAnimation.LateUpdate();
                        previewGameObject.GetComponent<Renderer>().enabled = false;
 
#if SPINE_UNITY_2018_PREVIEW_API
                        previewRenderUtility.AddSingleGO(previewGameObject);
#endif
                    }
 
                    if (this.ActiveTrack != null) cameraAdjustEndFrame = EditorApplication.timeSinceStartup + skeletonAnimation.AnimationState.GetCurrent(0).Alpha;
                    AdjustCameraGoals();
                } catch {
                    DestroyPreviewGameObject();
                }
 
                RefreshOnNextUpdate();
            }
        }
 
        public void HandleInteractivePreviewGUI (Rect r, GUIStyle background) {
            if (Event.current.type == EventType.Repaint) {
                if (requiresRefresh) {
                    previewRenderUtility.BeginPreview(r, background);
                    DoRenderPreview(true);
                    previewTexture = previewRenderUtility.EndPreview();
                    requiresRefresh = false;
                }
                if (previewTexture != null)
                    GUI.DrawTexture(r, previewTexture, ScaleMode.StretchToFill, false);
            }
 
            DrawSkinToolbar(r);
            //DrawSetupPoseButton(r);
            DrawTimeBar(r);
            HandleMouseScroll(r);
        }
 
        public Texture2D GetStaticPreview (int width, int height) {
            Camera c = this.PreviewUtilityCamera;
            if (c == null)
                return null;
 
            RefreshOnNextUpdate();
            AdjustCameraGoals();
            c.orthographicSize = cameraOrthoGoal / 2;
            c.transform.position = cameraPositionGoal;
            previewRenderUtility.BeginStaticPreview(new Rect(0, 0, width, height));
            DoRenderPreview(false);
            Texture2D tex = previewRenderUtility.EndStaticPreview();
 
            return tex;
        }
 
        public void DoRenderPreview (bool drawHandles) {
            if (this.PreviewUtilityCamera.activeTexture == null || this.PreviewUtilityCamera.targetTexture == null)
                return;
 
            GameObject go = previewGameObject;
            if (requiresRefresh && go != null) {
                Renderer renderer = go.GetComponent<Renderer>();
                renderer.enabled = true;
 
 
                if (!EditorApplication.isPlaying) {
                    float current = CurrentTime;
                    float deltaTime = (current - animationLastTime);
                    skeletonAnimation.Update(deltaTime);
                    animationLastTime = current;
                    skeletonAnimation.LateUpdate();
                }
 
                Camera thisPreviewUtilityCamera = this.PreviewUtilityCamera;
 
                if (drawHandles) {
                    Handles.SetCamera(thisPreviewUtilityCamera);
                    Handles.color = OriginColor;
 
                    // Draw Cross
                    float scale = skeletonDataAsset.scale;
                    float cl = 1000 * scale;
                    Handles.DrawLine(new Vector3(-cl, 0), new Vector3(cl, 0));
                    Handles.DrawLine(new Vector3(0, cl), new Vector3(0, -cl));
                }
 
                thisPreviewUtilityCamera.Render();
 
                if (drawHandles) {
                    Handles.SetCamera(thisPreviewUtilityCamera);
                    SpineHandles.DrawBoundingBoxes(skeletonAnimation.transform, skeletonAnimation.skeleton);
                    if (SkeletonDataAssetInspector.showAttachments)
                        SpineHandles.DrawPaths(skeletonAnimation.transform, skeletonAnimation.skeleton);
                }
 
                renderer.enabled = false;
            }
        }
 
        public void AdjustCamera () {
            if (previewRenderUtility == null)
                return;
 
            if (CurrentTime < cameraAdjustEndFrame)
                AdjustCameraGoals();
 
            lastCameraPositionGoal = cameraPositionGoal;
            lastCameraOrthoGoal = cameraOrthoGoal;
 
            Camera c = this.PreviewUtilityCamera;
            float orthoSet = Mathf.Lerp(c.orthographicSize, cameraOrthoGoal, 0.1f);
 
            c.orthographicSize = orthoSet;
 
            float dist = Vector3.Distance(c.transform.position, cameraPositionGoal);
            if (dist > 0f) {
                Vector3 pos = Vector3.Lerp(c.transform.position, cameraPositionGoal, 0.1f);
                pos.x = 0;
                c.transform.position = pos;
                c.transform.rotation = Quaternion.identity;
                RefreshOnNextUpdate();
            }
        }
 
        void AdjustCameraGoals () {
            if (previewGameObject == null) return;
 
            Bounds bounds = previewGameObject.GetComponent<Renderer>().bounds;
            cameraOrthoGoal = bounds.size.y;
            cameraPositionGoal = bounds.center + new Vector3(0, 0, -10f);
        }
 
        void HandleMouseScroll (Rect position) {
            Event current = Event.current;
            int controlID = GUIUtility.GetControlID(SliderHash, FocusType.Passive);
            switch (current.GetTypeForControl(controlID)) {
            case EventType.ScrollWheel:
                if (position.Contains(current.mousePosition)) {
                    cameraOrthoGoal += current.delta.y * 0.06f;
                    cameraOrthoGoal = Mathf.Max(0.01f, cameraOrthoGoal);
                    GUIUtility.hotControl = controlID;
                    current.Use();
                }
                break;
            }
        }
 
        public void RefreshOnNextUpdate () {
            requiresRefresh = true;
        }
 
        public void ClearAnimationSetupPose () {
            if (skeletonAnimation == null) {
                Debug.LogWarning("Animation was stopped but preview doesn't exist. It's possible that the Preview Panel is closed.");
            }
 
            skeletonAnimation.AnimationState.ClearTracks();
            skeletonAnimation.Skeleton.SetToSetupPose();
        }
 
        public void PlayPauseAnimation (string animationName, bool loop) {
            if (skeletonData == null) return;
 
            if (skeletonAnimation == null) {
                //Debug.LogWarning("Animation was stopped but preview doesn't exist. It's possible that the Preview Panel is closed.");
                return;
            }
 
            if (!skeletonAnimation.valid) return;
 
            if (string.IsNullOrEmpty(animationName)) {
                skeletonAnimation.Skeleton.SetToSetupPose();
                skeletonAnimation.AnimationState.ClearTracks();
                return;
            }
 
            Animation targetAnimation = skeletonData.FindAnimation(animationName);
            if (targetAnimation != null) {
                TrackEntry currentTrack = this.ActiveTrack;
                bool isEmpty = (currentTrack == null);
                bool isNewAnimation = isEmpty || currentTrack.Animation != targetAnimation;
 
                Skeleton skeleton = skeletonAnimation.Skeleton;
                AnimationState animationState = skeletonAnimation.AnimationState;
 
                if (isEmpty) {
                    skeleton.SetToSetupPose();
                    animationState.SetAnimation(0, targetAnimation, loop);
                } else {
                    bool sameAnimation = (currentTrack.Animation == targetAnimation);
                    if (sameAnimation) {
                        currentTrack.TimeScale = (currentTrack.TimeScale == 0) ? 1f : 0f; // pause/play
                    } else {
                        currentTrack.TimeScale = 1f;
                        animationState.SetAnimation(0, targetAnimation, loop);
                    }
                }
 
                if (isNewAnimation) {
                    currentAnimationEvents.Clear();
                    currentAnimationEventTimes.Clear();
                    foreach (Timeline timeline in targetAnimation.Timelines) {
                        EventTimeline eventTimeline = timeline as EventTimeline;
                        if (eventTimeline != null) {
                            for (int i = 0; i < eventTimeline.Events.Length; i++) {
                                currentAnimationEvents.Add(eventTimeline.Events[i]);
                                currentAnimationEventTimes.Add(eventTimeline.Frames[i]);
                            }
                        }
                    }
                }
            } else {
                Debug.LogFormat("The Spine.Animation named '{0}' was not found for this Skeleton.", animationName);
            }
 
        }
 
        void DrawSkinToolbar (Rect r) {
            if (!this.IsValid) return;
 
            Skeleton skeleton = this.Skeleton;
            string label = (skeleton.Skin != null) ? skeleton.Skin.Name : "default";
 
            Rect popRect = new Rect(r);
            popRect.y += 32;
            popRect.x += 4;
            popRect.height = 24;
            popRect.width = 40;
            EditorGUI.DropShadowLabel(popRect, SpineInspectorUtility.TempContent("Skin"));
 
            popRect.y += 11;
            popRect.width = 150;
            popRect.x += 44;
 
            if (GUI.Button(popRect, SpineInspectorUtility.TempContent(label, Icons.skin), EditorStyles.popup)) {
                DrawSkinDropdown();
            }
        }
 
        void DrawSetupPoseButton (Rect r) {
            if (!this.IsValid)
                return;
 
            Skeleton skeleton = this.Skeleton;
 
            Rect popRect = new Rect(r);
            popRect.y += 64;
            popRect.x += 4;
            popRect.height = 24;
            popRect.width = 40;
 
            //popRect.y += 11;
            popRect.width = 150;
            //popRect.x += 44;
 
            if (GUI.Button(popRect, SpineInspectorUtility.TempContent("Reset to SetupPose", Icons.skeleton))) {
                ClearAnimationSetupPose();
                RefreshOnNextUpdate();
            }
        }
 
        void DrawSkinDropdown () {
            GenericMenu menu = new GenericMenu();
            foreach (Skin s in skeletonData.Skins)
                menu.AddItem(new GUIContent(s.Name, Icons.skin), skeletonAnimation.skeleton.Skin == s, HandleSkinDropdownSelection, s);
 
            menu.ShowAsContext();
        }
 
        void HandleSkinDropdownSelection (object o) {
            Skin skin = (Skin)o;
            skeletonAnimation.initialSkinName = skin.Name;
            skeletonAnimation.Initialize(true);
            RefreshOnNextUpdate();
            if (OnSkinChanged != null) OnSkinChanged(skin.Name);
        }
 
        void DrawTimeBar (Rect r) {
            if (skeletonAnimation == null)
                return;
 
            Rect barRect = new Rect(r);
            barRect.height = 32;
            barRect.x += 4;
            barRect.width -= 4;
 
            GUI.Box(barRect, "");
 
            Rect lineRect = new Rect(barRect);
            float lineRectWidth = lineRect.width;
            TrackEntry t = skeletonAnimation.AnimationState.GetCurrent(0);
 
            if (t != null && Icons.userEvent != null) { // when changing to play mode, Icons.userEvent  will not be reset
                int loopCount = (int)(t.TrackTime / t.TrackEnd);
                float currentTime = t.TrackTime - (t.TrackEnd * loopCount);
                float normalizedTime = currentTime / t.Animation.Duration;
                float wrappedTime = normalizedTime % 1f;
 
                lineRect.x = barRect.x + (lineRectWidth * wrappedTime) - 0.5f;
                lineRect.width = 2;
 
                GUI.color = Color.red;
                GUI.DrawTexture(lineRect, EditorGUIUtility.whiteTexture);
                GUI.color = Color.white;
 
                currentAnimationEventTooltips = currentAnimationEventTooltips ?? new List<SpineEventTooltip>();
                currentAnimationEventTooltips.Clear();
                for (int i = 0; i < currentAnimationEvents.Count; i++) {
                    float eventTime = currentAnimationEventTimes[i];
                    Texture2D userEventIcon = Icons.userEvent;
                    float iconX = Mathf.Max(((eventTime / t.Animation.Duration) * lineRectWidth) - (userEventIcon.width / 2), barRect.x);
                    float iconY = barRect.y + userEventIcon.height;
                    Rect evRect = new Rect(barRect) {
                        x = iconX,
                        y = iconY,
                        width = userEventIcon.width,
                        height = userEventIcon.height
                    };
                    GUI.DrawTexture(evRect, userEventIcon);
                    Event ev = Event.current;
                    if (ev.type == EventType.Repaint) {
                        if (evRect.Contains(ev.mousePosition)) {
                            string eventName = currentAnimationEvents[i].Data.Name;
                            Rect tooltipRect = new Rect(evRect) {
                                width = EditorStyles.helpBox.CalcSize(new GUIContent(eventName)).x
                            };
                            tooltipRect.y -= 4;
                            tooltipRect.y -= tooltipRect.height * currentAnimationEventTooltips.Count; // Avoid several overlapping tooltips.
                            tooltipRect.x += 4;
 
                            // Handle tooltip overflowing to the right.
                            float rightEdgeOverflow = (tooltipRect.x + tooltipRect.width) - (barRect.x + barRect.width);
                            if (rightEdgeOverflow > 0)
                                tooltipRect.x -= rightEdgeOverflow;
 
                            currentAnimationEventTooltips.Add(new SpineEventTooltip { rect = tooltipRect, text = eventName });
                        }
                    }
                }
 
                // Draw tooltips.
                for (int i = 0; i < currentAnimationEventTooltips.Count; i++) {
                    GUI.Label(currentAnimationEventTooltips[i].rect, currentAnimationEventTooltips[i].text, EditorStyles.helpBox);
                    GUI.tooltip = currentAnimationEventTooltips[i].text;
                }
            }
        }
 
        public void OnDestroy () {
            DisposePreviewRenderUtility();
            DestroyPreviewGameObject();
        }
 
        public void Clear () {
            DisposePreviewRenderUtility();
            DestroyPreviewGameObject();
        }
 
        void DisposePreviewRenderUtility () {
            if (previewRenderUtility != null) {
                previewRenderUtility.Cleanup();
                previewRenderUtility = null;
            }
        }
 
        void DestroyPreviewGameObject () {
            if (previewGameObject != null) {
                GameObject.DestroyImmediate(previewGameObject);
                previewGameObject = null;
            }
        }
 
        internal struct SpineEventTooltip {
            public Rect rect;
            public string text;
        }
    }
 
}