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