Beispiel #1
0
        static void setCachedData(SpineData data)
        {
            data.slotByName = new Dictionary <string, SpineSlot>();


            data.boneByName = new Dictionary <string, SpineBone>();
            for (int i = 0; i < data.bones.Count; i++)
            {
                data.boneByName.Add(data.bones[i].name, data.bones[i]);
            }

            data.bonePathByName = new Dictionary <string, string>();

            foreach (SpineBone bone in data.bones)
            {
                string    path = "";
                SpineBone b    = bone;
                do
                {
                    path = b.name + "/" + path;
                    if (!string.IsNullOrEmpty(b.parent))
                    {
                        b = data.boneByName[b.parent];
                    }
                    else
                    {
                        b = null;
                    }
                } while (b != null);

                if (path.Length > 0)
                {
                    path = path.Remove(path.Length - 1);
                }

                data.bonePathByName.Add(bone.name, path);
            }


            data.slotOrder = new Dictionary <string, int>();

            data.slotPathByName         = new Dictionary <string, string>();
            data.slotDefaultAttachments = new Dictionary <string, string>();
            for (int i = 0; i < data.slots.Count; i++)
            {
                string slotName          = data.slots[i].name;
                string defaultAttachment = data.slots[i].attachment;
                data.slotOrder.Add(slotName, i);
                string boneName = data.slots[i].bone;
                string bonePath = data.bonePathByName[boneName];
                string slotPath = bonePath + "/" + SpineUtil.getSlotGOName(slotName);
                data.slotPathByName.Add(slotName, slotPath);
                data.slotDefaultAttachments.Add(slotName, defaultAttachment);
            }
        }
Beispiel #2
0
        void OnWizardCreate()
        {
            string        atlasPath = getAtlasFilePath(path);
            string        directory = Path.GetDirectoryName(atlasPath);
            string        name      = Path.GetFileNameWithoutExtension(path);
            SpritesByName spriteByName;
            Dictionary <string, GameObject> boneGOByName;
            Dictionary <string, Slot>       slotByName;
            List <Skin> skins;
            AttachmentGOByNameBySlot attachmentGOByNameBySlot;

            if (File.Exists(path))
            {
                try{
                    SpineMultiatlas spineMultiAtlas = SpineMultiatlas.deserializeFromFile(atlasPath);
                    SpineData       spineData       = SpineData.deserializeFromFile(path);

                    SpineUtil.updateImporters(spineMultiAtlas, directory, pixelsPerUnit, out spriteByName);
                    GameObject rootGO = SpineUtil.buildSceleton(name, spineData, pixelsPerUnit, out boneGOByName, out slotByName);
                    rootGO.name = name;
                    SpineUtil.addAllAttahcmentsSlots(spineData, spriteByName, slotByName, zStep, pixelsPerUnit, out skins, out attachmentGOByNameBySlot);
                    SkinController sk = SpineUtil.addSkinController(rootGO, spineData, skins, slotByName);
                    if (animationImportType == AnimationImportType.MECANIM)
                    {
                        Animator animator = SpineUtil.addAnimator(rootGO);
                        if (buildAvatarMask)
                        {
                            SpineUtil.builAvatarMask(rootGO, spineData, animator, directory, name);
                        }
                    }

                    ModelImporterAnimationType modelImporterAnimationType = getModelImporterAnimationType();
                    if (spineData.animations != null && spineData.animations.Count > 0)
                    {
                        SpineUtil.addAnimation(rootGO, directory, spineData, boneGOByName, attachmentGOByNameBySlot,
                                               pixelsPerUnit, modelImporterAnimationType, updateResources);
                    }
                    sk.showDefaulSlots();
                    SpineUtil.buildPrefab(rootGO, directory, name);
                    GameObject.DestroyImmediate(rootGO);
                } catch (SpineMultiatlasCreationException e) {
                    Debug.LogException(e);
                } catch (SpineDatatCreationException e) {
                    Debug.LogException(e);
                } catch (AtlasImageDuplicateSpriteName e) {
                    Debug.LogException(e);
                }
            }
        }
Beispiel #3
0
        public static SpineMultiatlas deserializeFromFile(string multiatlasFilePath)
        {
            SpineMultiatlas multiAtlas = new SpineMultiatlas();

            if (!File.Exists(multiatlasFilePath))
            {
                throw new SpineMultiatlasCreationException("provided file does not exists");
            }
            using (StreamReader streamReader = new StreamReader(multiatlasFilePath)){
                string      line;
                string      spriteNameAfterProps = "";
                bool        setMainProps         = false;
                SpineAtlas  spineAtlas           = null;
                SpineSprite sprite = null;
                while ((line = streamReader.ReadLine()) != null)
                {
                    if (line == "")
                    {
                        setMainProps = true;
                    }
                    else
                    {
                        if (setMainProps)
                        {
                            spineAtlas = new SpineAtlas();
                            multiAtlas.Add(spineAtlas);
                            spineAtlas.imageName = line;
                            Dictionary <string, string> keyValue = new Dictionary <string, string>();
                            string[] kvp;
                            while ((kvp = streamReader.ReadLine().Split(':')).Length == 2)
                            {
                                keyValue.Add(kvp[0].Trim(), kvp[1].Trim());
                            }

                            spineAtlas.format = keyValue["format"];
                            spineAtlas.filter = keyValue["filter"];
                            spineAtlas.repeat = keyValue["repeat"];

                            spriteNameAfterProps = kvp[0];
                            spineAtlas.sprites   = new List <SpineSprite>();
                            setMainProps         = false;
                        }

                        if (!setMainProps)
                        {
                            sprite = new SpineSprite();

                            if (string.IsNullOrEmpty(spriteNameAfterProps))
                            {
                                sprite.name = line;
                            }
                            else
                            {
                                sprite.name          = spriteNameAfterProps;
                                spriteNameAfterProps = "";
                            }
                            try{
                                sprite.rotate = bool.Parse(streamReader.ReadLine().Split(':')[1]);
                                sprite.xy     = SpineUtil.lineToVector2(streamReader.ReadLine());
                                sprite.size   = SpineUtil.lineToVector2(streamReader.ReadLine());
                                sprite.orig   = SpineUtil.lineToVector2(streamReader.ReadLine());
                                sprite.offset = SpineUtil.lineToVector2(streamReader.ReadLine());
                                sprite.index  = int.Parse(streamReader.ReadLine().Split(':')[1]);
                            } catch (System.FormatException e) {
                                throw new SpineMultiatlasCreationException("can't parse source file \n" + multiatlasFilePath + "\n" + e);
                            }
                            spineAtlas.sprites.Add(sprite);
                        }
                    }
                }
            }

            if (multiAtlas.Count == 0)
            {
                throw new SpineMultiatlasCreationException("don't have any atlases in provided file");
            }
            return(multiAtlas);
        }