public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
    {
        RegionAttachment regionAttachment = new RegionAttachment(name);
        Texture2D        texture          = this.sprite.get_texture();
        int         instanceID            = texture.GetInstanceID();
        AtlasRegion atlasRegion;

        if (SpriteAttachmentLoader.atlasTable.ContainsKey(instanceID))
        {
            atlasRegion = SpriteAttachmentLoader.atlasTable.get_Item(instanceID);
        }
        else
        {
            Material material = new Material(this.shader);
            if (this.sprite.get_packed())
            {
                material.set_name("Unity Packed Sprite Material");
            }
            else
            {
                material.set_name(this.sprite.get_name() + " Sprite Material");
            }
            material.set_mainTexture(texture);
            atlasRegion      = new AtlasRegion();
            atlasRegion.page = new AtlasPage
            {
                rendererObject = material
            };
            SpriteAttachmentLoader.atlasTable.set_Item(instanceID, atlasRegion);
        }
        Rect textureRect = this.sprite.get_textureRect();

        textureRect.set_x(Mathf.InverseLerp(0f, (float)texture.get_width(), textureRect.get_x()));
        textureRect.set_y(Mathf.InverseLerp(0f, (float)texture.get_height(), textureRect.get_y()));
        textureRect.set_width(Mathf.InverseLerp(0f, (float)texture.get_width(), textureRect.get_width()));
        textureRect.set_height(Mathf.InverseLerp(0f, (float)texture.get_height(), textureRect.get_height()));
        Bounds  bounds = this.sprite.get_bounds();
        Vector3 size   = bounds.get_size();
        bool    rotate = false;

        if (this.sprite.get_packed())
        {
            rotate = (this.sprite.get_packingRotation() == 15);
        }
        regionAttachment.SetUVs(textureRect.get_xMin(), textureRect.get_yMax(), textureRect.get_xMax(), textureRect.get_yMin(), rotate);
        regionAttachment.RendererObject = atlasRegion;
        regionAttachment.SetColor(Color.get_white());
        regionAttachment.ScaleX               = 1f;
        regionAttachment.ScaleY               = 1f;
        regionAttachment.RegionOffsetX        = this.sprite.get_rect().get_width() * (0.5f - Mathf.InverseLerp(bounds.get_min().x, bounds.get_max().x, 0f)) / this.sprite.get_pixelsPerUnit();
        regionAttachment.RegionOffsetY        = this.sprite.get_rect().get_height() * (0.5f - Mathf.InverseLerp(bounds.get_min().y, bounds.get_max().y, 0f)) / this.sprite.get_pixelsPerUnit();
        regionAttachment.Width                = size.x;
        regionAttachment.Height               = size.y;
        regionAttachment.RegionWidth          = size.x;
        regionAttachment.RegionHeight         = size.y;
        regionAttachment.RegionOriginalWidth  = size.x;
        regionAttachment.RegionOriginalHeight = size.y;
        regionAttachment.UpdateOffset();
        return(regionAttachment);
    }
Exemplo n.º 2
0
        public static RegionAttachment ToRegionAttachment(this AtlasRegion region, string attachmentName, float scale = 0.01f)
        {
            if (string.IsNullOrEmpty(attachmentName))
            {
                throw new ArgumentException("attachmentName can't be null or empty.", "attachmentName");
            }
            if (region == null)
            {
                throw new ArgumentNullException("region");
            }
            RegionAttachment regionAttachment = new RegionAttachment(attachmentName);

            regionAttachment.RendererObject = region;
            regionAttachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
            regionAttachment.regionOffsetX        = region.offsetX;
            regionAttachment.regionOffsetY        = region.offsetY;
            regionAttachment.regionWidth          = region.width;
            regionAttachment.regionHeight         = region.height;
            regionAttachment.regionOriginalWidth  = region.originalWidth;
            regionAttachment.regionOriginalHeight = region.originalHeight;
            regionAttachment.Path     = region.name;
            regionAttachment.scaleX   = 1f;
            regionAttachment.scaleY   = 1f;
            regionAttachment.rotation = 0f;
            regionAttachment.r        = 1f;
            regionAttachment.g        = 1f;
            regionAttachment.b        = 1f;
            regionAttachment.a        = 1f;
            regionAttachment.width    = regionAttachment.regionOriginalWidth * scale;
            regionAttachment.height   = regionAttachment.regionOriginalHeight * scale;
            regionAttachment.SetColor(Color.white);
            regionAttachment.UpdateOffset();
            return(regionAttachment);
        }
