三国卡牌客户端基础资源仓库
yyl
4 天以前 cec146fc3fe287928e075c79ece20a20a9b16b20
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
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
/******************************************************************************
 * 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.
 *****************************************************************************/
 
#if UNITY_2018_3 || UNITY_2019 || UNITY_2018_3_OR_NEWER
#define NEW_PREFAB_SYSTEM
#endif
 
#define SPINE_SKELETONMECANIM
 
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEditor;
using UnityEngine;
 
namespace Spine.Unity.Editor {
 
    /// <summary>
    /// [SUPPORTS]
    /// Linear, Constant, and Bezier Curves*
    /// Inverse Kinematics*
    /// Disabled Inherit Rotation, unless combined with negative scale
    /// Translate Timeline
    /// Rotate Timeline
    /// Scale Timeline**
    /// Event Timeline***
    /// Attachment Timeline
    ///
    /// RegionAttachment
    /// MeshAttachment (optionally Skinned)
    ///
    /// [LIMITATIONS]
    /// *Bezier Curves are baked into the animation at 60fps and are not realtime. Use bakeIncrement constant to adjust key density if desired.
    /// *Inverse Kinematics is baked into the animation at 60fps and are not realtime. Use bakeIncrement constant to adjust key density if desired.
    /// ***Events may only fire 1 type of data per event in Unity safely so priority to String data if present in Spine key, otherwise a Float is sent whether the Spine key was Int or Float with priority given to Int.
    ///
    /// [DOES NOT SUPPORT]
    /// FFD (Unity does not provide access to BlendShapes with code)
    /// Color Keys (Maybe one day when Unity supports full FBX standard and provides access with code)
    /// Draw Order Keyframes
    /// </summary>
    public static class SkeletonBaker {
 
        const string SpineEventStringId = "SpineEvent";
        const float EventTimeEqualityEpsilon = 0.01f;
 
        #region SkeletonMecanim's Mecanim Clips
#if SPINE_SKELETONMECANIM
        public static void UpdateMecanimClips (SkeletonDataAsset skeletonDataAsset) {
            if (skeletonDataAsset.controller == null)
                return;
 
            SkeletonBaker.GenerateMecanimAnimationClips(skeletonDataAsset);
        }
 
        public static void GenerateMecanimAnimationClips (SkeletonDataAsset skeletonDataAsset) {
            SkeletonData data = skeletonDataAsset.GetSkeletonData(true);
            if (data == null) {
                Debug.LogError("SkeletonData loading failed!", skeletonDataAsset);
                return;
            }
 
            string dataPath = AssetDatabase.GetAssetPath(skeletonDataAsset);
            string controllerPath = dataPath.Replace(AssetUtility.SkeletonDataSuffix, "_Controller").Replace(".asset", ".controller");
            UnityEditor.Animations.AnimatorController controller;
            if (skeletonDataAsset.controller != null) {
                controller = (UnityEditor.Animations.AnimatorController)skeletonDataAsset.controller;
                controllerPath = AssetDatabase.GetAssetPath(controller);
            } else {
                if (File.Exists(controllerPath)) {
                    if (EditorUtility.DisplayDialog("Controller Overwrite Warning", "Unknown Controller already exists at: " + controllerPath, "Update", "Overwrite")) {
                        controller = (UnityEditor.Animations.AnimatorController)AssetDatabase.LoadAssetAtPath(controllerPath, typeof(RuntimeAnimatorController));
                    } else {
                        controller = (UnityEditor.Animations.AnimatorController)UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
                    }
                } else {
                    controller = (UnityEditor.Animations.AnimatorController)UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
                }
 
            }
 
            skeletonDataAsset.controller = controller;
            EditorUtility.SetDirty(skeletonDataAsset);
 
            UnityEngine.Object[] objs = AssetDatabase.LoadAllAssetsAtPath(controllerPath);
 
            Dictionary<string, AnimationClip> unityAnimationClipTable = new Dictionary<string, AnimationClip>();
            Dictionary<string, Animation> spineAnimationTable = new Dictionary<string, Spine.Animation>();
 
            foreach (UnityEngine.Object o in objs) {
                //Debug.LogFormat("({0}){1} : {3} + {2} + {4}", o.GetType(), o.name, o.hideFlags, o.GetInstanceID(), o.GetHashCode());
                // There is a bug in Unity 5.3.3 (and likely before) that creates
                // a duplicate AnimationClip when you duplicate a Mecanim Animator State.
                // These duplicates seem to be identifiable by their HideFlags, so we'll exclude them.
                if (o is AnimationClip) {
                    AnimationClip clip = o as AnimationClip;
                    if (!clip.HasFlag(HideFlags.HideInHierarchy)) {
                        if (unityAnimationClipTable.ContainsKey(clip.name)) {
                            Debug.LogWarningFormat("Duplicate AnimationClips were found named {0}", clip.name);
                        }
                        unityAnimationClipTable.Add(clip.name, clip);
                    }
                }
            }
 
            SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(true);
            float fps = skeletonData != null ? skeletonData.Fps : 0.0f;
            foreach (Animation animations in data.Animations) {
                string animationName = animations.Name; // Review for unsafe names. Requires runtime implementation too.
                spineAnimationTable.Add(animationName, animations);
 
                if (unityAnimationClipTable.ContainsKey(animationName) == false) {
                    AnimationClip newClip = new AnimationClip {
                        name = animationName
                    };
                    //AssetDatabase.CreateAsset(newClip, Path.GetDirectoryName(dataPath) + "/" + animationName + ".asset");
                    AssetDatabase.AddObjectToAsset(newClip, controller);
                    unityAnimationClipTable.Add(animationName, newClip);
                }
 
                AnimationClip clip = unityAnimationClipTable[animationName];
                clip.frameRate = fps;
                clip.SetCurve("", typeof(GameObject), "dummy", AnimationCurve.Linear(0, 0, animations.Duration, 0));
                AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
                settings.stopTime = animations.Duration;
                SetAnimationSettings(clip, settings);
 
                AnimationEvent[] previousAnimationEvents = AnimationUtility.GetAnimationEvents(clip);
                List<AnimationEvent> animationEvents = new List<AnimationEvent>();
                foreach (Timeline t in animations.Timelines) {
                    if (t is EventTimeline)
                        ParseEventTimeline(ref animationEvents, (EventTimeline)t, SendMessageOptions.DontRequireReceiver);
                }
                AddPreviousUserEvents(ref animationEvents, previousAnimationEvents);
                AnimationUtility.SetAnimationEvents(clip, animationEvents.ToArray());
 
                EditorUtility.SetDirty(clip);
                unityAnimationClipTable.Remove(animationName);
            }
 
            foreach (AnimationClip clip in unityAnimationClipTable.Values) {
                AnimationClip.DestroyImmediate(clip, true);
            }
 
            AssetDatabase.Refresh();
            AssetDatabase.SaveAssets();
        }
 
        static bool HasFlag (this UnityEngine.Object o, HideFlags flagToCheck) {
            return (o.hideFlags & flagToCheck) == flagToCheck;
        }
#endif
        #endregion
 
        #region Prefab and AnimationClip Baking
        /// <summary>
        /// Interval between key sampling for Bezier curves, IK controlled bones, and Inherit Rotation effected bones.
        /// </summary>
        const float BakeIncrement = 1 / 60f;
 
