Example #1
0
		public void addAttachment(Attachment attachment){
			if (attachments == null){
				attachments = new Attachment[]{attachment};
			} else {
				Attachment[] newA = new Attachment[attachments.Length + 1];
				Array.Copy(attachments, newA,attachments.Length);
				newA[newA.Length -1] = attachment;
				attachments  = newA;
			}
			if (_attachmentByName == null)
				_attachmentByName = new Dictionary<string, Attachment>();
			_attachmentByName.Add(attachment.name, attachment);
		}
        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);
            }
        }