Exemplo n.º 3
0
        public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
        {
            RegionAttachment regionAttachment = new RegionAttachment(name);
            Texture2D        texture          = this.sprite.texture;
            int         instanceID            = texture.GetInstanceID();
            AtlasRegion atlasRegion;

            if (!SpriteAttachmentLoader.atlasTable.TryGetValue(instanceID, out atlasRegion))
            {
                Material material = new Material(this.shader);
                if (this.sprite.packed)
                {
                    material.name = "Unity Packed Sprite Material";
                }
                else
                {
                    material.name = this.sprite.name + " Sprite Material";
                }
                material.mainTexture = texture;
                atlasRegion          = new AtlasRegion();
                AtlasPage atlasPage = new AtlasPage();
                atlasPage.rendererObject = material;
                atlasRegion.page         = atlasPage;
                SpriteAttachmentLoader.atlasTable[instanceID] = atlasRegion;
            }
            Rect textureRect = this.sprite.textureRect;

            textureRect.x      = Mathf.InverseLerp(0f, (float)texture.width, textureRect.x);
            textureRect.y      = Mathf.InverseLerp(0f, (float)texture.height, textureRect.y);
            textureRect.width  = Mathf.InverseLerp(0f, (float)texture.width, textureRect.width);
            textureRect.height = Mathf.InverseLerp(0f, (float)texture.height, textureRect.height);
            Bounds  bounds  = this.sprite.bounds;
            Vector2 vector  = bounds.min;
            Vector2 vector2 = bounds.max;
            Vector2 vector3 = bounds.size;
            float   num     = 1f / this.sprite.pixelsPerUnit;
            bool    rotate  = false;

            if (this.sprite.packed)
            {
                rotate = (this.sprite.packingRotation == SpritePackingRotation.Any);
            }
            regionAttachment.SetUVs(textureRect.xMin, textureRect.yMax, textureRect.xMax, textureRect.yMin, rotate);
            regionAttachment.RendererObject = atlasRegion;
            regionAttachment.SetColor(Color.white);
            regionAttachment.ScaleX               = 1f;
            regionAttachment.ScaleY               = 1f;
            regionAttachment.RegionOffsetX        = this.sprite.rect.width * (0.5f - SpriteAttachmentLoader.InverseLerp(vector.x, vector2.x, 0f)) * num;
            regionAttachment.RegionOffsetY        = this.sprite.rect.height * (0.5f - SpriteAttachmentLoader.InverseLerp(vector.y, vector2.y, 0f)) * num;
            regionAttachment.Width                = vector3.x;
            regionAttachment.Height               = vector3.y;
            regionAttachment.RegionWidth          = vector3.x;
            regionAttachment.RegionHeight         = vector3.y;
            regionAttachment.RegionOriginalWidth  = vector3.x;
            regionAttachment.RegionOriginalHeight = vector3.y;
            regionAttachment.UpdateOffset();
            return(regionAttachment);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a new RegionAttachment from a given AtlasRegion.</summary>
        public static RegionAttachment ToRegionAttachment(this AtlasRegion region, string attachmentName, float scale = 0.01f, float rotation = 0f)
        {
            if (string.IsNullOrEmpty(attachmentName))
            {
                throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
            }
            if (region == null)
            {
                throw new System.ArgumentNullException("region");
            }

            // (AtlasAttachmentLoader.cs)
            var attachment = new RegionAttachment(attachmentName);

            attachment.RendererObject = region;
            attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
            attachment.regionOffsetX        = region.offsetX;
            attachment.regionOffsetY        = region.offsetY;
            attachment.regionWidth          = region.width;
            attachment.regionHeight         = region.height;
            attachment.regionOriginalWidth  = region.originalWidth;
            attachment.regionOriginalHeight = region.originalHeight;

            attachment.Path     = region.name;
            attachment.scaleX   = 1;
            attachment.scaleY   = 1;
            attachment.rotation = rotation;

            attachment.r = 1;
            attachment.g = 1;
            attachment.b = 1;
            attachment.a = 1;

            // pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
            attachment.width  = attachment.regionOriginalWidth * scale;
            attachment.height = attachment.regionOriginalHeight * scale;

            attachment.SetColor(Color.white);
            attachment.UpdateOffset();
            return(attachment);
        }
Exemplo n.º 5
0
        private void Apply(SkeletonRenderer skeletonRenderer)
        {
            this.atlas = this.atlasAsset.GetAtlas();
            AtlasAttachmentLoader atlasAttachmentLoader = new AtlasAttachmentLoader(new Atlas[]
            {
                this.atlas
            });
            float scale = skeletonRenderer.skeletonDataAsset.scale;

            foreach (object obj in this.attachments)
            {
                SlotRegionPair   slotRegionPair   = (SlotRegionPair)obj;
                RegionAttachment regionAttachment = atlasAttachmentLoader.NewRegionAttachment(null, slotRegionPair.region, slotRegionPair.region);
                regionAttachment.Width  = regionAttachment.RegionOriginalWidth * scale;
                regionAttachment.Height = regionAttachment.RegionOriginalHeight * scale;
                regionAttachment.SetColor(new Color(1f, 1f, 1f, 1f));
                regionAttachment.UpdateOffset();
                Slot slot = skeletonRenderer.skeleton.FindSlot(slotRegionPair.slot);
                slot.Attachment = regionAttachment;
            }
        }
Exemplo n.º 6
0
    private void Apply(SkeletonRenderer skeletonRenderer)
    {
        this.atlas = this.atlasAsset.GetAtlas();
        AtlasAttachmentLoader atlasAttachmentLoader = new AtlasAttachmentLoader(new Atlas[]
        {
            this.atlas
        });
        float       scale      = skeletonRenderer.skeletonDataAsset.scale;
        IEnumerator enumerator = this.attachments.GetEnumerator();

        while (enumerator.MoveNext())
        {
            AtlasRegionAttacher.SlotRegionPair slotRegionPair = (AtlasRegionAttacher.SlotRegionPair)enumerator.get_Current();
            RegionAttachment regionAttachment = atlasAttachmentLoader.NewRegionAttachment(null, slotRegionPair.region, slotRegionPair.region);
            regionAttachment.Width  = regionAttachment.RegionOriginalWidth * scale;
            regionAttachment.Height = regionAttachment.RegionOriginalHeight * scale;
            regionAttachment.SetColor(new Color(1f, 1f, 1f, 1f));
            regionAttachment.UpdateOffset();
            Slot slot = skeletonRenderer.skeleton.FindSlot(slotRegionPair.slot);
            slot.Attachment = regionAttachment;
        }
    }
Exemplo n.º 7
0
        public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
        {
            RegionAttachment attachment = new RegionAttachment(name);

            Texture2D   tex        = sprite.texture;
            int         instanceId = tex.GetInstanceID();
            AtlasRegion atlasRegion;

            //check cache first
            if (atlasTable.ContainsKey(instanceId))
            {
                atlasRegion = atlasTable[instanceId];
            }
            else
            {
                //Setup new material
                Material mat = new Material(shader);
                if (sprite.packed)
                {
                    mat.name = "Unity Packed Sprite Material";
                }
                else
                {
                    mat.name = sprite.name + " Sprite Material";
                }
                mat.mainTexture = tex;

                //create faux-region to play nice with SkeletonRenderer
                atlasRegion = new AtlasRegion();
                AtlasPage page = new AtlasPage();
                page.rendererObject = mat;
                atlasRegion.page    = page;

                //cache it
                atlasTable[instanceId] = atlasRegion;
            }

            Rect texRect = sprite.textureRect;

            //normalize rect to UV space of packed atlas
            texRect.x      = Mathf.InverseLerp(0, tex.width, texRect.x);
            texRect.y      = Mathf.InverseLerp(0, tex.height, texRect.y);
            texRect.width  = Mathf.InverseLerp(0, tex.width, texRect.width);
            texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height);

            Bounds  bounds = sprite.bounds;
            Vector3 size   = bounds.size;

            //TODO: make sure this rotation thing actually works
            bool rotated = false;

            if (sprite.packed)
            {
                rotated = sprite.packingRotation == SpritePackingRotation.Any;
            }

            //do some math and assign UVs and sizes
            attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated);
            attachment.RendererObject = atlasRegion;
            attachment.SetColor(Color.white);
            attachment.ScaleX               = 1;
            attachment.ScaleY               = 1;
            attachment.RegionOffsetX        = sprite.rect.width * (0.5f - Mathf.InverseLerp(bounds.min.x, bounds.max.x, 0)) / sprite.pixelsPerUnit;
            attachment.RegionOffsetY        = sprite.rect.height * (0.5f - Mathf.InverseLerp(bounds.min.y, bounds.max.y, 0)) / sprite.pixelsPerUnit;
            attachment.Width                = size.x;
            attachment.Height               = size.y;
            attachment.RegionWidth          = size.x;
            attachment.RegionHeight         = size.y;
            attachment.RegionOriginalWidth  = size.x;
            attachment.RegionOriginalHeight = size.y;
            attachment.UpdateOffset();

            return(attachment);
        }
		/// <summary>
		/// Creates a new RegionAttachment from a given AtlasRegion.</summary>
		public static RegionAttachment ToRegionAttachment (this AtlasRegion region, string attachmentName, float scale = 0.01f) {
			if (string.IsNullOrEmpty(attachmentName)) throw new System.ArgumentException("attachmentName can't be null or empty.", "attachmentName");
			if (region == null) throw new System.ArgumentNullException("region");

			// (AtlasAttachmentLoader.cs)
			var attachment = new RegionAttachment(attachmentName);

			attachment.RendererObject = region;
			attachment.SetUVs(region.u, region.v, region.u2, region.v2, region.rotate);
			attachment.regionOffsetX = region.offsetX;
			attachment.regionOffsetY = region.offsetY;
			attachment.regionWidth = region.width;
			attachment.regionHeight = region.height;
			attachment.regionOriginalWidth = region.originalWidth;
			attachment.regionOriginalHeight = region.originalHeight;

			attachment.Path = region.name;
			attachment.scaleX = 1;
			attachment.scaleY = 1;
			attachment.rotation = 0;

			// pass OriginalWidth and OriginalHeight because UpdateOffset uses it in its calculation.
			attachment.width = attachment.regionOriginalWidth * scale;
			attachment.height = attachment.regionOriginalHeight * scale;

			attachment.SetColor(Color.white);
			attachment.UpdateOffset();
			return attachment;
		}
