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);
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);