コード例 #1
0
    /// <summary>
    /// 设置箭头显示位置
    /// </summary>
    /// <param name="_rectTransform">RectTransform</param>
    /// <param name="_anchor">锚点</param>
    void SetArrowEdge(RectTransform _rectTransform, TextAnchor _anchor)
    {
        _rectTransform.anchorMin = _rectTransform.anchorMax = _anchor.GetTextAnchorPivot();
        switch (_anchor)
        {
        case TextAnchor.MiddleLeft:
            _rectTransform.localEulerAngles = Vector3.zero;
            _rectTransform.pivot            = new Vector2(1f, 0.5f);
            _rectTransform.anchoredPosition = Vector2.zero;
            break;

        case TextAnchor.MiddleRight:
            _rectTransform.localEulerAngles = Vector3.forward * 180f;
            _rectTransform.pivot            = new Vector2(1f, 0.5f);
            _rectTransform.anchoredPosition = Vector2.zero;
            break;

        case TextAnchor.UpperCenter:
            _rectTransform.localEulerAngles = Vector3.forward * -90f;
            _rectTransform.pivot            = new Vector2(1f, 0.5f);
            _rectTransform.anchoredPosition = Vector2.zero;
            break;

        case TextAnchor.LowerCenter:
            _rectTransform.localEulerAngles = Vector3.forward * 90f;
            _rectTransform.pivot            = new Vector2(1f, 0.5f);
            _rectTransform.anchoredPosition = Vector2.zero;
            break;
        }
    }
コード例 #2
0
    /// <summary>
    /// 从指定的RectTransform对齐位置
    /// </summary>
    /// <param name="_self">要復制值的RectTransform</param>
    /// <param name="_selfAnchor">自身锚点</param>
    /// <param name="_selfOffsetPos">自身位置偏移</param>
    /// <param name="_from">復制源RectTransform</param>
    /// <param name="_fromAnchor">复制源锚点</param>
    /// <param name="_fromOffsetPos">复制源锚点偏移</param>
    public static void AlignRectTransformFrom(this Graphic _self, TextAnchor _selfAnchor, Vector2 _selfOffsetPos, Graphic _from, TextAnchor _fromAnchor, Vector2 _fromOffsetPos)
    {
        Vector2 pos   = _self.LocalPointToRefer(_from);
        Vector2 pivot = _selfAnchor.GetTextAnchorPivot();

        pivot          = _self.rectTransform.pivot - pivot;
        _selfOffsetPos = pivot * _self.rectTransform.sizeDelta + _selfOffsetPos;

        pivot          = _fromAnchor.GetTextAnchorPivot();
        pivot          = pivot - _from.rectTransform.pivot;
        _fromOffsetPos = pivot * _from.rectTransform.sizeDelta + _fromOffsetPos;

        _self.rectTransform.anchoredPosition = pos + _selfOffsetPos + _fromOffsetPos;
    }