7
7
using UnityEditorInternal ;
8
8
using CurveExtended ;
9
9
using LitJson ;
10
+ using System . Reflection ;
10
11
11
12
namespace UnitySpineImporter {
12
13
public class AtlasImageNotFoundException : System . Exception {
@@ -44,16 +45,91 @@ public static void buildPrefab(GameObject gameObject, string directory, string n
44
45
PrefabUtility . ReplacePrefab ( gameObject , oldPrefab , ReplacePrefabOptions . ReplaceNameBased ) ;
45
46
}
46
47
48
+ public static void builAvatarMask ( GameObject gameObject , SpineData spineData , Animator animator , string directory , string name ) {
49
+ Avatar avatar = AvatarBuilder . BuildGenericAvatar ( gameObject , "" ) ;
50
+ animator . avatar = avatar ;
51
+ AvatarMask avatarMask = new AvatarMask ( ) ;
52
+ string [ ] transofrmPaths = getTransformPaths ( gameObject , spineData ) ;
53
+ avatarMask . transformCount = transofrmPaths . Length ;
54
+ for ( int i = 0 ; i < transofrmPaths . Length ; i ++ ) {
55
+ avatarMask . SetTransformPath ( i , transofrmPaths [ i ] ) ;
56
+ avatarMask . SetTransformActive ( i , true ) ;
57
+ }
58
+ createFolderIfNoExists ( directory , ANIMATION_FOLDER ) ;
59
+ AssetDatabase . CreateAsset ( avatar , directory + "/" + ANIMATION_FOLDER + "/" + name + ".anim.asset" ) ;
60
+ AssetDatabase . CreateAsset ( avatarMask , directory + "/" + ANIMATION_FOLDER + "/" + name + ".mask.asset" ) ;
61
+ }
62
+
63
+ public static string [ ] getTransformPaths ( GameObject go , SpineData spineData ) {
64
+ List < String > result = new List < string > ( ) ;
65
+ result . Add ( "" ) ;
66
+ foreach ( Transform t in go . GetComponentsInChildren < Transform > ( true ) ) {
67
+ string path = AnimationUtility . CalculateTransformPath ( t , go . transform ) ;
68
+ if ( t . name . StartsWith ( SLOT_PREFIX + " [" ) && t . name . EndsWith ( "]" ) ) {
69
+ string slotName = t . name . Remove ( t . name . Length - 1 ) ;
70
+ slotName = slotName . Remove ( 0 , ( SLOT_PREFIX + " [" ) . Length ) ;
71
+ if ( spineData . slotPathByName . ContainsKey ( slotName ) && spineData . slotPathByName [ slotName ] == path )
72
+ result . Add ( path ) ;
73
+ } else {
74
+ if ( spineData . bonePathByName . ContainsKey ( t . name ) && spineData . bonePathByName [ t . name ] == path )
75
+ result . Add ( path ) ;
76
+ }
77
+
78
+ }
79
+ return result . ToArray ( ) ;
80
+ }
81
+
82
+ static int [ ] sizes = new int [ ] { 0 , 32 , 64 , 128 , 256 , 512 , 1024 , 2048 , 4096 } ;
83
+ static string [ ] platforms = new string [ ] { "Web" , "Standalone" , "iPhone" , "Android" , "FlashPlayer" } ;
84
+ static void fixTextureSize ( string imagePath ) {
85
+ TextureImporter importer = TextureImporter . GetAtPath ( imagePath ) as TextureImporter ;
86
+ if ( importer != null ) {
87
+ object [ ] args = new object [ 2 ] { 0 , 0 } ;
88
+ MethodInfo mi = typeof ( TextureImporter ) . GetMethod ( "GetWidthAndHeight" , BindingFlags . NonPublic | BindingFlags . Instance ) ;
89
+ mi . Invoke ( importer , args ) ;
90
+
91
+ int width = ( int ) args [ 0 ] ;
92
+ int height = ( int ) args [ 1 ] ;
93
+
94
+ int max = Mathf . Max ( width , height ) ;
95
+ if ( max > 4096 ) {
96
+ Debug . LogError ( "original texture size is to big " + imagePath + " size=" + width + "x" + height ) ;
97
+ return ;
98
+ }
47
99
100
+ int fitSize = 0 ;
101
+ for ( int i = 0 , nextI = 1 ; i < max && fitSize == 0 ; i = nextI ++ ) {
102
+ if ( max > sizes [ i ] && max <= sizes [ nextI ] )
103
+ fitSize = sizes [ nextI ] ;
104
+ }
105
+ if ( importer . maxTextureSize != fitSize ) {
106
+ Debug . LogWarning ( "change default size to " + fitSize + " for " + imagePath ) ;
107
+ importer . maxTextureSize = fitSize ;
108
+ }
109
+ foreach ( string platform in platforms ) {
110
+ int maxTextureSize ;
111
+ TextureImporterFormat textureFormat ;
112
+ importer . GetPlatformTextureSettings ( platform , out maxTextureSize , out textureFormat ) ;
113
+ if ( maxTextureSize != fitSize ) {
114
+ Debug . LogWarning ( "change specific size to " + fitSize + "on " + platform + " for " + imagePath ) ;
115
+ importer . SetPlatformTextureSettings ( platform , fitSize , textureFormat ) ;
116
+ }
117
+ }
118
+ AssetDatabase . ImportAsset ( imagePath , ImportAssetOptions . ForceSynchronousImport ) ;
119
+ }
120
+ }
121
+
48
122
public static void updateImporters ( SpineMultiatlas multiatlas , string directory , int pixelsPerUnit , out SpritesByName spriteByName ) {
49
123
spriteByName = new SpritesByName ( ) ;
50
124
foreach ( SpineAtlas spineAtlas in multiatlas ) {
51
125
string imagePath = directory + "/" + spineAtlas . imageName ;
52
126
if ( ! File . Exists ( imagePath ) )
53
127
throw new AtlasImageNotFoundException ( "can't find " + spineAtlas . imageName + " image in " + directory + " folder" ) ;
128
+ fixTextureSize ( imagePath ) ;
54
129
Texture2D tex = AssetDatabase . LoadAssetAtPath ( imagePath , typeof ( Texture2D ) ) as Texture2D ;
55
130
Vector2 atlasSize = new Vector2 ( tex . width , tex . height ) ;
56
131
TextureImporter importer = TextureImporter . GetAtPath ( imagePath ) as TextureImporter ;
132
+ importer . maxTextureSize = 2048 ;
57
133
importer . spritesheet = getSpriteMetadata ( spineAtlas , atlasSize ) ;
58
134
importer . textureType = TextureImporterType . Sprite ;
59
135
importer . spriteImportMode = SpriteImportMode . Multiple ;
@@ -307,14 +383,15 @@ public static void addAnimation(GameObject rootGO,
307
383
SpineData spineData ,
308
384
Dictionary < string , GameObject > boneGOByName ,
309
385
AttachmentGOByNameBySlot attachmentGOByNameBySlot ,
310
- int pixelsPerUnit )
386
+ int pixelsPerUnit ,
387
+ ModelImporterAnimationType modelImporterAnimationType )
311
388
{
312
389
float ratio = 1.0f / ( float ) pixelsPerUnit ;
313
390
foreach ( KeyValuePair < string , SpineAnimation > kvp in spineData . animations ) {
314
391
string animationName = kvp . Key ;
315
392
SpineAnimation spineAnimation = kvp . Value ;
316
393
AnimationClip animationClip = new AnimationClip ( ) ;
317
- AnimationUtility . SetAnimationType ( animationClip , ModelImporterAnimationType . Generic ) ;
394
+ AnimationUtility . SetAnimationType ( animationClip , modelImporterAnimationType ) ;
318
395
if ( spineAnimation . bones != null )
319
396
addBoneAnimationToClip ( animationClip , spineAnimation . bones , spineData , boneGOByName , ratio ) ;
320
397
if ( spineAnimation . slots != null )
@@ -324,15 +401,30 @@ public static void addAnimation(GameObject rootGO,
324
401
325
402
animationClip . frameRate = 30 ;
326
403
string animationFolder = rootDirectory + "/" + ANIMATION_FOLDER ;
327
- if ( ! Directory . Exists ( animationFolder ) )
328
- Directory . CreateDirectory ( animationFolder ) ;
404
+ createFolderIfNoExists ( rootDirectory , ANIMATION_FOLDER ) ;
329
405
330
406
AssetDatabase . CreateAsset ( animationClip , animationFolder + "/" + animationName + ".anim" ) ;
331
407
AssetDatabase . SaveAssets ( ) ;
332
- AddClipToAnimatorComponent ( rootGO , animationClip ) ;
408
+ if ( modelImporterAnimationType == ModelImporterAnimationType . Generic )
409
+ AddClipToAnimatorComponent ( rootGO , animationClip ) ;
410
+ else
411
+ AddClipToLegacyAnimationComponent ( rootGO , animationClip ) ;
333
412
}
334
413
}
335
414
415
+ static void AddClipToLegacyAnimationComponent ( GameObject rootGO , AnimationClip animationClip ) {
416
+ Animation animation = rootGO . GetComponent < Animation > ( ) ;
417
+ if ( animation == null )
418
+ animation = rootGO . AddComponent < Animation > ( ) ;
419
+ animation . AddClip ( animationClip , animationClip . name ) ;
420
+ }
421
+
422
+ static void createFolderIfNoExists ( string root , string folderName ) {
423
+ string path = root + "/" + folderName ;
424
+ if ( ! Directory . Exists ( path ) )
425
+ Directory . CreateDirectory ( path ) ;
426
+ }
427
+
336
428
public static void addSlotAnimationToClip ( AnimationClip clip ,
337
429
Dictionary < string , SpineSlotAnimation > slotsAnimation ,
338
430
SpineData spineData ,
0 commit comments