public static void updateImporters(SpineMultiatlas multiatlas, string directory, int pixelsPerUnit, out SpritesByName spriteByName) { spriteByName = new SpritesByName(); foreach (SpineAtlas spineAtlas in multiatlas) { string imagePath = directory + "/" + spineAtlas.imageName; if (!File.Exists(imagePath)) { throw new AtlasImageNotFoundException("can't find " + spineAtlas.imageName + " image in " + directory + " folder"); } fixTextureSize(imagePath); Texture2D tex = AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D)) as Texture2D; Vector2 atlasSize = new Vector2(tex.width, tex.height); TextureImporter importer = TextureImporter.GetAtPath(imagePath) as TextureImporter; importer.spritesheet = getSpriteMetadata(spineAtlas, atlasSize); importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritePixelsToUnits = pixelsPerUnit; AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceUpdate); AssetDatabase.SaveAssets(); foreach (UnityEngine.Object obj in AssetDatabase.LoadAllAssetsAtPath(imagePath)) { Sprite s = obj as Sprite; if (s != null) { try{ spriteByName.Add(s.name, s); } catch (ArgumentException e) { throw new AtlasImageDuplicateSpriteName("source images has duplicate name " + s.name + "\n" + e); } } } foreach (SpineSprite spineSprite in spineAtlas.sprites) { if (spineSprite.rotate) { spriteByName.rotatedSprites.Add(spriteByName[spineSprite.name]); } } } }
public static void addAllAttahcmentsSlots(SpineData spineData, SpritesByName spriteByName, Dictionary <string, Slot> slotByName, float zStep, int pixelsPerUnit, out List <Skin> skins, out AttachmentGOByNameBySlot attachmentGOByNameBySlot) { float ratio = 1.0f / (float)pixelsPerUnit; skins = new List <Skin>(); attachmentGOByNameBySlot = new AttachmentGOByNameBySlot(); foreach (KeyValuePair <string, SpineSkinSlots> kvp in spineData.skins) { string skinName = kvp.Key; Skin skin = new Skin(); skin.name = skinName; List <SkinSlot> slotList = new List <SkinSlot>(); bool isDefault = skinName.Equals("default"); foreach (KeyValuePair <string, SpineSkinSlotAttachments> kvp2 in spineData.skins[skinName]) { string slotName = kvp2.Key; GameObject slotGO = slotByName[slotName].gameObject; Slot slot = slotByName[slotName]; SkinSlot skinSlot = new SkinSlot(); skinSlot.name = slotName; skinSlot.gameObject = slotGO; List <SkinSlotAttachment> attachmentList = new List <SkinSlotAttachment>(); foreach (KeyValuePair <string, SpineSkinAttachment> kvp3 in spineData.skins[skinName][slotName]) { string attachmenName = kvp3.Key; SkinSlotAttachment attachment = new SkinSlotAttachment(); attachment.name = attachmenName; SpineSkinAttachment spineAttachment = kvp3.Value; // - create skined object or direct GO for default skin Sprite sprite; spriteByName.TryGetValue(spineAttachment.name, out sprite); int drawOrder = spineData.slotOrder[slotName]; GameObject parentGO; GameObject spriteGO; string fixedName = attachmenName.Replace("/", SLASH_REPLACEMENT); if (isDefault) { parentGO = slotGO; spriteGO = new GameObject(fixedName); Attachment a = new Attachment(attachmenName, AttachmentType.SINGLE_SPRITE, spriteGO); slot.addAttachment(a); } else { spriteGO = new GameObject(skinName); Attachment a; slot.attachmentByName.TryGetValue(attachmenName, out a); if (a == null) { GameObject attachmentGO = new GameObject(fixedName); attachmentGO.transform.parent = slotGO.transform; resetLocalTRS(attachmentGO); a = new Attachment(attachmenName, AttachmentType.SKINED_SPRITE, attachmentGO); slot.addAttachment(a); } parentGO = a.gameObject; } attachment.gameObject = spriteGO; spriteGO.transform.parent = parentGO.gameObject.transform; // - if (spineAttachment.type.Equals("region")) { SpriteRenderer sr = spriteGO.AddComponent <SpriteRenderer>(); sr.sprite = sprite; spriteGO.transform.localPosition = getAttachmentPosition(spineAttachment, ratio, -(drawOrder * zStep)); spriteGO.transform.localRotation = getAttachmentRotation(spineAttachment, spriteByName.rotatedSprites.Contains(sprite)); spriteGO.transform.localScale = getAttachmentScale(spineAttachment); } else if (spineAttachment.type.Equals("boundingbox")) { PolygonCollider2D collider = spriteGO.AddComponent <PolygonCollider2D>(); resetLocalTRS(spriteGO); Vector2[] vertices = new Vector2[spineAttachment.vertices.Length / 2]; for (int i = 0; i < spineAttachment.vertices.Length; i += 2) { float x = (float)spineAttachment.vertices[i] * ratio; float y = (float)spineAttachment.vertices[i + 1] * ratio; vertices[i / 2] = new Vector2(x, y); } collider.points = vertices; collider.SetPath(0, vertices); } else { Debug.LogWarning("Attachment type " + spineAttachment.type + " is not supported yiet FIX MEEE"); } attachmentList.Add(attachment); } skinSlot.attachments = attachmentList.ToArray(); slotList.Add(skinSlot); } skin.slots = slotList.ToArray(); skins.Add(skin); } }
public static void updateImporters(SpineMultiatlas multiatlas, string directory, int pixelsPerUnit, out SpritesByName spriteByName) { spriteByName = new SpritesByName(); foreach (SpineAtlas spineAtlas in multiatlas){ string imagePath = directory + "/" + spineAtlas.imageName; if (!File.Exists(imagePath)) throw new AtlasImageNotFoundException("can't find " + spineAtlas.imageName + " image in " + directory + " folder"); fixTextureSize(imagePath); Texture2D tex = AssetDatabase.LoadAssetAtPath(imagePath, typeof(Texture2D )) as Texture2D; Vector2 atlasSize = new Vector2(tex.width, tex.height); TextureImporter importer = TextureImporter.GetAtPath(imagePath) as TextureImporter; importer.spritesheet = getSpriteMetadata(spineAtlas, atlasSize); importer.textureType = TextureImporterType.Sprite; importer.spriteImportMode = SpriteImportMode.Multiple; importer.spritePixelsToUnits = pixelsPerUnit; AssetDatabase.ImportAsset(imagePath, ImportAssetOptions.ForceUpdate); AssetDatabase.SaveAssets(); foreach(UnityEngine.Object obj in AssetDatabase.LoadAllAssetsAtPath(imagePath)){ Sprite s = obj as Sprite; if (s!=null){ try{ spriteByName.Add(s.name,s); } catch (ArgumentException e) { throw new AtlasImageDuplicateSpriteName("source images has duplicate name "+s.name +"\n"+e); } } } foreach(SpineSprite spineSprite in spineAtlas.sprites){ if (spineSprite.rotate){ spriteByName.rotatedSprites.Add(spriteByName[spineSprite.name]); } } } }
public static void addAllAttahcmentsSlots(SpineData spineData, SpritesByName spriteByName, Dictionary<string, Slot> slotByName, int pixelsPerUnit, out List<Skin> skins, out AttachmentGOByNameBySlot attachmentGOByNameBySlot) { float ratio = 1.0f / (float) pixelsPerUnit; skins = new List<Skin>(); attachmentGOByNameBySlot= new AttachmentGOByNameBySlot(); foreach(KeyValuePair<string, SpineSkinSlots>kvp in spineData.skins){ string skinName = kvp.Key; Skin skin = new Skin(); skin.name = skinName; List<SkinSlot> slotList = new List<SkinSlot>(); bool isDefault = skinName.Equals("default"); foreach(KeyValuePair<string, SpineSkinSlotAttachments>kvp2 in spineData.skins[skinName]){ string slotName = kvp2.Key; GameObject slotGO = slotByName[slotName].gameObject; Slot slot = slotByName[slotName]; string spritePath = spineData.slotPathByName[ slotName ] + "/"; SkinSlot skinSlot = new SkinSlot(); skinSlot.name = slotName; skinSlot.gameObject = slotGO; List<SkinSlotAttachment> attachmentList = new List<SkinSlotAttachment>(); foreach(KeyValuePair<string, SpineSkinAttachment> kvp3 in spineData.skins[skinName][slotName]){ string attachmenName = kvp3.Key; SkinSlotAttachment attachment = new SkinSlotAttachment(); attachment.name = attachmenName; SpineSkinAttachment spineAttachment = kvp3.Value; // - create skined object or direct GO for default skin Sprite sprite; spriteByName.TryGetValue(spineAttachment.name, out sprite); GameObject parentGO; GameObject spriteGO; string fixedName = attachmenName.Replace("/",SLASH_REPLACEMENT); if (isDefault){ parentGO = slotGO; spriteGO = new GameObject(fixedName); spritePath += fixedName; Attachment a = new Attachment(attachmenName, AttachmentType.SINGLE_SPRITE, spriteGO); slot.addAttachment(a); } else { spriteGO = new GameObject(skinName); Attachment a; slot.attachmentByName.TryGetValue(attachmenName, out a); if (a == null){ GameObject attachmentGO = new GameObject(fixedName); attachmentGO.transform.parent = slotGO.transform; resetLocalTRS(attachmentGO); a = new Attachment(attachmenName, AttachmentType.SKINED_SPRITE, attachmentGO); slot.addAttachment(a); } spritePath += fixedName + "/" + skinName; parentGO = a.gameObject; } attachment.gameObject = spriteGO; attachment.ObPath = spritePath; spriteGO.transform.parent = parentGO.gameObject.transform; // - if (spineAttachment.type.Equals("region")){ SpriteRenderer sr = spriteGO.AddComponent<SpriteRenderer>(); sr.sprite = sprite; spriteGO.transform.localPosition = getAttachmentPosition(spineAttachment, ratio, 0); spriteGO.transform.localRotation = getAttachmentRotation(spineAttachment, spriteByName.rotatedSprites.Contains(sprite)); spriteGO.transform.localScale = getAttachmentScale(spineAttachment); attachment.sprite = sr; } else if (spineAttachment.type.Equals("boundingbox")) { PolygonCollider2D collider = spriteGO.AddComponent<PolygonCollider2D>(); resetLocalTRS(spriteGO); Vector2[] vertices = new Vector2[spineAttachment.vertices.Length/2]; for (int i = 0; i < spineAttachment.vertices.Length; i+=2) { float x = (float) spineAttachment.vertices[i ] * ratio; float y = (float) spineAttachment.vertices[i+1] * ratio; vertices[i/2] = new Vector2(x,y); } collider.points = vertices; collider.SetPath(0,vertices); }else { Debug.LogWarning("Attachment type " + spineAttachment.type + " is not supported yiet FIX MEEE"); } attachmentList.Add(attachment); } skinSlot.attachments = attachmentList.ToArray(); slotList.Add(skinSlot); } skin.slots = slotList.ToArray(); skins.Add(skin); } }