        public static void BakeToPrefab (SkeletonDataAsset skeletonDataAsset, ExposedList<Skin> skins, string outputPath = "", bool bakeAnimations = true, bool bakeIK = true, SendMessageOptions eventOptions = SendMessageOptions.DontRequireReceiver) {
            if (skeletonDataAsset == null || skeletonDataAsset.GetSkeletonData(true) == null) {
                Debug.LogError("Could not export Spine Skeleton because SkeletonData Asset is null or invalid!");
                return;
            }
 
            if (outputPath == "") {
                outputPath = System.IO.Path.GetDirectoryName(AssetDatabase.GetAssetPath(skeletonDataAsset)).Replace('\\', '/') + "/Baked";
                System.IO.Directory.CreateDirectory(outputPath);
            }
 
            SkeletonData skeletonData = skeletonDataAsset.GetSkeletonData(true);
            bool hasAnimations = bakeAnimations && skeletonData.Animations.Count > 0;
            UnityEditor.Animations.AnimatorController controller = null;
            if (hasAnimations) {
                string controllerPath = outputPath + "/" + skeletonDataAsset.skeletonJSON.name + " Controller.controller";
                bool newAnimContainer = false;
 
                UnityEngine.Object runtimeController = AssetDatabase.LoadAssetAtPath(controllerPath, typeof(RuntimeAnimatorController));
 
                if (runtimeController != null) {
                    controller = (UnityEditor.Animations.AnimatorController)runtimeController;
                } else {
                    controller = UnityEditor.Animations.AnimatorController.CreateAnimatorControllerAtPath(controllerPath);
                    newAnimContainer = true;
                }
 
                Dictionary<string, AnimationClip> existingClipTable = new Dictionary<string, AnimationClip>();
                List<string> unusedClipNames = new List<string>();
                Object[] animObjs = AssetDatabase.LoadAllAssetsAtPath(controllerPath);
 
                foreach (Object o in animObjs) {
                    if (o is AnimationClip) {
                        AnimationClip clip = (AnimationClip)o;
                        existingClipTable.Add(clip.name, clip);
                        unusedClipNames.Add(clip.name);
                    }
                }
 
                Dictionary<int, List<string>> slotLookup = new Dictionary<int, List<string>>();
 
                int skinCount = skins.Count;
 
                for (int s = 0; s < skeletonData.Slots.Count; s++) {
                    List<string> attachmentNames = new List<string>();
                    for (int i = 0; i < skinCount; i++) {
                        Skin skin = skins.Items[i];
                        List<Skin.SkinEntry> skinEntries = new List<Skin.SkinEntry>();
                        skin.GetAttachments(s, skinEntries);
                        foreach (Skin.SkinEntry entry in skinEntries) {
                            if (!attachmentNames.Contains(entry.Name))
                                attachmentNames.Add(entry.Name);
                        }
                    }
                    slotLookup.Add(s, attachmentNames);
                }
 
                foreach (Animation anim in skeletonData.Animations) {
 
                    AnimationClip clip = null;
                    if (existingClipTable.ContainsKey(anim.Name)) {
                        clip = existingClipTable[anim.Name];
                    }
 
                    clip = ExtractAnimation(anim.Name, skeletonData, slotLookup, bakeIK, eventOptions, clip);
 
                    if (unusedClipNames.Contains(clip.name)) {
                        unusedClipNames.Remove(clip.name);
                    } else {
                        AssetDatabase.AddObjectToAsset(clip, controller);
                        controller.AddMotion(clip);
                    }
                }
 
                if (newAnimContainer) {
                    EditorUtility.SetDirty(controller);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.ImportAsset(controllerPath, ImportAssetOptions.ForceUpdate);
                    AssetDatabase.Refresh();
                } else {
 
                    foreach (string str in unusedClipNames) {
                        AnimationClip.DestroyImmediate(existingClipTable[str], true);
                    }
 
                    EditorUtility.SetDirty(controller);
                    AssetDatabase.SaveAssets();
                    AssetDatabase.ImportAsset(controllerPath, ImportAssetOptions.ForceUpdate);
                    AssetDatabase.Refresh();
                }
            }
 
            foreach (Skin skin in skins) {
                bool newPrefab = false;
 
                string prefabPath = outputPath + "/" + skeletonDataAsset.skeletonJSON.name + " (" + skin.Name + ").prefab";
 
                Object prefab = AssetDatabase.LoadAssetAtPath(prefabPath, typeof(GameObject));
 
                if (prefab == null) {
#if NEW_PREFAB_SYSTEM
                    GameObject emptyGameObject = new GameObject();
                    prefab = PrefabUtility.SaveAsPrefabAssetAndConnect(emptyGameObject, prefabPath, InteractionMode.AutomatedAction);
                    GameObject.DestroyImmediate(emptyGameObject);
#else
                    prefab = PrefabUtility.CreateEmptyPrefab(prefabPath);
#endif
                    newPrefab = true;
                }
 
                Dictionary<string, Mesh> meshTable = new Dictionary<string, Mesh>();
                List<string> unusedMeshNames = new List<string>();
                UnityEngine.Object[] assets = AssetDatabase.LoadAllAssetsAtPath(prefabPath);
                foreach (UnityEngine.Object obj in assets) {
                    if (obj is Mesh) {
                        meshTable.Add(obj.name, (Mesh)obj);
                        unusedMeshNames.Add(obj.name);
                    }
                }
 
                GameObject prefabRoot = EditorInstantiation.NewGameObject("root", true);
 
                Dictionary<string, Transform> slotTable = new Dictionary<string, Transform>();
                Dictionary<string, Transform> boneTable = new Dictionary<string, Transform>();
                List<Transform> boneList = new List<Transform>();
 
                //create bones
                for (int i = 0; i < skeletonData.Bones.Count; i++) {
                    BoneData boneData = skeletonData.Bones.Items[i];
                    Transform boneTransform = EditorInstantiation.NewGameObject(boneData.Name, true).transform;
                    boneTransform.parent = prefabRoot.transform;
                    boneTable.Add(boneTransform.name, boneTransform);
                    boneList.Add(boneTransform);
                }
 
                for (int i = 0; i < skeletonData.Bones.Count; i++) {
 
                    BoneData boneData = skeletonData.Bones.Items[i];
                    Transform boneTransform = boneTable[boneData.Name];
                    Transform parentTransform = null;
                    if (i > 0)
                        parentTransform = boneTable[boneData.Parent.Name];
                    else
                        parentTransform = boneTransform.parent;
 
                    boneTransform.parent = parentTransform;
                    boneTransform.localPosition = new Vector3(boneData.X, boneData.Y, 0);
                    Inherit inherit = boneData.Inherit;
                    if (inherit.InheritsRotation())
                        boneTransform.localRotation = Quaternion.Euler(0, 0, boneData.Rotation);
                    else
                        boneTransform.rotation = Quaternion.Euler(0, 0, boneData.Rotation);
 
                    if (inherit.InheritsScale())
                        boneTransform.localScale = new Vector3(boneData.ScaleX, boneData.ScaleY, 1);
                }
 
                //create slots and attachments
                for (int slotIndex = 0; slotIndex < skeletonData.Slots.Count; slotIndex++) {
                    SlotData slotData = skeletonData.Slots.Items[slotIndex];
                    Transform slotTransform = EditorInstantiation.NewGameObject(slotData.Name, true).transform;
                    slotTransform.parent = prefabRoot.transform;
                    slotTable.Add(slotData.Name, slotTransform);
 
                    List<Skin.SkinEntry> skinEntries = new List<Skin.SkinEntry>();
                    skin.GetAttachments(slotIndex, skinEntries);
                    if (skin != skeletonData.DefaultSkin)
                        skeletonData.DefaultSkin.GetAttachments(slotIndex, skinEntries);
 
                    for (int a = 0; a < skinEntries.Count; a++) {
                        Attachment attachment = skinEntries[a].Attachment;
                        string attachmentName = skinEntries[a].Name;
                        string attachmentMeshName = "[" + slotData.Name + "] " + attachmentName;
                        Vector3 offset = Vector3.zero;
                        float rotation = 0;
                        Mesh mesh = null;
                        Material material = null;
                        bool isWeightedMesh = false;
 
                        if (meshTable.ContainsKey(attachmentMeshName))
                            mesh = meshTable[attachmentMeshName];
                        if (attachment is RegionAttachment) {
                            RegionAttachment regionAttachment = (RegionAttachment)attachment;
                            offset.x = regionAttachment.X;
                            offset.y = regionAttachment.Y;
                            rotation = regionAttachment.Rotation;
                            mesh = ExtractRegionAttachment(attachmentMeshName, regionAttachment, mesh);
                            material = attachment.GetMaterial();
                            unusedMeshNames.Remove(attachmentMeshName);
                            if (newPrefab || meshTable.ContainsKey(attachmentMeshName) == false)
                                AssetDatabase.AddObjectToAsset(mesh, prefab);
                        } else if (attachment is MeshAttachment) {
                            MeshAttachment meshAttachment = (MeshAttachment)attachment;
                            isWeightedMesh = (meshAttachment.Bones != null);
                            offset.x = 0;
                            offset.y = 0;
                            rotation = 0;
 
                            if (isWeightedMesh)
                                mesh = ExtractWeightedMeshAttachment(attachmentMeshName, meshAttachment, slotIndex, skeletonData, boneList, mesh);
                            else
                                mesh = ExtractMeshAttachment(attachmentMeshName, meshAttachment, mesh);
 
                            material = attachment.GetMaterial();
                            unusedMeshNames.Remove(attachmentMeshName);
                            if (newPrefab || meshTable.ContainsKey(attachmentMeshName) == false)
                                AssetDatabase.AddObjectToAsset(mesh, prefab);
                        } else
                            continue;
 
                        Transform attachmentTransform = EditorInstantiation.NewGameObject(attachmentName, true).transform;
 
                        attachmentTransform.parent = slotTransform;
                        attachmentTransform.localPosition = offset;
                        attachmentTransform.localRotation = Quaternion.Euler(0, 0, rotation);
 
                        if (isWeightedMesh) {
                            attachmentTransform.position = Vector3.zero;
                            attachmentTransform.rotation = Quaternion.identity;
                            SkinnedMeshRenderer skinnedMeshRenderer = attachmentTransform.gameObject.AddComponent<SkinnedMeshRenderer>();
                            skinnedMeshRenderer.rootBone = boneList[0];
                            skinnedMeshRenderer.bones = boneList.ToArray();
                            skinnedMeshRenderer.sharedMesh = mesh;
                        } else {
                            attachmentTransform.gameObject.AddComponent<MeshFilter>().sharedMesh = mesh;
                            attachmentTransform.gameObject.AddComponent<MeshRenderer>();
                        }
 
                        attachmentTransform.GetComponent<Renderer>().sharedMaterial = material;
                        attachmentTransform.GetComponent<Renderer>().sortingOrder = slotIndex;
 
                        if (attachmentName != slotData.AttachmentName)
                            attachmentTransform.gameObject.SetActive(false);
                    }
 
                }
 
                foreach (SlotData slotData in skeletonData.Slots) {
                    Transform slotTransform = slotTable[slotData.Name];
                    slotTransform.parent = boneTable[slotData.BoneData.Name];
                    slotTransform.localPosition = Vector3.zero;
                    slotTransform.localRotation = Quaternion.identity;
                    slotTransform.localScale = Vector3.one;
                }
 
                if (hasAnimations) {
                    Animator animator = prefabRoot.AddComponent<Animator>();
                    animator.applyRootMotion = false;
                    animator.runtimeAnimatorController = (RuntimeAnimatorController)controller;
                    EditorGUIUtility.PingObject(controller);
                }
 
                if (newPrefab) {
#if NEW_PREFAB_SYSTEM
                    PrefabUtility.SaveAsPrefabAssetAndConnect(prefabRoot, prefabPath, InteractionMode.AutomatedAction);
#else
                    PrefabUtility.ReplacePrefab(prefabRoot, prefab, ReplacePrefabOptions.ConnectToPrefab);
#endif
                } else {
 
                    foreach (string str in unusedMeshNames) {
                        Mesh.DestroyImmediate(meshTable[str], true);
                    }
 
#if NEW_PREFAB_SYSTEM
                    PrefabUtility.SaveAsPrefabAssetAndConnect(prefabRoot, prefabPath, InteractionMode.AutomatedAction);
#else
                    PrefabUtility.ReplacePrefab(prefabRoot, prefab, ReplacePrefabOptions.ReplaceNameBased);
#endif
                }
 
 
                EditorGUIUtility.PingObject(prefab);
 
                AssetDatabase.Refresh();
                AssetDatabase.SaveAssets();
 
                GameObject.DestroyImmediate(prefabRoot);
 
            }
        }
 
