public RegionAttachment NewRegionAttachment(Skin skin, String name, String path) { FAtlasElement element = Futile.atlasManager.GetElementWithName(name); RegionAttachment attachment = new RegionAttachment(name); if (element != null) { attachment.RendererObject = element; attachment.SetUVs(element.uvBottomLeft.x, element.uvBottomLeft.y, element.uvTopRight.x, element.uvTopRight.y, false); attachment.RegionOffsetX = 0.0f; attachment.RegionOffsetY = 0.0f; attachment.RegionWidth = element.sourceSize.x; attachment.RegionHeight = element.sourceSize.y; attachment.RegionOriginalWidth = element.sourceSize.x; attachment.RegionOriginalHeight = element.sourceSize.y; } else { attachment.RendererObject = null; attachment.SetUVs(0.0f, 0.0f, 0.0f, 0.0f, false); attachment.RegionOffsetX = 0.0f; attachment.RegionOffsetY = 0.0f; attachment.RegionWidth = 0.0f; attachment.RegionHeight = 0.0f; attachment.RegionOriginalWidth = 0.0f; attachment.RegionOriginalHeight = 0.0f; Debug.Log("Element [" + name + "] not found in Futile.AtlasManager"); } return(attachment); }
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); }
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); }
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); }
// this is the attachment loader which will hook into Futile's AtlasManager for the attachment/element data public Attachment NewAttachment(Skin skin, AttachmentType type, String name) { switch (type) { case AttachmentType.region: FAtlasElement element = Futile.atlasManager.GetElementWithName(name); RegionAttachment attachment = new RegionAttachment(name); if (element != null) { attachment.Texture = element; attachment.SetUVs(element.uvBottomLeft.x, element.uvBottomLeft.y, element.uvTopRight.x, element.uvTopRight.y, false); attachment.RegionOffsetX = 0.0f; attachment.RegionOffsetY = 0.0f; attachment.RegionWidth = element.sourceSize.x; attachment.RegionHeight = element.sourceSize.y; attachment.RegionOriginalWidth = element.sourceSize.x; attachment.RegionOriginalHeight = element.sourceSize.y; } else { attachment.Texture = null; attachment.SetUVs(0.0f, 0.0f, 0.0f, 0.0f, false); attachment.RegionOffsetX = 0.0f; attachment.RegionOffsetY = 0.0f; attachment.RegionWidth = 0.0f; attachment.RegionHeight = 0.0f; attachment.RegionOriginalWidth = 0.0f; attachment.RegionOriginalHeight = 0.0f; Debug.Log("Element [" + name + "] not found in Futile.AtlasManager"); } return(attachment); } throw new Exception("Unknown attachment type: " + type); }
private void addSkeletonAttachment(string slotName, string attachmentName, TextureData textureData) { RegionAttachment regionAttachment = new RegionAttachment(attachmentName); regionAttachment.RendererObject = textureData.Texture; regionAttachment.Width = regionAttachment.RegionWidth = regionAttachment.RegionOriginalWidth = textureData.Texture.Width; regionAttachment.Height = regionAttachment.RegionHeight = regionAttachment.RegionOriginalHeight = textureData.Texture.Height; regionAttachment.RegionOffsetX = textureData.OriginX; regionAttachment.RegionOffsetY = textureData.OriginY; regionAttachment.Rotation = textureData.Rotation; regionAttachment.SetUVs(0, 0, 1, 1, false); regionAttachment.UpdateOffset(); Skeleton.Data.FindSkin("default").AddAttachment(Skeleton.FindSlotIndex(slotName), attachmentName, regionAttachment); }
public RegionAttachment NewRegionAttachment (Skin skin, String name, String path) { ProcessSpriteDefinition(path); RegionAttachment region = new RegionAttachment(name); region.Path = path; region.RendererObject = material; region.SetUVs(u, v, u2, v2, regionRotated); region.RegionOriginalWidth = regionOriginalWidth; region.RegionOriginalHeight = regionOriginalHeight; region.RegionWidth = regionWidth; region.RegionHeight = regionHeight; region.RegionOffsetX = regionOffsetX; region.RegionOffsetY = regionOffsetY; return region; }
public static void SetRegion(this RegionAttachment attachment, AtlasRegion region, bool updateOffset = true) { if (region == null) { throw new ArgumentNullException("region"); } 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; if (updateOffset) { attachment.UpdateOffset(); } }
/// <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); }
public static Material CreateRegionAttackmentByTexture(Slot slot, Texture2D tex) { Debug.Log("准备替换图片"); if (slot == null || tex == null) { return(null); } RegionAttachment attackment = slot.Attachment as RegionAttachment; if (attackment == null) { return(null); } Debug.Log("替换图片"); attackment.RendererObject = CreateRegion(tex); attackment.SetUVs(0f, 1f, 1f, 0f, false); Material mat = new Material(Shader.Find("Sprites/Default")); mat.mainTexture = tex; (attackment.RendererObject as AtlasRegion).page.rendererObject = mat; // UpdateOffsetByTexture2D (attackment,tex); slot.Attachment = attackment; return(mat); }
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); }
public Attachment NewAttachment(Skin skin, AttachmentType type, String name) { if (type != AttachmentType.region) { throw new Exception("Unknown attachment type: " + type); } // Strip folder names. int index = name.LastIndexOfAny(new char[] { '/', '\\' }); if (index != -1) { name = name.Substring(index + 1); } tk2dSpriteDefinition attachmentParameters = null; for (int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) { tk2dSpriteDefinition def = sprites.inst.spriteDefinitions[i]; if (def.name == name) { attachmentParameters = def; break; } } if (attachmentParameters == null) { throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); } if (attachmentParameters.complexGeometry) { throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")"); } if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) { throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")"); } Vector2 minTexCoords = Vector2.one; Vector2 maxTexCoords = Vector2.zero; for (int i = 0; i < attachmentParameters.uvs.Length; ++i) { Vector2 uv = attachmentParameters.uvs[i]; minTexCoords = Vector2.Min(minTexCoords, uv); maxTexCoords = Vector2.Max(maxTexCoords, uv); } Texture texture = attachmentParameters.material.mainTexture; int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * texture.width); int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * texture.height); bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d); if (rotated) { float temp = minTexCoords.x; minTexCoords.x = maxTexCoords.x; maxTexCoords.x = temp; } RegionAttachment attachment = new RegionAttachment(name); attachment.SetUVs( minTexCoords.x, maxTexCoords.y, maxTexCoords.x, minTexCoords.y, rotated ); // TODO - Set attachment.RegionOffsetX/Y. What units does attachmentParameters.untrimmedBoundsData use?! attachment.RegionWidth = width; attachment.RegionHeight = height; attachment.RegionOriginalWidth = width; attachment.RegionOriginalHeight = height; return(attachment); }
public Attachment NewAttachment(Skin skin, AttachmentType type, String name) { switch (type) { case AttachmentType.region: break; case AttachmentType.boundingbox: return(new BoundingBoxAttachment(name)); default: throw new Exception("Unknown attachment type: " + type); } // Strip folder names. int index = name.LastIndexOfAny(new char[] { '/', '\\' }); if (index != -1) { name = name.Substring(index + 1); } tk2dSpriteDefinition def = sprites.inst.GetSpriteDefinition(name); if (def == null) { throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); } if (def.complexGeometry) { throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")"); } if (def.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) { throw new NotImplementedException("Only 2D Toolkit atlases are supported: " + name + " (" + type + ")"); } RegionAttachment attachment = new RegionAttachment(name); Vector2 minTexCoords = Vector2.one; Vector2 maxTexCoords = Vector2.zero; for (int i = 0; i < def.uvs.Length; ++i) { Vector2 uv = def.uvs[i]; minTexCoords = Vector2.Min(minTexCoords, uv); maxTexCoords = Vector2.Max(maxTexCoords, uv); } bool rotated = def.flipped == tk2dSpriteDefinition.FlipMode.Tk2d; if (rotated) { float temp = minTexCoords.x; minTexCoords.x = maxTexCoords.x; maxTexCoords.x = temp; } attachment.SetUVs( minTexCoords.x, maxTexCoords.y, maxTexCoords.x, minTexCoords.y, rotated ); attachment.RegionOriginalWidth = (int)(def.untrimmedBoundsData[1].x / def.texelSize.x); attachment.RegionOriginalHeight = (int)(def.untrimmedBoundsData[1].y / def.texelSize.y); attachment.RegionWidth = (int)(def.boundsData[1].x / def.texelSize.x); attachment.RegionHeight = (int)(def.boundsData[1].y / def.texelSize.y); float x0 = def.untrimmedBoundsData[0].x - def.untrimmedBoundsData[1].x / 2; float x1 = def.boundsData[0].x - def.boundsData[1].x / 2; attachment.RegionOffsetX = (int)((x1 - x0) / def.texelSize.x); float y0 = def.untrimmedBoundsData[0].y - def.untrimmedBoundsData[1].y / 2; float y1 = def.boundsData[0].y - def.boundsData[1].y / 2; attachment.RegionOffsetY = (int)((y1 - y0) / def.texelSize.y); attachment.RendererObject = def.material; 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; }
public Attachment NewAttachment(Skin skin, AttachmentType type, String name) { if (type != AttachmentType.region) { throw new Exception("Unknown attachment type: " + type); } tk2dSpriteDefinition attachmentParameters = null; for (int i = 0; i < sprites.inst.spriteDefinitions.Length; ++i) { tk2dSpriteDefinition def = sprites.inst.spriteDefinitions[i]; if (def.name == name) { attachmentParameters = def; break; } } if (attachmentParameters == null) { throw new Exception("Sprite not found in atlas: " + name + " (" + type + ")"); } if (attachmentParameters.complexGeometry) { throw new NotImplementedException("Complex geometry is not supported: " + name + " (" + type + ")"); } if (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.TPackerCW) { throw new NotImplementedException("Only 2d toolkit atlases are supported: " + name + " (" + type + ")"); } Texture tex = attachmentParameters.material.mainTexture; Vector2 minTexCoords = Vector2.one; Vector2 maxTexCoords = Vector2.zero; for (int i = 0; i < attachmentParameters.uvs.Length; ++i) { Vector2 uv = attachmentParameters.uvs[i]; minTexCoords = Vector2.Min(minTexCoords, uv); maxTexCoords = Vector2.Max(maxTexCoords, uv); } int width = (int)(Mathf.Abs(maxTexCoords.x - minTexCoords.x) * tex.width); int height = (int)(Mathf.Abs(maxTexCoords.y - minTexCoords.y) * tex.height); bool rotated = (attachmentParameters.flipped == tk2dSpriteDefinition.FlipMode.Tk2d); if (rotated) { float temp = minTexCoords.x; minTexCoords.x = maxTexCoords.x; maxTexCoords.x = temp; } RegionAttachment attachment = new RegionAttachment(name); attachment.SetUVs( minTexCoords.x, maxTexCoords.y, maxTexCoords.x, minTexCoords.y, rotated ); attachment.RegionWidth = width; attachment.RegionHeight = height; attachment.RegionOriginalWidth = width; attachment.RegionOriginalHeight = height; return(attachment); }
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); }