Esempio n. 1
0
        public SpriteFlatData(string name, Texture2D texture, float width, float height, ScaleType scaleType)
        {
            this.name = name;

            float textureWidth  = texture.width;
            float textureHeight = texture.height;

            switch (scaleType)
            {
            case ScaleType.NONE:
                this.width  = textureWidth;
                this.height = textureHeight;
                break;

            case ScaleType.SCALED_WIDTH:
                this.width  = width;
                this.height = width * textureHeight / textureWidth;
                break;

            case ScaleType.SCALED_HEIGHT:
                this.width  = height * textureWidth / textureHeight;
                this.height = height;
                break;

            case ScaleType.SCALED:
            default:
                this.width  = width;
                this.height = height;
                break;
            }
            this.regionWidth  = textureWidth;
            this.regionHeight =
                texture.wrapMode == TextureWrapMode.Repeat ?
                this.height * textureWidth / this.width :
                textureHeight
            ;

            Rect    region = new Rect(0f, 0f, this.regionWidth, this.regionHeight);
            Vector2 anchor = new Vector2(this.regionWidth / 2, this.regionHeight / 2);

            tk2dRuntime.SpriteCollectionSize size = tk2dRuntime.SpriteCollectionSize.ForTk2dCamera();
            this.data = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, region, anchor);
            this.data.gameObject.name = String.Format("DataSpriteFlat{0}", name);
        }
Esempio n. 2
0
        // Texture packer import
        public static tk2dSpriteCollectionData CreateFromTexturePacker(tk2dRuntime.SpriteCollectionSize spriteCollectionSize, string texturePackerFileContents, Texture texture)
        {
            List <string>  names     = new List <string>();
            List <Rect>    rects     = new List <Rect>();
            List <Rect>    trimRects = new List <Rect>();
            List <Vector2> anchors   = new List <Vector2>();
            List <bool>    rotated   = new List <bool>();

            int state = 0;

            System.IO.TextReader tr = new System.IO.StringReader(texturePackerFileContents);

            // tmp state
            bool    entryRotated      = false;
            bool    entryTrimmed      = false;
            string  entryName         = "";
            Rect    entryRect         = new Rect();
            Rect    entryTrimData     = new Rect();
            Vector2 textureDimensions = Vector2.zero;
            Vector2 anchor            = Vector2.zero;

            // gonna write a non-allocating parser for this one day.
            // all these substrings & splits can't be good
            // but should be a tiny bit better than an xml / json parser...
            string line = tr.ReadLine();

            while (line != null)
            {
                if (line.Length > 0)
                {
                    char cmd = line[0];
                    switch (state)
                    {
                    case 0: {
                        switch (cmd)
                        {
                        case 'i': break;                                         // ignore version field for now

                        case 'w': textureDimensions.x = Int32.Parse(line.Substring(2)); break;

                        case 'h': textureDimensions.y = Int32.Parse(line.Substring(2)); break;

                        // total number of sprites would be ideal to statically allocate
                        case '~': state++; break;
                        }
                    }
                    break;

                    case 1: {
                        switch (cmd)
                        {
                        case 'n': entryName = line.Substring(2); break;

                        case 'r': entryRotated = Int32.Parse(line.Substring(2)) == 1; break;

                        case 's': {                                         // sprite
                            string[] tokens = line.Split();
                            entryRect.Set(Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]));
                        }
                        break;

                        case 'o': {                                         // origin
                            string[] tokens = line.Split();
                            entryTrimData.Set(Int32.Parse(tokens[1]), Int32.Parse(tokens[2]), Int32.Parse(tokens[3]), Int32.Parse(tokens[4]));
                            entryTrimmed = true;
                        }
                        break;

                        case '~': {
                            names.Add(entryName);
                            rotated.Add(entryRotated);
                            rects.Add(entryRect);
                            if (!entryTrimmed)
                            {
                                // The entryRect dimensions will be the wrong way around if the sprite is rotated
                                if (entryRotated)
                                {
                                    entryTrimData.Set(0, 0, entryRect.height, entryRect.width);
                                }
                                else
                                {
                                    entryTrimData.Set(0, 0, entryRect.width, entryRect.height);
                                }
                            }
                            trimRects.Add(entryTrimData);
                            anchor.Set((int)(entryTrimData.width / 2), (int)(entryTrimData.height / 2));
                            anchors.Add(anchor);
                            entryName    = "";
                            entryTrimmed = false;
                            entryRotated = false;
                        }
                        break;
                        }
                    }
                    break;
                    }
                }
                line = tr.ReadLine();
            }

            return(CreateFromTexture(
                       texture,
                       spriteCollectionSize,
                       textureDimensions,
                       names.ToArray(),
                       rects.ToArray(),
                       trimRects.ToArray(),
                       anchors.ToArray(),
                       rotated.ToArray()));
        }
Esempio n. 3
0
        public SpriteAnimData(string name, Texture2D texture, float width, float height, ScaleType scaleType, int frames, bool loop, float duration)
        {
            this.name   = name;
            this.frames = frames;

            float textureWidth  = texture.width / frames;
            float textureHeight = texture.height;

            switch (scaleType)
            {
            case ScaleType.NONE:
                this.width  = textureWidth;
                this.height = textureHeight;
                break;

            case ScaleType.SCALED_WIDTH:
                this.width  = width;
                this.height = width * textureHeight / textureWidth;
                break;

            case ScaleType.SCALED_HEIGHT:
                this.width  = height * textureWidth / textureHeight;
                this.height = height;
                break;

            case ScaleType.SCALED:
            default:
                this.width  = width;
                this.height = height;
                break;
            }
            this.regionWidth  = textureWidth;
            this.regionHeight =
                texture.wrapMode == TextureWrapMode.Repeat ?
                this.height * textureWidth / this.width :
                textureHeight
            ;

            string[]  names   = new string[frames];
            Rect[]    regions = new Rect[frames];
            Vector2[] anchors = new Vector2[frames];
            for (int i = 0; i < frames; i++)
            {
                names[i]   = String.Format("{0}_frame{1}", name, i);
                regions[i] = new Rect(this.regionWidth * i, 0f, this.regionWidth, this.regionHeight);
                anchors[i] = new Vector2(this.regionWidth / 2, this.regionHeight / 2);
            }

            tk2dRuntime.SpriteCollectionSize size = tk2dRuntime.SpriteCollectionSize.ForTk2dCamera();
            this.data = tk2dRuntime.SpriteCollectionGenerator.CreateFromTexture(texture, size, names, regions, anchors);
            this.data.gameObject.name = String.Format("DataSpriteAnim{0}", name);

            tk2dSpriteAnimationFrame[] animationFrames = new tk2dSpriteAnimationFrame[frames];
            for (int i = 0; i < frames; i++)
            {
                tk2dSpriteAnimationFrame frame = new tk2dSpriteAnimationFrame();
                frame.spriteCollection = this.data;
                frame.spriteId         = i;
                animationFrames[i]     = frame;
            }
            tk2dSpriteAnimationClip clip = new tk2dSpriteAnimationClip();

            clip.fps      = frames / duration;
            clip.name     = name;
            clip.wrapMode = loop ? tk2dSpriteAnimationClip.WrapMode.Loop : tk2dSpriteAnimationClip.WrapMode.Once;
            clip.frames   = animationFrames;

            this.anim       = this.data.gameObject.AddComponent <tk2dSpriteAnimation>();
            this.anim.clips = new tk2dSpriteAnimationClip[] { clip };
        }