        #region Attachment Baking
        static Bone DummyBone;
        static Slot DummySlot;
 
        internal static Bone GetDummyBone () {
            if (DummyBone != null)
                return DummyBone;
 
            SkeletonData skelData = new SkeletonData();
            BoneData data = new BoneData(0, "temp", null) {
                ScaleX = 1,
                ScaleY = 1,
                Length = 100
            };
 
            skelData.Bones.Add(data);
 
            Skeleton skeleton = new Skeleton(skelData);
 
            Bone bone = new Bone(data, skeleton, null);
            bone.UpdateWorldTransform();
 
            DummyBone = bone;
 
            return DummyBone;
        }
 
        internal static Slot GetDummySlot () {
            if (DummySlot != null)
                return DummySlot;
 
            Bone bone = GetDummyBone();
 
            SlotData data = new SlotData(0, "temp", bone.Data);
            Slot slot = new Slot(data, bone);
            DummySlot = slot;
            return DummySlot;
        }
 
        internal static Mesh ExtractRegionAttachment (string name, RegionAttachment attachment, Mesh mesh = null, bool centered = true) {
            Slot slot = GetDummySlot();
            Bone bone = slot.Bone;
 
            if (centered) {
                bone.X = -attachment.X;
                bone.Y = -attachment.Y;
            }
 
            bone.UpdateWorldTransform();
 
            float[] floatVerts = new float[8];
            attachment.ComputeWorldVertices(slot, floatVerts, 0);
            Vector2[] uvs = ExtractUV(attachment.UVs);
            Vector3[] verts = ExtractVerts(floatVerts);
 
            //unrotate verts now that they're centered
            if (centered) {
                for (int i = 0; i < verts.Length; i++)
                    verts[i] = Quaternion.Euler(0, 0, -attachment.Rotation) * verts[i];
            }
 
            int[] triangles = { 1, 3, 0, 2, 3, 1 };
            Color color = attachment.GetColor();
 
            if (mesh == null)
                mesh = new Mesh();
 
            mesh.triangles = new int[0];
 
            mesh.vertices = verts;
            mesh.uv = uvs;
            mesh.triangles = triangles;
            mesh.colors = new[] { color, color, color, color };
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
            mesh.name = name;
 
            return mesh;
        }
 
        internal static Mesh ExtractMeshAttachment (string name, MeshAttachment attachment, Mesh mesh = null) {
            Slot slot = GetDummySlot();
 
            slot.Bone.X = 0;
            slot.Bone.Y = 0;
            slot.Bone.UpdateWorldTransform();
 
            float[] floatVerts = new float[attachment.WorldVerticesLength];
            attachment.ComputeWorldVertices(slot, floatVerts);
            Vector2[] uvs = ExtractUV(attachment.UVs);
            Vector3[] verts = ExtractVerts(floatVerts);
 
            int[] triangles = attachment.Triangles;
            Color color = attachment.GetColor();
 
            if (mesh == null)
                mesh = new Mesh();
 
            mesh.triangles = new int[0];
 
            mesh.vertices = verts;
            mesh.uv = uvs;
            mesh.triangles = triangles;
            Color[] colors = new Color[verts.Length];
            for (int i = 0; i < verts.Length; i++)
                colors[i] = color;
 
            mesh.colors = colors;
            mesh.RecalculateBounds();
            mesh.RecalculateNormals();
            mesh.name = name;
 
            return mesh;
        }
 
