示例#1
0
        public static void UnpackToSprite(UnityEngine.Object spriteCollectionData, out SpritePackage sprites, string fileLocation = null)
        {
            OutClass <SpritePackage> resultSprites = new OutClass <SpritePackage>();

            var iterator = UnpackToSpriteAsyncInternal(spriteCollectionData, resultSprites, fileLocation);

            while (iterator.MoveNext())
            {
            }

            sprites = resultSprites.Value;
        }
示例#2
0
    public void SetTokenBod(Bod b, bool faceLeft = true)
    {
        // Set Bod reference
        bod = b;

        // Set SpritePackage
        GameObject newSpritePackage = Instantiate(SpriteManager.instance.spritePackagePrefab, spriteDisplay.transform);

        spritePackage = newSpritePackage.GetComponent <SpritePackage>();
        spritePackage.SetBodPackage(bod.spritePath);
        spritePackage.FlipSprite(faceLeft);
        spritePackage.SetStanding();


        if (BodManager.instance.GetBodMind(bod) == 0)
        {
            mindBar.SetActive(false);
        }
        bodMenu.SetMenu(bod);
    }
示例#3
0
        static IEnumerator UnpackToSpriteAsyncInternal(UnityEngine.Object tk2dSpriteCollection, OutClass <SpritePackage> resultSprites = null, string fileLocation = null)
        {
            //TODO : READ ACTUAL IMAGE SIZE AND RESIZE TEXTURE TO THAT SIZE
            var data = GetCollectionData(tk2dSpriteCollection);

            using (var resizeContext = new ResizeTextureContext(data.MainTexture))
            {
                List <Texture2D>      spriteTextures = new List <Texture2D>();
                List <tk2DSpriteData> sprites        = new List <tk2DSpriteData>();
                //yield return null;

                foreach (var spriteData in ReadSpritesFromData(data))
                {
                    sprites.Add(spriteData);
                    spriteTextures.Add(spriteData.Texture);
                    yield return(null);
                }

                //Debug.Log("Creating Texture with = " + new Vector2Int(data.MainTexture.width,data.MainTexture.height));
                Texture2D atlas     = new Texture2D(data.MainTexture.width, data.MainTexture.height);
                string    atlasName = data.MainTexture.name + "_unpacked";
                atlas.name = atlasName;

                var uvCoords = atlas.PackTextures(spriteTextures.ToArray(), 1, Mathf.Max(data.MainTexture.width, data.MainTexture.height), false);
                //WeaverLog.Log("Atlas Size = " + atlas.width + " , " + atlas.height);
                var pngData = atlas.EncodeToPNG();

                if (fileLocation == null)
                {
                    fileLocation = "Assets\\" + atlasName + ".png";
                }
                else if (!fileLocation.EndsWith(".png"))
                {
                    fileLocation += "\\" + atlasName + ".png";
                }

                var fileInfo = new FileInfo(fileLocation);

                if (!fileInfo.Directory.Exists)
                {
                    fileInfo.Directory.Create();
                }

                using (var fileTest = File.Create(fileLocation))
                {
                    fileTest.Write(pngData, 0, pngData.GetLength(0));
                }
                AssetDatabase.ImportAsset(fileLocation);

                yield return(null);

                //DefaultTexturePlatform


                var importer         = (TextureImporter)AssetImporter.GetAtPath(fileLocation);
                var platformSettings = importer.GetPlatformTextureSettings("DefaultTexturePlatform");
                platformSettings.maxTextureSize = Mathf.Max(data.MainTexture.width, data.MainTexture.height);
                importer.SetPlatformTextureSettings(platformSettings);

                float averagePPU = 0f;

                foreach (var sprite in sprites)
                {
                    averagePPU += sprite.PixelsPerUnit;
                    //yield return null;
                }
                averagePPU /= sprites.Count;


                importer.spriteImportMode    = SpriteImportMode.Multiple;
                importer.spritePixelsPerUnit = averagePPU;

                List <SpriteMetaData> metas = new List <SpriteMetaData>();

                Dictionary <Rect, int> rectToId = new Dictionary <Rect, int>();

                //foreach (var sprite in sprites)
                for (int i = 0; i < sprites.Count; i++)
                {
                    var sprite = sprites[i];
                    var uv     = uvCoords[i];
                    //var textureSize = new Vector2(sprite.Texture.width, sprite.Texture.height);
                    //WeaverLog.Log("Sprite Coords for " + sprite.Texture.name + " = " + uvCoords[i]);
                    metas.Add(new SpriteMetaData()
                    {
                        name      = sprite.Name,
                        border    = Vector4.zero,
                        pivot     = new Vector2(1 - sprite.Pivot.x, 1 - sprite.Pivot.y),
                        alignment = (int)SpriteAlignment.Custom,
                        rect      = new Rect(uv.x * atlas.width, uv.y * atlas.height, uv.width * atlas.width, uv.height * atlas.height)
                    });

                    rectToId.Add(metas[metas.Count - 1].rect, i);
                }
                importer.spritesheet = metas.ToArray();

                Debug.Log("Unpacked " + metas.Count + " different sprites");

                importer.SaveAndReimport();

                yield return(null);

                EditorUtility.ClearProgressBar();

                var finalTexture = AssetDatabase.LoadAssetAtPath <Texture2D>(fileLocation);

                if (resultSprites != null)
                {
                    var package = new SpritePackage(finalTexture);

                    var finalSprites = GetSpritesFromTexture(finalTexture);

                    foreach (var finalSprite in finalSprites)
                    {
                        var definition = data.Definitions.FirstOrDefault(d => d.Name == finalSprite.name);

                        if (definition.Positions != default(Vector2[]))
                        {
                            package.AddSprite(Array.IndexOf(data.Definitions, definition), finalSprite);
                        }
                    }

                    /*foreach (var finalSprite in finalSprites)
                     * {
                     *      if (rectToId.ContainsKey(finalSprite.rect))
                     *      {
                     *              //spriteList[rectToId[finalSprite.rect]] = finalSprite;
                     *              package.AddSprite(rectToId[finalSprite.rect], finalSprite);
                     *      }
                     * }*/

                    resultSprites.Value = package;
                }
            }
        }