Exemple #1
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);
                }
            }
        }
        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]);
                    }
                }
            }
        }
Exemple #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);
        }