        public class BoneWeightContainer {
            public struct Pair {
                public Transform bone;
                public float weight;
 
                public Pair (Transform bone, float weight) {
                    this.bone = bone;
                    this.weight = weight;
                }
            }
 
            public List<Transform> bones;
            public List<float> weights;
            public List<Pair> pairs;
 
 
            public BoneWeightContainer () {
                this.bones = new List<Transform>();
                this.weights = new List<float>();
                this.pairs = new List<Pair>();
            }
 
            public void Add (Transform transform, float weight) {
                bones.Add(transform);
                weights.Add(weight);
 
                pairs.Add(new Pair(transform, weight));
            }
        }
 
        internal static Mesh ExtractWeightedMeshAttachment (string name, MeshAttachment attachment, int slotIndex, SkeletonData skeletonData, List<Transform> boneList, Mesh mesh = null) {
            if (!attachment.IsWeighted())
                throw new System.ArgumentException("Mesh is not weighted.", "attachment");
 
            Skeleton skeleton = new Skeleton(skeletonData);
            skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
 
            float[] floatVerts = new float[attachment.WorldVerticesLength];
            attachment.ComputeWorldVertices(skeleton.Slots.Items[slotIndex], floatVerts);
            Vector2[] uvs = ExtractUV(attachment.UVs);
            Vector3[] verts = ExtractVerts(floatVerts);
 
            int[] triangles = attachment.Triangles;
            Color color = new Color(attachment.R, attachment.G, attachment.B, attachment.A);
 
            mesh = (mesh == null) ? new Mesh() : mesh;
 
            mesh.triangles = new int[0];
 
            mesh.vertices = verts;
            mesh.uv = uvs;
            mesh.triangles = triangles;
            Color[] colors = new Color[verts.Length];
            for (int i = 0; i < verts.Length; i++)
                colors[i] = color;
 
            mesh.colors = colors;
            mesh.name = name;
            mesh.RecalculateNormals();
            mesh.RecalculateBounds();
 
            // Handle weights and binding
            Dictionary<int, BoneWeightContainer> weightTable = new Dictionary<int, BoneWeightContainer>();
            StringBuilder warningBuilder = new System.Text.StringBuilder();
 
            int[] bones = attachment.Bones;
            float[] weights = attachment.Vertices;
            for (int w = 0, v = 0, b = 0, n = bones.Length; v < n; w += 2) {
 
                int nn = bones[v++] + v;
                for (; v < nn; v++, b += 3) {
                    Transform boneTransform = boneList[bones[v]];
                    int vIndex = w / 2;
                    BoneWeightContainer container;
                    if (weightTable.ContainsKey(vIndex))
                        container = weightTable[vIndex];
                    else {
                        container = new BoneWeightContainer();
                        weightTable.Add(vIndex, container);
                    }
 
                    float weight = weights[b + 2];
                    container.Add(boneTransform, weight);
                }
            }
 
            BoneWeight[] boneWeights = new BoneWeight[weightTable.Count];
 
            for (int i = 0; i < weightTable.Count; i++) {
                BoneWeight bw = new BoneWeight();
                BoneWeightContainer container = weightTable[i];
 
                List<BoneWeightContainer.Pair> pairs = container.pairs.OrderByDescending(pair => pair.weight).ToList();
 
                for (int b = 0; b < pairs.Count; b++) {
                    if (b > 3) {
                        if (warningBuilder.Length == 0)
                            warningBuilder.Insert(0, "[Weighted Mesh: " + name + "]\r\nUnity only supports 4 weight influences per vertex! The 4 strongest influences will be used.\r\n");
 
                        warningBuilder.AppendFormat("{0} ignored on vertex {1}!\r\n", pairs[b].bone.name, i);
                        continue;
                    }
 
                    int boneIndex = boneList.IndexOf(pairs[b].bone);
                    float weight = pairs[b].weight;
 
                    switch (b) {
                    case 0:
                        bw.boneIndex0 = boneIndex;
                        bw.weight0 = weight;
                        break;
                    case 1:
                        bw.boneIndex1 = boneIndex;
                        bw.weight1 = weight;
                        break;
                    case 2:
                        bw.boneIndex2 = boneIndex;
                        bw.weight2 = weight;
                        break;
                    case 3:
                        bw.boneIndex3 = boneIndex;
                        bw.weight3 = weight;
                        break;
                    }
                }
 
                boneWeights[i] = bw;
            }
 
            Matrix4x4[] bindPoses = new Matrix4x4[boneList.Count];
            for (int i = 0; i < boneList.Count; i++) {
                bindPoses[i] = boneList[i].worldToLocalMatrix;
            }
 
            mesh.boneWeights = boneWeights;
            mesh.bindposes = bindPoses;
 
            string warningString = warningBuilder.ToString();
            if (warningString.Length > 0)
                Debug.LogWarning(warningString);
 
 
            return mesh;
        }
 
        internal static Vector2[] ExtractUV (float[] floats) {
            Vector2[] arr = new Vector2[floats.Length / 2];
 
            for (int i = 0; i < floats.Length; i += 2) {
                arr[i / 2] = new Vector2(floats[i], floats[i + 1]);
            }
 
            return arr;
        }
 
        internal static Vector3[] ExtractVerts (float[] floats) {
            Vector3[] arr = new Vector3[floats.Length / 2];
 
            for (int i = 0; i < floats.Length; i += 2) {
                arr[i / 2] = new Vector3(floats[i], floats[i + 1], 0);// *scale;
            }
 
            return arr;
        }
        #endregion
 
        #region Animation Baking
        static AnimationClip ExtractAnimation (string name, SkeletonData skeletonData, Dictionary<int, List<string>> slotLookup, bool bakeIK, SendMessageOptions eventOptions, AnimationClip clip = null) {
            Animation animation = skeletonData.FindAnimation(name);
 
            ExposedList<Timeline> timelines = animation.Timelines;
 
            if (clip == null) {
                clip = new AnimationClip();
            } else {
                clip.ClearCurves();
                AnimationUtility.SetAnimationEvents(clip, new AnimationEvent[0]);
            }
 
            clip.name = name;
 
            Skeleton skeleton = new Skeleton(skeletonData);
 
            List<int> ignoreRotateTimelineIndexes = new List<int>();
 
            if (bakeIK) {
                foreach (IkConstraint i in skeleton.IkConstraints) {
                    foreach (Bone b in i.Bones) {
                        ignoreRotateTimelineIndexes.Add(b.Data.Index);
                        BakeBoneConstraints(b, animation, clip);
                    }
                }
            }
 
            foreach (Bone b in skeleton.Bones) {
                if (!b.Data.Inherit.InheritsRotation()) {
                    int index = b.Data.Index;
                    if (ignoreRotateTimelineIndexes.Contains(index) == false) {
                        ignoreRotateTimelineIndexes.Add(index);
                        BakeBoneConstraints(b, animation, clip);
                    }
                }
            }
 
            foreach (Timeline t in timelines) {
                skeleton.SetToSetupPose();
 
                if (t is ScaleTimeline) {
                    ParseScaleTimeline(skeleton, (ScaleTimeline)t, clip);
                } else if (t is ScaleXTimeline) {
                    ParseSingleSplitScaleTimeline(skeleton, (ScaleXTimeline)t, null, clip);
                } else if (t is ScaleYTimeline) {
                    ParseSingleSplitScaleTimeline(skeleton, null, (ScaleYTimeline)t, clip);
                } else if (t is TranslateTimeline) {
                    ParseTranslateTimeline(skeleton, (TranslateTimeline)t, clip);
                } else if (t is TranslateXTimeline) {
                    ParseSingleSplitTranslateTimeline(skeleton, (TranslateXTimeline)t, null, clip);
                } else if (t is TranslateYTimeline) {
                    ParseSingleSplitTranslateTimeline(skeleton, null, (TranslateYTimeline)t, clip);
                } else if (t is RotateTimeline) {
                    //bypass any rotation keys if they're going to get baked anyway to prevent localEulerAngles vs Baked collision
                    if (ignoreRotateTimelineIndexes.Contains(((RotateTimeline)t).BoneIndex) == false)
                        ParseRotateTimeline(skeleton, (RotateTimeline)t, clip);
                } else if (t is AttachmentTimeline) {
                    ParseAttachmentTimeline(skeleton, (AttachmentTimeline)t, slotLookup, clip);
                } else if (t is EventTimeline) {
                    ParseEventTimeline((EventTimeline)t, clip, eventOptions);
                }
 
            }
 
            AnimationClipSettings settings = AnimationUtility.GetAnimationClipSettings(clip);
            settings.loopTime = true;
            settings.stopTime = Mathf.Max(clip.length, 0.001f);
 
            SetAnimationSettings(clip, settings);
 
            clip.EnsureQuaternionContinuity();
 
            EditorUtility.SetDirty(clip);
 
            return clip;
        }
 
