private void SetUpCustomAnchors(RectTransform rectTransform, CustomAnchorTypes customAnchorType)
    {
        Vector2 pivot = rectTransform.pivot;

        //rectTransform.pivot = new Vector2(0.5f, 0.5f);


        var absoluteSize              = RectTransformUtilities.GetAbsoluteSize(rectTransform);
        var absolutePos               = RectTransformUtilities.GetAbsolutePosition(rectTransform);
        var parentAbsoluteSize        = RectTransformUtilities.GetAbsoluteSize(rectTransform.parent.GetComponent <RectTransform>());
        var offsetForAnchors          = new Vector2((0.5f - rectTransform.pivot.x) * rectTransform.sizeDelta.x, (0.5f - rectTransform.pivot.y) * rectTransform.sizeDelta.y);
        var offsetForAnchoredPosition = new Vector2((0.5f - rectTransform.pivot.x) * absoluteSize.x, (0.5f - rectTransform.pivot.y) * absoluteSize.y);

        Debug.Log(offsetForAnchors);
        Debug.Log(offsetForAnchoredPosition);

        absolutePos        += offsetForAnchors;
        rectTransform.pivot = new Vector2(0.5f, 0.5f);

        rectTransform.anchorMin = new Vector2();
        rectTransform.anchorMax = new Vector2();

        rectTransform.anchorMin = CustomAnchorMin(absoluteSize, absolutePos, parentAbsoluteSize, rectTransform.pivot, customAnchorType);
        rectTransform.anchorMax = CustomAnchorMax(absoluteSize, absolutePos, parentAbsoluteSize, rectTransform.pivot, customAnchorType);

        rectTransform.anchoredPosition = CustomAnchorPosition(absoluteSize, customAnchorType, offsetForAnchoredPosition);
        rectTransform.sizeDelta        = CustomAnchorSize(absoluteSize, customAnchorType);

        rectTransform.pivot = pivot;
    }
    private Vector2 CustomAnchorSize(Vector2 absoluteSize, CustomAnchorTypes customAnchorType)
    {
        switch (customAnchorType)
        {
        case CustomAnchorTypes.OnCorners:
            return(new Vector2());

        case CustomAnchorTypes.StretchX:
            return(new Vector2(0f, absoluteSize.y));

        case CustomAnchorTypes.StretchX_Bottom:
            return(new Vector2(0f, absoluteSize.y));

        case CustomAnchorTypes.StretchX_Top:
            return(new Vector2(0f, absoluteSize.y));

        case CustomAnchorTypes.StretchY:
            return(new Vector2(absoluteSize.x, 0f));

        case CustomAnchorTypes.StretchY_Left:
            return(new Vector2(absoluteSize.x, 0f));

        case CustomAnchorTypes.StretchY_Right:
            return(new Vector2(absoluteSize.x, 0f));

        case CustomAnchorTypes.LeftTopCorner:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.Top:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.RightTopCorner:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.Left:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.Center:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.Right:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.LeftBottomCorner:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.Bottom:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        case CustomAnchorTypes.RightBottomCorner:
            return(new Vector2(absoluteSize.x, absoluteSize.y));

        default:
            return(new Vector2());
        }
    }
    private Vector2 CustomAnchorPosition(Vector2 absoluteSize, CustomAnchorTypes customAnchorType, Vector2 offset)
    {
        switch (customAnchorType)
        {
        case CustomAnchorTypes.OnCorners:
            return(new Vector2());

        case CustomAnchorTypes.StretchX:
            return(new Vector2(0, -offset.y));

        case CustomAnchorTypes.StretchX_Bottom:
            return(new Vector2(0f, absoluteSize.y / 2 - offset.y));

        case CustomAnchorTypes.StretchX_Top:
            return(new Vector2(0f, -absoluteSize.y / 2 - offset.y));

        case CustomAnchorTypes.StretchY:
            return(new Vector2(-offset.x, 0));

        case CustomAnchorTypes.StretchY_Left:
            return(new Vector2(absoluteSize.x / 2 - offset.x, 0));

        case CustomAnchorTypes.StretchY_Right:
            return(new Vector2(-absoluteSize.x / 2 - offset.x, 0));

        case CustomAnchorTypes.LeftTopCorner:
            return(new Vector2(absoluteSize.x / 2, -absoluteSize.y / 2) - offset);

        case CustomAnchorTypes.Top:
            return(new Vector2(0f, -absoluteSize.y / 2) - offset);

        case CustomAnchorTypes.RightTopCorner:
            return(new Vector2(-absoluteSize.x / 2, -absoluteSize.y / 2) - offset);

        case CustomAnchorTypes.Left:
            return(new Vector2(absoluteSize.x / 2, 0f) - offset);

        case CustomAnchorTypes.Center:
            return(new Vector2() - offset);

        case CustomAnchorTypes.Right:
            return(new Vector2(-absoluteSize.x / 2, 0f) - offset);

        case CustomAnchorTypes.LeftBottomCorner:
            return(new Vector2(absoluteSize.x / 2, absoluteSize.y / 2) - offset);

        case CustomAnchorTypes.Bottom:
            return(new Vector2(0f, absoluteSize.y / 2) - offset);

        case CustomAnchorTypes.RightBottomCorner:
            return(new Vector2(-absoluteSize.x / 2, absoluteSize.y / 2) - offset);

        default:
            return(new Vector2() - offset);
        }
    }
    public void SetCutomAnchorsButton_OnClick(CustomAnchorTypes customAnchorType)
    {
        //Iterate over all targets in this editor
        foreach (var t in targets)
        {
            //Register changes made for specific rect transform
            Undo.RecordObject(t, ButtonText);

            //Set anchor which depends on given parameter customAnchorType
            SetUpCustomAnchors(t as RectTransform, customAnchorType);
        }
    }
    private Vector2 CustomAnchorMax(Vector2 absoluteSize, Vector2 absolutePos, Vector2 parentAbsoluteSize, Vector2 pivot,
                                    CustomAnchorTypes customAnchorType)
    {
        float anchorMaxX = 0f;
        float anchorMaxY = 0f;

        switch (customAnchorType)
        {
        case CustomAnchorTypes.OnCorners:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchX:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = absolutePos.y / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchX_Bottom:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y - absoluteSize.y * pivot.y) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchX_Top:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchY:
            anchorMaxX = absolutePos.x / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchY_Left:
            anchorMaxX = (absolutePos.x - absoluteSize.x * pivot.x) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.StretchY_Right:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.LeftTopCorner:
            anchorMaxX = (absolutePos.x - absoluteSize.x * pivot.x) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.Top:
            anchorMaxX = absolutePos.x / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.RightTopCorner:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y + absoluteSize.y * (1.0f - pivot.y)) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.Left:
            anchorMaxX = (absolutePos.x - absoluteSize.x * pivot.x) / parentAbsoluteSize.x;
            anchorMaxY = absolutePos.y / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.Center:
            anchorMaxX = absolutePos.x / parentAbsoluteSize.x;
            anchorMaxY = absolutePos.y / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.Right:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = absolutePos.y / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.LeftBottomCorner:
            anchorMaxX = (absolutePos.x - absoluteSize.x * pivot.x) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y - absoluteSize.y * pivot.y) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.Bottom:
            anchorMaxX = absolutePos.x / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y - absoluteSize.y * pivot.y) / parentAbsoluteSize.y;
            break;

        case CustomAnchorTypes.RightBottomCorner:
            anchorMaxX = (absolutePos.x + absoluteSize.x * (1.0f - pivot.x)) / parentAbsoluteSize.x;
            anchorMaxY = (absolutePos.y - absoluteSize.y * pivot.y) / parentAbsoluteSize.y;
            break;
        }
        return(new Vector2(anchorMaxX, anchorMaxY));
    }