Exemplo n.º 9
0
        public RegionAttachment NewRegionAttachment(Skin skin, string name, string path)
        {
            RegionAttachment attachment = new RegionAttachment(name);

            Texture2D   tex        = sprite.texture;
            int         instanceId = tex.GetInstanceID();
            AtlasRegion atlasRegion;
            bool        cachedMaterialExists = atlasTable.TryGetValue(instanceId, out atlasRegion);

            if (!cachedMaterialExists)
            {
                // Setup new material.
                var material = new Material(shader);
                if (sprite.packed)
                {
                    material.name = "Unity Packed Sprite Material";
                }
                else
                {
                    material.name = sprite.name + " Sprite Material";
                }
                material.mainTexture = tex;

                // Create faux-region to play nice with SkeletonRenderer.
                atlasRegion = new AtlasRegion();
                var page = new AtlasPage();
                page.rendererObject = material;
                atlasRegion.page    = page;

                // Cache it.
                atlasTable[instanceId] = atlasRegion;
            }

            Rect texRect = sprite.textureRect;

            // Normalize rect to UV space of packed atlas
            texRect.x      = Mathf.InverseLerp(0, tex.width, texRect.x);
            texRect.y      = Mathf.InverseLerp(0, tex.height, texRect.y);
            texRect.width  = Mathf.InverseLerp(0, tex.width, texRect.width);
            texRect.height = Mathf.InverseLerp(0, tex.height, texRect.height);

            Bounds  bounds = sprite.bounds;
            Vector2 boundsMin = bounds.min, boundsMax = bounds.max;
            Vector2 size                = bounds.size;
            float   spriteUnitsPerPixel = 1f / sprite.pixelsPerUnit;

            bool rotated = false;

            if (sprite.packed)
            {
                rotated = sprite.packingRotation == SpritePackingRotation.Any;
            }

            attachment.SetUVs(texRect.xMin, texRect.yMax, texRect.xMax, texRect.yMin, rotated);
            attachment.RendererObject = atlasRegion;
            attachment.SetColor(Color.white);
            attachment.ScaleX               = 1;
            attachment.ScaleY               = 1;
            attachment.RegionOffsetX        = sprite.rect.width * (0.5f - InverseLerp(boundsMin.x, boundsMax.x, 0)) * spriteUnitsPerPixel;
            attachment.RegionOffsetY        = sprite.rect.height * (0.5f - InverseLerp(boundsMin.y, boundsMax.y, 0)) * spriteUnitsPerPixel;
            attachment.Width                = size.x;
            attachment.Height               = size.y;
            attachment.RegionWidth          = size.x;
            attachment.RegionHeight         = size.y;
            attachment.RegionOriginalWidth  = size.x;
            attachment.RegionOriginalHeight = size.y;
            attachment.UpdateOffset();

            return(attachment);
        }