        internal static int Search (float[] frames, float time) {
            int n = frames.Length;
            for (int i = 1; i < n; i++)
                if (frames[i] > time) return i - 1;
            return n - 1;
        }
 
        static void BakeBoneConstraints (Bone bone, Spine.Animation animation, AnimationClip clip) {
            Skeleton skeleton = bone.Skeleton;
            bool inheritRotation = bone.Data.Inherit.InheritsRotation();
 
            animation.Apply(skeleton, 0, 0, false, null, 1f, MixBlend.Setup, MixDirection.In);
            skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
            float duration = animation.Duration;
 
            AnimationCurve curve = new AnimationCurve();
 
            List<Keyframe> keys = new List<Keyframe>();
 
            float rotation = bone.AppliedRotation;
            if (!inheritRotation)
                rotation = GetUninheritedAppliedRotation(bone);
 
            keys.Add(new Keyframe(0, rotation, 0, 0));
 
            int listIndex = 0;
 
            float r = rotation;
 
            int steps = Mathf.CeilToInt(duration / BakeIncrement);
 
            float currentTime = 0;
            float angle = rotation;
 
            for (int i = 1; i <= steps; i++) {
                currentTime += BakeIncrement;
                if (i == steps)
                    currentTime = duration;
 
                animation.Apply(skeleton, 0, currentTime, true, null, 1f, MixBlend.Setup, MixDirection.In);
                skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
 
                int pIndex = listIndex;
 
                Keyframe pk = keys[pIndex];
 
                pk = keys[pIndex];
 
                rotation = inheritRotation ? bone.AppliedRotation : GetUninheritedAppliedRotation(bone);
 
                angle += Mathf.DeltaAngle(angle, rotation);
 
                r = angle;
 
                float rOut = (r - pk.value) / (currentTime - pk.time);
 
                pk.outTangent = rOut;
 
                keys.Add(new Keyframe(currentTime, r, rOut, 0));
 
                keys[pIndex] = pk;
 
                listIndex++;
            }
 
            curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));
 
            string path = GetPath(bone.Data);
            string propertyName = "localEulerAnglesBaked";
 
