- Home /
Question by
BugXHunter · Nov 05, 2015 at 01:04 PM ·
c#canvastagsrecttransformoffset
How can I set the anchor tags to the center of it's own rectTransform rather than the parent's rectTransfom?
This is what I have so far, any time I try something it just sends it flying off the screen:
if (mapIcon == null) return;
Vector2 newAnchorMin = new Vector2(mapIcon.anchorMin.x + mapIcon.offsetMin.x / map.rect.width,
mapIcon.anchorMin.y + mapIcon.offsetMin.y / map.rect.height);
Vector2 newAnchorMax = new Vector2(mapIcon.anchorMax.x + mapIcon.offsetMax.x / map.rect.width,
mapIcon.anchorMax.y + mapIcon.offsetMax.y / map.rect.height);
mapIcon.anchorMin = newAnchorMin;
mapIcon.anchorMax = newAnchorMax;
mapIcon.position = new Vector2(0, 0);
Comment
Answer by BugXHunter · Nov 10, 2015 at 08:11 AM
I figured it out:
//Setting the Anchor tags to the center of the object, so that they don't scale with the size of the parent panel.
Vector2 newAnchorMin = new Vector2(mapIcon.anchorMin.x + (mapIcon.offsetMin.x + mapIcon.rect.width/2) / map.rect.width,
mapIcon.anchorMin.y + (mapIcon.offsetMin.y + mapIcon.rect.height/2) / map.rect.height);
Vector2 newAnchorMax = new Vector2(mapIcon.anchorMax.x + (mapIcon.offsetMax.x - mapIcon.rect.width/2) / map.rect.width,
mapIcon.anchorMax.y + (mapIcon.offsetMax.y - mapIcon.rect.height/2) / map.rect.height);
mapIcon.anchorMin = newAnchorMin;
mapIcon.anchorMax = newAnchorMax;
//Set the size of the rectangle by using the offset from the center.
mapIcon.offsetMax = new Vector2(xOffset, yOffset);
mapIcon.offsetMin = new Vector2(-xOffset, -yOffset);
Your answer
![](https://koobas.hobune.stream/wayback/20220612043941im_/https://answers.unity.com/themes/thub/images/avi.jpg)