コード例 #1
0
    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;
    }
コード例 #2
0
    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        GetData();

        //Show if target parent is not null
        if (HasParent)
        {
            ShowAnchorSetter = EditorGUILayout.Foldout(ShowAnchorSetter, ShowAnchorSetter ? "Hide Custom Anchor Setter" : "Show Custom Anchor Setter");

            if (ShowAnchorSetter)
            {
                EditorGUILayout.Vector2Field("Absolute pos", RectTransformUtilities.GetAbsolutePosition(_target));
                EditorGUILayout.Vector2Field("Absolute size", RectTransformUtilities.GetAbsoluteSize(_target));

                int width = 150;

                GUI.backgroundColor = Color.clear;
                GUILayoutOption[] options = { GUILayout.MaxWidth(100.0f), GUILayout.MinWidth(10.0f) };

                GUILayout.BeginHorizontal(GUILayout.Width(width));
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Top_L_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.LeftTopCorner);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Top_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.Top);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Top_R_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.RightTopCorner);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Top_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchX_Top);
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal(GUILayout.Width(width));
                if (GUILayout.Button(buttonsIcons["Pivot_Button_R_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.Left);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.Center);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_L_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.Right);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Center_Horizontal_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchX);
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal(GUILayout.Width(width));
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Down_L_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.LeftBottomCorner);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Down_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.Bottom);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Down_R_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.RightBottomCorner);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Down_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchX_Bottom);
                }
                GUILayout.EndHorizontal();
                GUILayout.BeginHorizontal(GUILayout.Width(width));
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_L_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchY_Left);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Center_Vertical_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchY);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Center_R_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.StretchY_Right);
                }
                if (GUILayout.Button(buttonsIcons["Pivot_Button_Streach_Center_unactive"]))
                {
                    SetCutomAnchorsButton_OnClick(CustomAnchorTypes.OnCorners);
                }
                GUILayout.EndHorizontal();
            }
        }
        else    //Show when there is no parent
        {
            GUILayout.Label("No parent with RectTransform", EditorStyles.centeredGreyMiniLabel);
        }
    }