            EditorCurveBinding xBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".x");
            AnimationUtility.SetEditorCurve(clip, xBind, new AnimationCurve());
            EditorCurveBinding yBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".y");
            AnimationUtility.SetEditorCurve(clip, yBind, new AnimationCurve());
            EditorCurveBinding zBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".z");
            AnimationUtility.SetEditorCurve(clip, zBind, curve);
        }
 
        static void ParseTranslateTimeline (Skeleton skeleton, TranslateTimeline timeline, AnimationClip clip) {
            BoneData boneData = skeleton.Data.Bones.Items[timeline.BoneIndex];
            Bone bone = skeleton.Bones.Items[timeline.BoneIndex];
 
            AnimationCurve xCurve = new AnimationCurve();
            AnimationCurve yCurve = new AnimationCurve();
            AnimationCurve zCurve = new AnimationCurve();
 
            float endTime = timeline.Frames[(timeline.FrameCount * TranslateTimeline.ENTRIES) - TranslateTimeline.ENTRIES];
 
            float currentTime = timeline.Frames[0];
 
            List<Keyframe> xKeys = new List<Keyframe>();
            List<Keyframe> yKeys = new List<Keyframe>();
 
            xKeys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[1] + boneData.X, 0, 0));
            yKeys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[2] + boneData.Y, 0, 0));
 
            int listIndex = 0;
            int frameIndex = 0;
            int f = TranslateTimeline.ENTRIES;
            float[] frames = timeline.Frames;
            skeleton.SetToSetupPose();
            float lastTime = 0;
            while (currentTime < endTime) {
                int pIndex = listIndex;
 
                float curveType = timeline.GetCurveType(frameIndex);
                if (curveType == 0) {
                    //linear
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
                    float x = frames[f + 1] + boneData.X;
                    float y = frames[f + 2] + boneData.Y;
 
                    float xOut = (x - px.value) / (time - px.time);
                    float yOut = (y - py.value) / (time - py.time);
 
                    px.outTangent = xOut;
                    py.outTangent = yOut;
 
                    xKeys.Add(new Keyframe(time, x, xOut, 0));
                    yKeys.Add(new Keyframe(time, y, yOut, 0));
 
                    xKeys[pIndex] = px;
                    yKeys[pIndex] = py;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else if (curveType == 1) {
                    //stepped
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
                    float x = frames[f + 1] + boneData.X;
                    float y = frames[f + 2] + boneData.Y;
 
                    float xOut = float.PositiveInfinity;
                    float yOut = float.PositiveInfinity;
 
                    px.outTangent = xOut;
                    py.outTangent = yOut;
 
                    xKeys.Add(new Keyframe(time, x, xOut, 0));
                    yKeys.Add(new Keyframe(time, y, yOut, 0));
 
                    xKeys[pIndex] = px;
                    yKeys[pIndex] = py;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else {
                    //bezier
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
 
                    int steps = Mathf.FloorToInt((time - px.time) / BakeIncrement);
 
                    for (int i = 1; i <= steps; i++) {
                        currentTime += BakeIncrement;
                        if (i == steps)
                            currentTime = time;
 
                        timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                        px = xKeys[listIndex];
                        py = yKeys[listIndex];
 
                        float xOut = (bone.X - px.value) / (currentTime - px.time);
                        float yOut = (bone.Y - py.value) / (currentTime - py.time);
 
                        px.outTangent = xOut;
                        py.outTangent = yOut;
 
                        xKeys.Add(new Keyframe(currentTime, bone.X, xOut, 0));
                        yKeys.Add(new Keyframe(currentTime, bone.Y, yOut, 0));
 
                        xKeys[listIndex] = px;
                        yKeys[listIndex] = py;
 
                        listIndex++;
                        lastTime = currentTime;
                    }
                }
 
                frameIndex++;
                f += TranslateTimeline.ENTRIES;
            }
 
            xCurve = EnsureCurveKeyCount(new AnimationCurve(xKeys.ToArray()));
            yCurve = EnsureCurveKeyCount(new AnimationCurve(yKeys.ToArray()));
 
 
 
            string path = GetPath(boneData);
            const string propertyName = "localPosition";
 
            clip.SetCurve(path, typeof(Transform), propertyName + ".x", xCurve);
            clip.SetCurve(path, typeof(Transform), propertyName + ".y", yCurve);
            clip.SetCurve(path, typeof(Transform), propertyName + ".z", zCurve);
        }
 
        /// <summary>Parses a single TranslateXTimeline or TranslateYTimeline.
        /// Only one of <c>timelineX</c> or <c>timelineY</c> shall be filled out, the other must be null.</summary>
        static void ParseSingleSplitTranslateTimeline (Skeleton skeleton,
            TranslateXTimeline timelineX, TranslateYTimeline timelineY, AnimationClip clip) {
 
            bool isXTimeline = timelineX != null;
            CurveTimeline1 timeline = isXTimeline ? timelineX : timelineY as CurveTimeline1;
            IBoneTimeline boneTimeline = isXTimeline ? timelineX : timelineY as IBoneTimeline;
 
            BoneData boneData = skeleton.Data.Bones.Items[boneTimeline.BoneIndex];
            Bone bone = skeleton.Bones.Items[boneTimeline.BoneIndex];
            float boneDataOffset = isXTimeline ? boneData.X : boneData.Y;
 
            AnimationCurve curve = new AnimationCurve();
            float endTime = timeline.Frames[(timeline.FrameCount * TranslateXTimeline.ENTRIES) - TranslateXTimeline.ENTRIES];
            float currentTime = timeline.Frames[0];
            List<Keyframe> keys = new List<Keyframe>();
            keys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[1] + boneDataOffset, 0, 0));
 
            int listIndex = 0;
            int frameIndex = 0;
            int f = TranslateXTimeline.ENTRIES;
            float[] frames = timeline.Frames;
            skeleton.SetToSetupPose();
            float lastTime = 0;
            while (currentTime < endTime) {
                int pIndex = listIndex;
 
                float curveType = timeline.GetCurveType(frameIndex);
                if (curveType == 0) {
                    //linear
                    Keyframe p = keys[pIndex];
 
                    float time = frames[f];
                    float value = frames[f + 1] + boneDataOffset;
                    float valueOut = (value - p.value) / (time - p.time);
                    p.outTangent = valueOut;
                    keys.Add(new Keyframe(time, value, valueOut, 0));
 
                    keys[pIndex] = p;
                    currentTime = time;
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else if (curveType == 1) {
                    //stepped
                    Keyframe p = keys[pIndex];
 
                    float time = frames[f];
                    float value = frames[f + 1] + boneDataOffset;
                    float valueOut = float.PositiveInfinity;
                    p.outTangent = valueOut;
                    keys.Add(new Keyframe(time, value, valueOut, 0));
 
                    keys[pIndex] = p;
                    currentTime = time;
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else {
                    //bezier
                    Keyframe p = keys[pIndex];
 
                    float time = frames[f];
 
                    int steps = Mathf.FloorToInt((time - p.time) / BakeIncrement);
 
                    for (int i = 1; i <= steps; i++) {
                        currentTime += BakeIncrement;
                        if (i == steps)
                            currentTime = time;
 
                        timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                        p = keys[listIndex];
                        float boneOffset = isXTimeline ? bone.X : bone.Y;
                        float valueOut = (boneOffset - p.value) / (currentTime - p.time);
                        p.outTangent = valueOut;
                        keys.Add(new Keyframe(currentTime, boneOffset, valueOut, 0));
 
                        keys[listIndex] = p;
 
                        listIndex++;
                        lastTime = currentTime;
                    }
                }
 
                frameIndex++;
                f += TranslateXTimeline.ENTRIES;
            }
 
            curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));
 
            string path = GetPath(boneData);
            const string propertyName = "localPosition";
 
            clip.SetCurve(path, typeof(Transform), propertyName + (isXTimeline ? ".x" : ".y"), curve);
        }
 
        static void ParseScaleTimeline (Skeleton skeleton, ScaleTimeline timeline, AnimationClip clip) {
            BoneData boneData = skeleton.Data.Bones.Items[timeline.BoneIndex];
            Bone bone = skeleton.Bones.Items[timeline.BoneIndex];
 
            AnimationCurve xCurve = new AnimationCurve();
            AnimationCurve yCurve = new AnimationCurve();
            AnimationCurve zCurve = new AnimationCurve();
 
            float endTime = timeline.Frames[(timeline.FrameCount * ScaleTimeline.ENTRIES) - ScaleTimeline.ENTRIES];
 
            float currentTime = timeline.Frames[0];
 
            List<Keyframe> xKeys = new List<Keyframe>();
            List<Keyframe> yKeys = new List<Keyframe>();
 
            xKeys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[1] * boneData.ScaleX, 0, 0));
            yKeys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[2] * boneData.ScaleY, 0, 0));
 
            int listIndex = 0;
            int frameIndex = 0;
            int f = ScaleTimeline.ENTRIES;
            float[] frames = timeline.Frames;
            skeleton.SetToSetupPose();
            float lastTime = 0;
            while (currentTime < endTime) {
                int pIndex = listIndex;
                float curveType = timeline.GetCurveType(frameIndex);
                if (curveType == 0) {
                    //linear
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
                    float x = frames[f + 1] * boneData.ScaleX;
                    float y = frames[f + 2] * boneData.ScaleY;
 
                    float xOut = (x - px.value) / (time - px.time);
                    float yOut = (y - py.value) / (time - py.time);
 
                    px.outTangent = xOut;
                    py.outTangent = yOut;
 
                    xKeys.Add(new Keyframe(time, x, xOut, 0));
                    yKeys.Add(new Keyframe(time, y, yOut, 0));
 
                    xKeys[pIndex] = px;
                    yKeys[pIndex] = py;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else if (curveType == 1) {
                    //stepped
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
                    float x = frames[f + 1] * boneData.ScaleX;
                    float y = frames[f + 2] * boneData.ScaleY;
 
                    float xOut = float.PositiveInfinity;
                    float yOut = float.PositiveInfinity;
 
                    px.outTangent = xOut;
                    py.outTangent = yOut;
 
                    xKeys.Add(new Keyframe(time, x, xOut, 0));
                    yKeys.Add(new Keyframe(time, y, yOut, 0));
 
                    xKeys[pIndex] = px;
                    yKeys[pIndex] = py;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else {
                    //bezier
                    Keyframe px = xKeys[pIndex];
                    Keyframe py = yKeys[pIndex];
 
                    float time = frames[f];
 
                    int steps = Mathf.FloorToInt((time - px.time) / BakeIncrement);
 
                    for (int i = 1; i <= steps; i++) {
                        currentTime += BakeIncrement;
                        if (i == steps)
                            currentTime = time;
 
                        timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                        px = xKeys[listIndex];
                        py = yKeys[listIndex];
 
                        float xOut = (bone.ScaleX - px.value) / (currentTime - px.time);
                        float yOut = (bone.ScaleY - py.value) / (currentTime - py.time);
 
                        px.outTangent = xOut;
                        py.outTangent = yOut;
 
                        xKeys.Add(new Keyframe(currentTime, bone.ScaleX, xOut, 0));
                        yKeys.Add(new Keyframe(currentTime, bone.ScaleY, yOut, 0));
 
                        xKeys[listIndex] = px;
                        yKeys[listIndex] = py;
 
                        listIndex++;
                        lastTime = currentTime;
                    }
                }
 
                frameIndex++;
                f += ScaleTimeline.ENTRIES;
            }
 
            xCurve = EnsureCurveKeyCount(new AnimationCurve(xKeys.ToArray()));
            yCurve = EnsureCurveKeyCount(new AnimationCurve(yKeys.ToArray()));
 
            string path = GetPath(boneData);
            string propertyName = "localScale";
 
            clip.SetCurve(path, typeof(Transform), propertyName + ".x", xCurve);
            clip.SetCurve(path, typeof(Transform), propertyName + ".y", yCurve);
            clip.SetCurve(path, typeof(Transform), propertyName + ".z", zCurve);
        }
 
        static void ParseSingleSplitScaleTimeline (Skeleton skeleton,
            ScaleXTimeline timelineX, ScaleYTimeline timelineY, AnimationClip clip) {
 
            bool isXTimeline = timelineX != null;
            CurveTimeline1 timeline = isXTimeline ? timelineX : timelineY as CurveTimeline1;
            IBoneTimeline boneTimeline = isXTimeline ? timelineX : timelineY as IBoneTimeline;
 
            BoneData boneData = skeleton.Data.Bones.Items[boneTimeline.BoneIndex];
            Bone bone = skeleton.Bones.Items[boneTimeline.BoneIndex];
            float boneDataOffset = isXTimeline ? boneData.ScaleX : boneData.ScaleY;
 
            AnimationCurve curve = new AnimationCurve();
            float endTime = timeline.Frames[(timeline.FrameCount * ScaleXTimeline.ENTRIES) - ScaleXTimeline.ENTRIES];
            float currentTime = timeline.Frames[0];
            List<Keyframe> keys = new List<Keyframe>();
            keys.Add(new Keyframe(timeline.Frames[0], timeline.Frames[1] * boneDataOffset, 0, 0));
 
            int listIndex = 0;
            int frameIndex = 0;
            int f = ScaleXTimeline.ENTRIES;
            float[] frames = timeline.Frames;
            skeleton.SetToSetupPose();
            float lastTime = 0;
            while (currentTime < endTime) {
                int pIndex = listIndex;
                float curveType = timeline.GetCurveType(frameIndex);
                if (curveType == 0) {
                    //linear
                    Keyframe p = keys[pIndex];
 
                    float time = frames[f];
                    float value = frames[f + 1] * boneDataOffset;
                    float valueOut = (value - p.value) / (time - p.time);
                    p.outTangent = valueOut;
                    keys.Add(new Keyframe(time, value, valueOut, 0));
 
                    keys[pIndex] = p;
                    currentTime = time;
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else if (curveType == 1) {
                    //stepped
                    Keyframe p = keys[pIndex];
 
                    float time = frames[f];
                    float value = frames[f + 1] * boneDataOffset;
                    float valueOut = float.PositiveInfinity;
                    p.outTangent = valueOut;
                    keys.Add(new Keyframe(time, value, valueOut, 0));
 
                    keys[pIndex] = p;
                    currentTime = time;
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else {
                    //bezier
                    Keyframe p = keys[pIndex];
                    float time = frames[f];
                    int steps = Mathf.FloorToInt((time - p.time) / BakeIncrement);
 
                    for (int i = 1; i <= steps; i++) {
                        currentTime += BakeIncrement;
                        if (i == steps)
                            currentTime = time;
 
                        timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                        p = keys[listIndex];
 
                        float boneScale = isXTimeline ? bone.ScaleX : bone.ScaleY;
                        float valueOut = (boneScale - p.value) / (currentTime - p.time);
                        p.outTangent = valueOut;
                        keys.Add(new Keyframe(currentTime, boneScale, valueOut, 0));
 
                        keys[listIndex] = p;
 
                        listIndex++;
                        lastTime = currentTime;
                    }
                }
 
                frameIndex++;
                f += ScaleXTimeline.ENTRIES;
            }
 
            curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));
 
            string path = GetPath(boneData);
            string propertyName = "localScale";
 
            clip.SetCurve(path, typeof(Transform), propertyName + (isXTimeline ? ".x" : ".y"), curve);
        }
 
        static void ParseRotateTimeline (Skeleton skeleton, RotateTimeline timeline, AnimationClip clip) {
            BoneData boneData = skeleton.Data.Bones.Items[timeline.BoneIndex];
            Bone bone = skeleton.Bones.Items[timeline.BoneIndex];
 
            AnimationCurve curve = new AnimationCurve();
 
            float endTime = timeline.Frames[(timeline.FrameCount * 2) - 2];
 
            float currentTime = timeline.Frames[0];
 
            List<Keyframe> keys = new List<Keyframe>();
 
            float rotation = timeline.Frames[1] + boneData.Rotation;
 
            keys.Add(new Keyframe(timeline.Frames[0], rotation, 0, 0));
 
            int listIndex = 0;
            int frameIndex = 0;
            int f = 2;
            float[] frames = timeline.Frames;
            skeleton.SetToSetupPose();
            float lastTime = 0;
            float angle = rotation;
            while (currentTime < endTime) {
                int pIndex = listIndex;
                float curveType = timeline.GetCurveType(frameIndex);
 
                if (curveType == 0) {
                    //linear
                    Keyframe pk = keys[pIndex];
 
                    float time = frames[f];
 
                    rotation = frames[f + 1] + boneData.Rotation;
                    angle += Mathf.DeltaAngle(angle, rotation);
                    float r = angle;
 
                    float rOut = (r - pk.value) / (time - pk.time);
 
                    pk.outTangent = rOut;
 
                    keys.Add(new Keyframe(time, r, rOut, 0));
 
                    keys[pIndex] = pk;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else if (curveType == 1) {
                    //stepped
 
                    Keyframe pk = keys[pIndex];
 
                    float time = frames[f];
 
                    rotation = frames[f + 1] + boneData.Rotation;
                    angle += Mathf.DeltaAngle(angle, rotation);
                    float r = angle;
 
                    float rOut = float.PositiveInfinity;
 
                    pk.outTangent = rOut;
 
                    keys.Add(new Keyframe(time, r, rOut, 0));
 
                    keys[pIndex] = pk;
 
                    currentTime = time;
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
 
                    lastTime = time;
                    listIndex++;
                } else {
                    //bezier
                    Keyframe pk = keys[pIndex];
 
                    float time = frames[f];
 
                    timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
                    skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
 
                    rotation = frames[f + 1] + boneData.Rotation;
                    angle += Mathf.DeltaAngle(angle, rotation);
                    float r = angle;
 
                    int steps = Mathf.FloorToInt((time - pk.time) / BakeIncrement);
 
                    for (int i = 1; i <= steps; i++) {
                        currentTime += BakeIncrement;
                        if (i == steps)
                            currentTime = time;
 
                        timeline.Apply(skeleton, lastTime, currentTime, null, 1, MixBlend.Setup, MixDirection.In);
                        skeleton.UpdateWorldTransform(Skeleton.Physics.Update);
                        pk = keys[listIndex];
 
                        rotation = bone.Rotation;
                        angle += Mathf.DeltaAngle(angle, rotation);
                        r = angle;
 
                        float rOut = (r - pk.value) / (currentTime - pk.time);
 
                        pk.outTangent = rOut;
 
                        keys.Add(new Keyframe(currentTime, r, rOut, 0));
 
                        keys[listIndex] = pk;
 
                        listIndex++;
                        lastTime = currentTime;
                    }
                }
 
                frameIndex++;
                f += 2;
            }
 
            curve = EnsureCurveKeyCount(new AnimationCurve(keys.ToArray()));
 
            string path = GetPath(boneData);
            const string propertyName = "localEulerAnglesBaked";
 
            EditorCurveBinding xBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".x");
            AnimationUtility.SetEditorCurve(clip, xBind, new AnimationCurve());
            EditorCurveBinding yBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".y");
            AnimationUtility.SetEditorCurve(clip, yBind, new AnimationCurve());
            EditorCurveBinding zBind = EditorCurveBinding.FloatCurve(path, typeof(Transform), propertyName + ".z");
            AnimationUtility.SetEditorCurve(clip, zBind, curve);
        }
 
        static void ParseEventTimeline (EventTimeline timeline, AnimationClip clip, SendMessageOptions eventOptions) {
            List<AnimationEvent> animationEvents = new List<AnimationEvent>();
            ParseEventTimeline(ref animationEvents, timeline, eventOptions);
            AnimationUtility.SetAnimationEvents(clip, animationEvents.ToArray());
        }
 
        static void ParseEventTimeline (ref List<AnimationEvent> animationEvents,
            EventTimeline timeline, SendMessageOptions eventOptions) {
 
            float[] frames = timeline.Frames;
            Event[] events = timeline.Events;
 
            for (int i = 0, n = frames.Length; i < n; i++) {
                Event spineEvent = events[i];
                string eventName = spineEvent.Data.Name;
                if (SpineEditorUtilities.Preferences.mecanimEventIncludeFolderName)
                    eventName = eventName.Replace("/", ""); // calls method FolderNameEventName()
                else
                    eventName = eventName.Substring(eventName.LastIndexOf('/') + 1); // calls method EventName()
                AnimationEvent unityAnimationEvent = new AnimationEvent {
                    time = frames[i],
                    functionName = eventName,
                    messageOptions = eventOptions,
                    stringParameter = SpineEventStringId
                };
 
                if (!string.IsNullOrEmpty(spineEvent.String)) {
                    unityAnimationEvent.stringParameter = spineEvent.String;
                } else if (spineEvent.Int != 0) {
                    unityAnimationEvent.intParameter = spineEvent.Int;
                } else if (spineEvent.Float != 0) {
                    unityAnimationEvent.floatParameter = spineEvent.Float;
                } // else, paramless function/Action.
 
                animationEvents.Add(unityAnimationEvent);
            }
        }
 
        static void AddPreviousUserEvents (ref List<AnimationEvent> allEvents, AnimationEvent[] previousEvents) {
            foreach (AnimationEvent previousEvent in previousEvents) {
                if (previousEvent.stringParameter == SpineEventStringId)
                    continue;
                AnimationEvent identicalEvent = allEvents.Find(newEvent => {
                    return newEvent.functionName == previousEvent.functionName &&
                        Mathf.Abs(newEvent.time - previousEvent.time) < EventTimeEqualityEpsilon;
                });
                if (identicalEvent != null)
                    continue;
 
                allEvents.Add(previousEvent);
            }
        }
 
        static void ParseAttachmentTimeline (Skeleton skeleton, AttachmentTimeline timeline, Dictionary<int, List<string>> slotLookup, AnimationClip clip) {
            List<string> attachmentNames = slotLookup[timeline.SlotIndex];
 
            string bonePath = GetPath(skeleton.Slots.Items[timeline.SlotIndex].Bone.Data);
            string slotPath = bonePath + "/" + skeleton.Slots.Items[timeline.SlotIndex].Data.Name;
 
            Dictionary<string, AnimationCurve> curveTable = new Dictionary<string, AnimationCurve>();
 
            foreach (string str in attachmentNames) {
                curveTable.Add(str, new AnimationCurve());
            }
 
            float[] frames = timeline.Frames;
 
            if (frames[0] != 0) {
                string startingName = skeleton.Slots.Items[timeline.SlotIndex].Data.AttachmentName;
                foreach (KeyValuePair<string, AnimationCurve> pair in curveTable) {
                    if (startingName == "" || startingName == null) {
                        pair.Value.AddKey(new Keyframe(0, 0, float.PositiveInfinity, float.PositiveInfinity));
                    } else {
                        if (pair.Key == startingName) {
                            pair.Value.AddKey(new Keyframe(0, 1, float.PositiveInfinity, float.PositiveInfinity));
                        } else {
                            pair.Value.AddKey(new Keyframe(0, 0, float.PositiveInfinity, float.PositiveInfinity));
                        }
                    }
                }
            }
 
            for (int f = 0, frameCount = frames.Length; f < frameCount; ++f) {
                float time = frames[f];
 
                int frameIndex = Search(frames, time);
 
                string name = timeline.AttachmentNames[frameIndex];
                foreach (KeyValuePair<string, AnimationCurve> pair in curveTable) {
                    if (name == "") {
                        pair.Value.AddKey(new Keyframe(time, 0, float.PositiveInfinity, float.PositiveInfinity));
                    } else {
                        if (pair.Key == name) {
                            pair.Value.AddKey(new Keyframe(time, 1, float.PositiveInfinity, float.PositiveInfinity));
                        } else {
                            pair.Value.AddKey(new Keyframe(time, 0, float.PositiveInfinity, float.PositiveInfinity));
                        }
                    }
                }
            }
 
            foreach (KeyValuePair<string, AnimationCurve> pair in curveTable) {
                string path = slotPath + "/" + pair.Key;
                string prop = "m_IsActive";
 
                clip.SetCurve(path, typeof(GameObject), prop, pair.Value);
            }
        }
 
        static AnimationCurve EnsureCurveKeyCount (AnimationCurve curve) {
            if (curve.length == 1)
                curve.AddKey(curve.keys[0].time + 0.25f, curve.keys[0].value);
 
            return curve;
        }
 
        static float GetUninheritedAppliedRotation (Bone b) {
            Bone parent = b.Parent;
            float angle = b.AppliedRotation;
 
            while (parent != null) {
                angle -= parent.AppliedRotation;
                parent = parent.Parent;
            }
 
            return angle;
        }
        #endregion
        #endregion
 
        #region Region Baking
        public static GameObject BakeRegion (SpineAtlasAsset atlasAsset, AtlasRegion region, bool autoSave = true) {
            atlasAsset.GetAtlas(); // Initializes atlasAsset.
 
            string atlasAssetPath = AssetDatabase.GetAssetPath(atlasAsset);
            string atlasAssetDirPath = Path.GetDirectoryName(atlasAssetPath).Replace('\\', '/');
            string bakedDirPath = Path.Combine(atlasAssetDirPath, atlasAsset.name);
            string bakedPrefabPath = Path.Combine(bakedDirPath, AssetUtility.GetPathSafeName(region.name) + ".prefab").Replace("\\", "/");
 
            GameObject prefab = (GameObject)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(GameObject));
            GameObject root;
            Mesh mesh;
            bool isNewPrefab = false;
 
            if (!Directory.Exists(bakedDirPath))
                Directory.CreateDirectory(bakedDirPath);
 
            if (prefab == null) {
                root = EditorInstantiation.NewGameObject("temp", true, typeof(MeshFilter), typeof(MeshRenderer));
#if NEW_PREFAB_SYSTEM
                prefab = PrefabUtility.SaveAsPrefabAsset(root, bakedPrefabPath);
#else
                prefab = PrefabUtility.CreatePrefab(bakedPrefabPath, root);
#endif
 
                isNewPrefab = true;
                Object.DestroyImmediate(root);
            }
 
            mesh = (Mesh)AssetDatabase.LoadAssetAtPath(bakedPrefabPath, typeof(Mesh));
 
            Material mat = null;
            mesh = atlasAsset.GenerateMesh(region.name, mesh, out mat);
            if (isNewPrefab) {
                AssetDatabase.AddObjectToAsset(mesh, prefab);
                prefab.GetComponent<MeshFilter>().sharedMesh = mesh;
            }
 
            EditorUtility.SetDirty(mesh);
            EditorUtility.SetDirty(prefab);
 
            if (autoSave) {
                AssetDatabase.SaveAssets();
                AssetDatabase.Refresh();
            }
 
            prefab.GetComponent<MeshRenderer>().sharedMaterial = mat;
 
            return prefab;
        }
        #endregion
 
        static string GetPath (BoneData b) {
            return GetPathRecurse(b).Substring(1);
        }
 
        static string GetPathRecurse (BoneData b) {
            if (b == null) return "";
            return GetPathRecurse(b.Parent) + "/" + b.Name;
        }
 
        static void SetAnimationSettings (AnimationClip clip, AnimationClipSettings settings) {
            AnimationUtility.SetAnimationClipSettings(clip, settings);
        }
 
 
    }
 
}