//using UnityEngine;
//using UnityEditor;

//[InitializeOnLoad]
//public class AnchorFitRectTool : EditorWindow
//{
//    static AnchorFitRectTool()
//    {
//        SceneView.onSceneGUIDelegate += OnScene;
//    }

//    private static void OnScene(SceneView sceneview)
//    {
//        if (Event.current.type == EventType.MouseUp && Event.current.button == 0 && Event.current.control)
//        {
//            UpdateAnchors();
//        }
//    }

//    public void OnDestroy()
//    {
//        SceneView.onSceneGUIDelegate -= OnScene;
//    }

//    public static Rect anchorRect;
//    public static Vector2 anchorVector;
//    private static Rect anchorRectOld;
//    private static Vector2 anchorVectorOld;
//    private static RectTransform currentRectTransform;
//    private static RectTransform parentRectTransform;
//    private static Vector2 pivotOld;
//    private static Vector2 offsetMinOld;
//    private static Vector2 offsetMaxOld;

//    public static void UpdateAnchors()
//    {
//        TryToGetRectTransform();
//        if (currentRectTransform != null && parentRectTransform != null && ShouldStick())
//        {
//            //Debug.Log("[Anchors Tools] Updating");
//            Stick();
//        }
//    }

//    private static bool ShouldStick()
//    {
//        return (
//            currentRectTransform.offsetMin != offsetMinOld ||
//            currentRectTransform.offsetMax != offsetMaxOld ||
//            currentRectTransform.pivot != pivotOld ||
//            anchorVector != anchorVectorOld ||
//            anchorRect != anchorRectOld
//            );
//    }

//    private static void Stick()
//    {
//        CalculateCurrentWH();
//        CalculateCurrentXY();

//        CalculateCurrentXY();
//        pivotOld = currentRectTransform.pivot;
//        anchorVectorOld = anchorVector;

//        AnchorsToCorners();
//        anchorRectOld = anchorRect;

//        UnityEditor.EditorUtility.SetDirty(currentRectTransform.gameObject);
//    }

//    private static void TryToGetRectTransform()
//    {
//        currentRectTransform = UnityEditor.Selection.activeGameObject.GetComponent<RectTransform>();
//        parentRectTransform = currentRectTransform.parent.gameObject.GetComponent<RectTransform>();

//    }

//    private static void CalculateCurrentXY()
//    {
//        float pivotX = anchorRect.width * currentRectTransform.pivot.x;
//        float pivotY = anchorRect.height * (1 - currentRectTransform.pivot.y);
//        Vector2 newXY = new Vector2(currentRectTransform.anchorMin.x * parentRectTransform.rect.width + currentRectTransform.offsetMin.x + pivotX - parentRectTransform.rect.width * anchorVector.x,
//                                  -(1 - currentRectTransform.anchorMax.y) * parentRectTransform.rect.height + currentRectTransform.offsetMax.y - pivotY + parentRectTransform.rect.height * (1 - anchorVector.y));
//        anchorRect.x = newXY.x;
//        anchorRect.y = newXY.y;
//        anchorRectOld = anchorRect;
//    }

//    private static void CalculateCurrentWH()
//    {
//        anchorRect.width = currentRectTransform.rect.width;
//        anchorRect.height = currentRectTransform.rect.height;
//        anchorRectOld = anchorRect;
//    }

//    private static void AnchorsToCorners()
//    {
//        Undo.RecordObject(currentRectTransform, "AnchorsToCorners");
//        float pivotX = anchorRect.width * currentRectTransform.pivot.x;
//        float pivotY = anchorRect.height * (1 - currentRectTransform.pivot.y);
//        currentRectTransform.anchorMin = new Vector2(0f, 1f);
//        currentRectTransform.anchorMax = new Vector2(0f, 1f);
//        currentRectTransform.offsetMin = new Vector2(anchorRect.x / Mathf.Abs(currentRectTransform.localScale.x), anchorRect.y / Mathf.Abs(currentRectTransform.localScale.y) - anchorRect.height);
//        currentRectTransform.offsetMax = new Vector2(anchorRect.x / Mathf.Abs(currentRectTransform.localScale.x) + anchorRect.width, anchorRect.y / Mathf.Abs(currentRectTransform.localScale.y));

//        var min =
//            new Vector2(
//                currentRectTransform.anchorMin.x + anchorVector.x +
//                (currentRectTransform.offsetMin.x - pivotX) / parentRectTransform.rect.width *
//                Mathf.Abs(currentRectTransform.localScale.x),
//                currentRectTransform.anchorMin.y - (1 - anchorVector.y) +
//                (currentRectTransform.offsetMin.y + pivotY) / parentRectTransform.rect.height *
//                Mathf.Abs(currentRectTransform.localScale.y));
//        var max =
//            new Vector2(
//                currentRectTransform.anchorMax.x + anchorVector.x +
//                (currentRectTransform.offsetMax.x - pivotX) / parentRectTransform.rect.width *
//                Mathf.Abs(currentRectTransform.localScale.x),
//                currentRectTransform.anchorMax.y - (1 - anchorVector.y) +
//                (currentRectTransform.offsetMax.y + pivotY) / parentRectTransform.rect.height *
//                Mathf.Abs(currentRectTransform.localScale.y));

//        //min *= (Vector2.one - AnchorToolsWindow.OffsetMin); //+= (max - min) * AnchorToolsWindow.OffsetMin;
//        //max += (max - min) * AnchorToolsWindow.OffsetMax;

//        currentRectTransform.anchorMin = min;
//        currentRectTransform.anchorMax = max;
        
//        currentRectTransform.offsetMin = new Vector2(
//                (0 - currentRectTransform.pivot.x) * anchorRect.width * (1 - Mathf.Abs(currentRectTransform.localScale.x)),
//                (0 - currentRectTransform.pivot.y) * anchorRect.height * (1 - Mathf.Abs(currentRectTransform.localScale.y)));
//        //currentRectTransform.offsetMin *=//-= (currentRectTransform.offsetMax - currentRectTransform.offsetMin) *
//        //                                  (Vector2.one - AnchorToolsWindow.OffsetMin);
//        currentRectTransform.offsetMax = new Vector2(
//                (1 - currentRectTransform.pivot.x) * anchorRect.width * (1 - Mathf.Abs(currentRectTransform.localScale.x)),
//                (1 - currentRectTransform.pivot.y) * anchorRect.height * (1 - Mathf.Abs(currentRectTransform.localScale.y)));

//        offsetMinOld = currentRectTransform.offsetMin;
//        offsetMaxOld = currentRectTransform.offsetMax;
//    }
//}