Example #1
0
    public void SetUIArea(RectTransform target, Rect area, Transform canvas)
    {
        Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(canvas, target);

        Vector2 delta = m_padding;

        if (bounds.center.x - bounds.extents.x < area.x)//target超出area的左边框
        {
            delta.x += Mathf.Abs(bounds.center.x - bounds.extents.x - area.x);
        }
        else if (bounds.center.x + bounds.extents.x > area.width / 2)//target超出area的右边框
        {
            delta.x -= Mathf.Abs(bounds.center.x + bounds.extents.x - area.width / 2);
        }

        if (bounds.center.y - bounds.extents.y < area.y)//target超出area上边框
        {
            delta.y += Mathf.Abs(bounds.center.y - bounds.extents.y - area.y);
        }
        else if (bounds.center.y + bounds.extents.y > area.height / 2)//target超出area的下边框
        {
            delta.y -= Mathf.Abs(bounds.center.y + bounds.extents.y - area.height / 2);
        }

        //加上偏移位置算出在屏幕内的坐标
        target.anchoredPosition += delta;
    }
Example #2
0
    /// <summary>
    /// 固定位置在指定画布区域内
    /// </summary>
    /// <param name="_source">源位置</param>
    /// <param name="_canvas">画布</param>
    /// <param name="_rect">限制区域(画布左下角为坐标原点(0,0))</param>
    public static void FixedPositionInCanvas(this RectTransform _source, RectTransform _canvas, Rect _rect)
    {
        Bounds  bounds     = RectTransformUtility.CalculateRelativeRectTransformBounds(_canvas, _source);
        Vector2 delta      = Vector2.zero;
        Vector3 tempCenter = bounds.center;

        tempCenter.x += _canvas.rect.width * 0.5f;
        tempCenter.y += _canvas.rect.height * 0.5f;
        bounds.center = tempCenter;
        if (bounds.center.x - bounds.extents.x < _rect.x)//target超出area的左边框
        {
            delta.x += Mathf.Abs(bounds.center.x - bounds.extents.x - _rect.x);
        }
        else if (bounds.center.x + bounds.extents.x > _rect.width)//target超出area的右边框
        {
            delta.x -= Mathf.Abs(bounds.center.x + bounds.extents.x - _rect.width);
        }

        if (bounds.center.y - bounds.extents.y < _rect.y)//target超出area上边框
        {
            delta.y += Mathf.Abs(bounds.center.y - bounds.extents.y - _rect.y);
        }
        else if (bounds.center.y + bounds.extents.y > _rect.height)//target超出area的下边框
        {
            delta.y -= Mathf.Abs(bounds.center.y + bounds.extents.y - _rect.height);
        }
        //加上偏移位置算出在屏幕内的坐标
        _source.anchoredPosition += delta;
    }
Example #3
0
    private void GenerateStartingPoints()
    {
        Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(m_RectTransform);

        // Coordinates are relativ to the middle center point of the parent rect transform

        float xMin = -bounds.extents.x + (m_ControlPointSize.x / 2f);
        float xMax = bounds.extents.x + (m_ControlPointSize.x / 2f);
        float yMin = -bounds.extents.y + (m_ControlPointSize.y / 2f);
        float yMax = bounds.extents.y + (m_ControlPointSize.y / 2f);

        float x, y;

        // @ToDo: Generate Colormaps

        // Blue
        x = Mathf.Lerp(xMin, xMax, 0.125f);
        y = Mathf.Lerp(yMin, yMax, 0.25f);
        this.CreatePoint(new Vector2(x, y), Color.blue, false);

        // Green
        x = Mathf.Lerp(xMin, xMax, 0.375f);
        y = Mathf.Lerp(yMin, yMax, 0.5f);
        this.CreatePoint(new Vector2(x, y), Color.green, false);

        // Yellow
        x = Mathf.Lerp(xMin, xMax, 0.625f);
        y = Mathf.Lerp(yMin, yMax, 0.625f);
        this.CreatePoint(new Vector2(x, y), Color.yellow, false);

        // Red
        x = Mathf.Lerp(xMin, xMax, 0.875f);
        y = Mathf.Lerp(yMin, yMax, 0.75f);
        this.CreatePoint(new Vector2(x, y), Color.red, false);
    }
    static int CalculateRelativeRectTransformBounds(IntPtr L)
    {
        int count = LuaDLL.lua_gettop(L);

        if (count == 1)
        {
            Transform arg0 = (Transform)LuaScriptMgr.GetUnityObject(L, 1, typeof(Transform));
            Bounds    o    = RectTransformUtility.CalculateRelativeRectTransformBounds(arg0);
            LuaScriptMgr.Push(L, o);
            return(1);
        }
        else if (count == 2)
        {
            Transform arg0 = (Transform)LuaScriptMgr.GetUnityObject(L, 1, typeof(Transform));
            Transform arg1 = (Transform)LuaScriptMgr.GetUnityObject(L, 2, typeof(Transform));
            Bounds    o    = RectTransformUtility.CalculateRelativeRectTransformBounds(arg0, arg1);
            LuaScriptMgr.Push(L, o);
            return(1);
        }
        else
        {
            LuaDLL.luaL_error(L, "invalid arguments to method: RectTransformUtility.CalculateRelativeRectTransformBounds");
        }

        return(0);
    }
Example #5
0
    public static void FixedPositionInCanvas(this RectTransform _source, RectTransform _canvas, Rect _rect)
    {
        Bounds  bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_canvas, _source);
        Vector2 zero   = Vector2.zero;
        Vector3 center = bounds.center;

        center.x     += _canvas.sizeDelta.x * 0.5f;
        center.y     += _canvas.sizeDelta.y * 0.5f;
        bounds.center = center;
        if ((bounds.center.x - bounds.extents.x) < _rect.x)
        {
            zero.x += Mathf.Abs((float)((bounds.center.x - bounds.extents.x) - _rect.x));
        }
        else if ((bounds.center.x + bounds.extents.x) > _rect.width)
        {
            zero.x -= Mathf.Abs((float)((bounds.center.x + bounds.extents.x) - _rect.width));
        }
        if ((bounds.center.y - bounds.extents.y) < _rect.y)
        {
            zero.y += Mathf.Abs((float)((bounds.center.y - bounds.extents.y) - _rect.y));
        }
        else if ((bounds.center.y + bounds.extents.y) > _rect.height)
        {
            zero.y -= Mathf.Abs((float)((bounds.center.y + bounds.extents.y) - _rect.height));
        }
        _source.anchoredPosition += zero;
    }
Example #6
0
        public void RecalculateBounds(Vector2 margin)
        {
            var rootTransform = gameObject.GetComponent <RectTransform>();
            var bounds        = RectTransformUtility.CalculateRelativeRectTransformBounds(rootTransform);

            rootTransform.sizeDelta = new Vector2(rootTransform.sizeDelta.x + margin.x, bounds.size.y + margin.y);
        }
Example #7
0
    private void UpdateUVRect()
    {
        if (realTime)
        {
            return;
        }

        var tsz  = new Vector2(m_camera.targetTexture.width, m_camera.targetTexture.height);
        var isz  = tsz;
        var rect = transform as RectTransform;

        if (rect)
        {
            var ss = RectTransformUtility.CalculateRelativeRectTransformBounds(rect).size;
            isz.Set(ss.x, ss.y);
            tsz *= ss.y / tsz.y;
        }

        var relative = new Vector2((m_offset.x + isz.x * m_pivot.x) / tsz.x, (m_offset.y + isz.y * m_pivot.y) / tsz.y);

        tsz *= scale;

        var off = new Vector2(tsz.x * relative.x - isz.x * m_pivot.x, tsz.y * relative.y - isz.y * m_pivot.y);

        m_image.uvRect = new Rect(off.x / tsz.x, off.y / tsz.y, isz.x / tsz.x, isz.y / tsz.y);
    }
Example #8
0
        public static float VerticalScrollDeltaToCoverElement(ScrollRect scrollRect, RectTransform element)
        {
            if (scrollRect.content != element.parent)
            {
                throw new ArgumentException("Element must be a direct child of the scroll rect content pane.");
            }

            RectTransform scrollRtf           = scrollRect.transform as RectTransform;
            float         scrollWindow        = scrollRect.content.rect.height - scrollRtf.rect.height;
            Bounds        relativeChildBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(scrollRtf, element);

            if (scrollWindow <= 0.0f)
            {
                return(0.0f);
            }

            float aboveDelta = relativeChildBounds.center.y + relativeChildBounds.extents.y
                               - scrollRtf.rect.height / 2.0f;
            float belowDelta = relativeChildBounds.center.y - relativeChildBounds.extents.y
                               + scrollRtf.rect.height / 2.0f;

            if (aboveDelta > 0.0f)
            {
                return(aboveDelta / scrollWindow);
            }
            else if (belowDelta < 0.0f)
            {
                return(belowDelta / scrollWindow);
            }

            return(0.0f);
        }
Example #9
0
        /// <summary>
        /// Deactivate on Awake.
        /// </summary>
        void Awake()
        {
            Instance = this;

            m_StartingScale = transform.localScale;
            Bounds canvasBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform);

            RectTransform rect = GetComponent <RectTransform>();

            m_ObjectBounds = new Vector3(canvasBounds.size.x * rect.localScale.x, canvasBounds.size.y * rect.localScale.y, canvasBounds.size.z * rect.localScale.z);

            // Actually find microphone key in the keyboard
            var dictationButton = TransformExtensions.GetChildRecursive(gameObject.transform, "Dictation");

            if (dictationButton != null)
            {
                var dictationIcon = dictationButton.Find("keyboard_closeIcon");
                if (dictationIcon != null)
                {
                    _recordImage = dictationIcon.GetComponentInChildren <Image>();
                    var material = new Material(_recordImage.material);
                    _defaultColor         = material.color;
                    _recordImage.material = material;
                }
            }

            // Setting the keyboardType to an undefined TouchScreenKeyboardType,
            // which prevents the MRTK keyboard from triggering the system keyboard itself.
            InputField.keyboardType = (TouchScreenKeyboardType)(int.MaxValue);

            // Keep keyboard deactivated until needed
            gameObject.SetActive(false);
        }
Example #10
0
    /// <summary>
    /// 注意rect中心点在中间
    /// </summary>
    /// <param name="target"></param>
    /// <param name="area"></param>
    /// <returns></returns>
    public static bool SetUIArea(RectTransform target, Rect area, Transform canvas)
    {
        Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(canvas, target);

        if (null == area)
        {
            return(false);
        }

        Vector2 delta = default(Vector2);

        if (bounds.center.x - bounds.extents.x < area.x) //target超出area的左边框
        {
            delta.x += Mathf.Abs(bounds.center.x - bounds.extents.x - area.x);
        }
        else if (bounds.center.x + bounds.extents.x > area.width / 2) //target超出area的右边框
        {
            delta.x -= Mathf.Abs(bounds.center.x + bounds.extents.x - area.width / 2);
        }

        if (bounds.center.y - bounds.extents.y < area.y) //target超出area上边框
        {
            delta.y += Mathf.Abs(bounds.center.y - bounds.extents.y - area.y);
        }
        else if (bounds.center.y + bounds.extents.y > area.height / 2) //target超出area的下边框
        {
            delta.y -= Mathf.Abs(bounds.center.y + bounds.extents.y - area.height / 2);
        }

        //加上偏移位置算出在屏幕内的坐标
        target.anchoredPosition += delta;

        return(delta != default(Vector2));
    }
Example #11
0
    public void SetAnchors()
    {
        this.rectTransform = gameObject.GetComponent <RectTransform>();

        this.parentBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform.parent);
        this.parentSize   = new Vector2(this.parentBounds.size.x, this.parentBounds.size.y);

        //anchor ratio => pixel position
        this.positionMin = new Vector2(this.parentSize.x * this.rectTransform.anchorMin.x, this.parentSize.y * this.rectTransform.anchorMin.y);
        this.positionMax = new Vector2(this.parentSize.x * this.rectTransform.anchorMax.x, this.parentSize.y * this.rectTransform.anchorMax.y);

        //offsetMin - lower left corner
        //offsetMax - upper right corner
        this.positionMin = this.positionMin + this.rectTransform.offsetMin;
        this.positionMax = this.positionMax + this.rectTransform.offsetMax;

        //pixel position => anchor ratio
        this.positionMin = new Vector2(this.positionMin.x / this.parentBounds.size.x, this.positionMin.y / this.parentBounds.size.y);
        this.positionMax = new Vector2(this.positionMax.x / this.parentBounds.size.x, this.positionMax.y / this.parentBounds.size.y);

        this.rectTransform.anchorMin = this.positionMin;
        this.rectTransform.anchorMax = this.positionMax;

        this.rectTransform.offsetMin = Vector2.zero;
        this.rectTransform.offsetMax = Vector2.zero;
    }
        /// <summary>
        /// Deactivate on Awake.
        /// </summary>
        protected override void Awake()
        {
            base.Awake();

            m_StartingScale = transform.localScale;
            Bounds canvasBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform);

            RectTransform rect = GetComponent <RectTransform>();

            m_ObjectBounds = new Vector3(canvasBounds.size.x * rect.localScale.x, canvasBounds.size.y * rect.localScale.y, canvasBounds.size.z * rect.localScale.z);

            // Actually find microphone key in the keyboard
            var dictationButton = Utils.GetChildRecursive(gameObject.transform, "Dictation");

            if (dictationButton != null)
            {
                var dictationIcon = dictationButton.Find("keyboard_closeIcon");
                if (dictationIcon != null)
                {
                    _recordImage = dictationIcon.GetComponentInChildren <Image>();
                    var material = new Material(_recordImage.material);
                    _defaultColor         = material.color;
                    _recordImage.material = material;
                }
            }

            // Keep keyboard deactivated until needed
            gameObject.SetActive(false);
        }
        void OnWindowResize()
        {
            //Collapse regions based on axis
            for (int i = 0; i < Regions.Count; i++)
            {
                Vector2 resize = Regions[i].OriginalSize;
                if (Regions[i].ThresholdX)
                {
                    Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(Regions[i].UITransform, Regions[i].ThresholdX);
                    float  size   = Regions[i].ReverseX ? -bounds.min.x : bounds.max.x;
                    if (size < Regions[i].OriginalSize.x)
                    {
                        resize.x = size;
                    }
                }

                if (Regions[i].ThresholdY)
                {
                    Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(Regions[i].UITransform, Regions[i].ThresholdY);
                    float  size   = Regions[i].ReverseY ? -bounds.min.y : bounds.max.y;
                    if (size < Regions[i].OriginalSize.y)
                    {
                        resize.y = size;
                    }
                }
                Regions[i].UITransform.sizeDelta = resize;
            }
        }
        private void Update()
        {
            RectTransform rectTransform = transform as RectTransform;
            Vector2       size          = RectTransformUtility.CalculateRelativeRectTransformBounds(rectTransform).size;
            Vector3       scale         = transform.lossyScale;
            int           width         = (int)(size.x * scale.x);
            int           height        = (int)(size.y * scale.y);

            if (width <= 0 || height <= 0)
            {
                return;
            }

            Texture oldTexture = rawImage.texture;

            if (oldTexture && (oldTexture.width != width || oldTexture.height != height))
            {
                Destroy(oldTexture);
                RenderTexture renderTexture = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
                rawImage.texture = renderTexture;
                UpdateRenderCamera();
            }
            else if (!oldTexture)
            {
                RenderTexture renderTexture = new RenderTexture(width, height, 16, RenderTextureFormat.ARGB32);
                rawImage.texture = renderTexture;
                UpdateRenderCamera();
            }
        }
Example #15
0
        private static void MakeGroup(object userdata)
        {
            List <RectTransform> targets = (List <RectTransform>)userdata;
            GameObject           group   = new GameObject("Container", typeof(RectTransform));

            Undo.IncrementCurrentGroup();
            int group_index = Undo.GetCurrentGroup();

            Undo.SetCurrentGroupName("Make Group");
            Undo.RegisterCreatedObjectUndo(group, "create group object");
            RectTransform rectTrans = group.GetComponent <RectTransform>();

            if (rectTrans != null)
            {
                Transform parent = targets[0].parent;
                Bounds    bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(parent, targets[0]);
                for (int i = 1; i < targets.Count; i++)
                {
                    Bounds a = RectTransformUtility.CalculateRelativeRectTransformBounds(parent, targets[i]);
                    bounds.Encapsulate(a);
                }

                rectTrans.SetParent(targets[0].parent);
                rectTrans.localScale    = Vector3.one;
                rectTrans.sizeDelta     = bounds.size;
                rectTrans.localPosition = bounds.center;

                foreach (var target in targets)
                {
                    Undo.SetTransformParent(target, rectTrans, "move item to group");
                }
            }
            Selection.activeGameObject = group;
            Undo.CollapseUndoOperations(group_index);
        }
Example #16
0
        private Bounds CalcBounds(RectTransform self, RectTransform tar, bool recursive)
        {
            Bounds bounds = default;

            if (recursive)
            {
                var trans = tar.GetComponentsInChildren <RectTransform>();
                for (int i = 0; i < trans.Length; i++)
                {
                    var t = trans[i];
                    var b = RectTransformUtility.CalculateRelativeRectTransformBounds(self, t);
                    if (i == 0)
                    {
                        bounds = b;
                    }
                    else
                    {
                        bounds.Encapsulate(b);
                    }
                }
            }
            else
            {
                bounds = CetRectTransformSelfBounds(self, tar);
            }

            return(bounds);
        }
Example #17
0
        /// <summary>
        /// 子階層を含むレイアウトのRectを取得.
        /// </summary>
        /// <param name="trans"></param>
        /// <returns></returns>
        public static Bounds CalculateRelativeWorldRect(this RectTransform trans)
        {
            var gameObjects = UnityUtility.GetChildrenAndSelf(trans.gameObject);

            // ※ 複数のコンポーネントを検証出来る様にforeachで実行.

            foreach (var gameObject in gameObjects)
            {
                var components = gameObject.GetComponents <Component>();

                foreach (var component in components)
                {
                    var preferredSizeCopy = component as PreferredSizeCopy;

                    if (preferredSizeCopy != null)
                    {
                        preferredSizeCopy.UpdateLayoutImmediate();
                    }
                }
            }

            var bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(trans);

            var center = trans.TransformPoint(bounds.center);
            var size   = trans.TransformVector(bounds.size);

            return(new Bounds(center, size));
        }
Example #18
0
    public static void adjustRectTransform(this RectTransform rectT)
    {
        if (rectT == null || rectT.parent == null)
        {
            return;
        }

        Bounds parentBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(rectT.parent);

        Vector2 parentSize = new Vector2(parentBounds.size.x, parentBounds.size.y);
        // convert anchor ration in to pixel position
        Vector2 posMin = new Vector2(parentSize.x * rectT.anchorMin.x, parentSize.y * rectT.anchorMin.y);
        Vector2 posMax = new Vector2(parentSize.x * rectT.anchorMax.x, parentSize.y * rectT.anchorMax.y);

        // add offset
        posMin = posMin + rectT.offsetMin;
        posMax = posMax + rectT.offsetMax;

        // convert from pixel position to anchor ratio again
        posMin = new Vector2(posMin.x / parentBounds.size.x, posMin.y / parentBounds.size.y);
        posMax = new Vector2(posMax.x / parentBounds.size.x, posMax.y / parentBounds.size.y);

        rectT.anchorMin = posMin;
        rectT.anchorMax = posMax;

        rectT.offsetMin = Vector2.zero;
        rectT.offsetMax = Vector2.zero;
    }
Example #19
0
    void AdjustRectTransform()
    {
        GameObject    gameObject = textBoxes[actual];
        RectTransform transform  = gameObject.GetComponent <RectTransform>();

        if (transform == null || transform.parent == null)
        {
            return;
        }

        Bounds parentBounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform.parent);

        Vector2 parentSize = new Vector2(parentBounds.size.x, parentBounds.size.y);
        // convert anchor ration in to pixel position
        Vector2 posMin = new Vector2(parentSize.x * transform.anchorMin.x, parentSize.y * transform.anchorMin.y);
        Vector2 posMax = new Vector2(parentSize.x * transform.anchorMax.x, parentSize.y * transform.anchorMax.y);

        // add offset
        posMin = posMin + transform.offsetMin;
        posMax = posMax + transform.offsetMax;

        // convert from pixel position to anchor ratio again
        posMin = new Vector2(posMin.x / parentBounds.size.x, posMin.y / parentBounds.size.y);
        posMax = new Vector2(posMax.x / parentBounds.size.x, posMax.y / parentBounds.size.y);

        transform.anchorMin = posMin;
        transform.anchorMax = posMax;

        transform.offsetMin = Vector2.zero;
        transform.offsetMax = Vector2.zero;
        print(textBoxes[actual].GetComponent <RectTransform>().rect.size.y + " ");
    }
Example #20
0
        private static Component CreateGridLayout(GameObject gameObject, IAssetLoader assetLoader, GridLayoutComponent gridLayoutComponent)
        {
            var gridLayoutGroup = gameObject.AddComponent <GridLayoutGroup>();

            var spacing = Vector2.zero;

            if (gridLayoutComponent.SpacingX != null)
            {
                spacing.x = gridLayoutComponent.SpacingX.Value;
            }
            if (gridLayoutComponent.SpacingY != null)
            {
                spacing.y = gridLayoutComponent.SpacingY.Value;
            }
            gridLayoutGroup.spacing = spacing;

            var children = gameObject.GetDirectChildren();

            if (children.Length == 1)
            {
                var childRect = RectTransformUtility.CalculateRelativeRectTransformBounds(children[0].GetComponent <RectTransform>());

                var cellSize = Vector2.zero;
                cellSize.x = childRect.size.x;
                cellSize.y = childRect.size.y;
                gridLayoutGroup.cellSize = cellSize;
            }
            else
            {
                Debug.LogWarning($"need children.Length({children.Length}) == 1");
            }

            return(gridLayoutGroup);
        }
Example #21
0
        public void Show()
        {
            if (_chapterInfo == null)
            {
                return;
            }

            string  chapterBGTexturePath = ResPath.GetChapterBGPath(_chapterInfo.chapterData.chapterBG);
            Texture chapterBGTexture     = ResMgr.instance.Load <Texture>(chapterBGTexturePath);

            chapterBGRawImage.texture = chapterBGTexture;
            chapterBGRawImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, CHAPTER_BG_WIDTH);
            chapterBGRawImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, CHAPTER_BG_HEIGHT);

            Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform);

            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, bounds.size.x);
            rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, bounds.size.y);

            string  chapterBGLineTexturePath = ResPath.GetChapterBGPath(_chapterInfo.chapterData.chapterLineBG);
            Texture chapterBGLineTexture     = ResMgr.instance.Load <Texture>(chapterBGLineTexturePath);

            chapterBGLineRawImage.texture = chapterBGLineTexture;
            chapterBGRawImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, CHAPTER_BG_WIDTH);
            chapterBGRawImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, CHAPTER_BG_HEIGHT);
        }
Example #22
0
    private float GetRectTransformRelativeBoundsHeight(RectTransform r)
    {
        var   b = RectTransformUtility.CalculateRelativeRectTransformBounds(r);
        float h = Mathf.Abs(b.extents.y * 2);

        return(h);
    }
Example #23
0
    public static Bounds GetRect(RectTransform rectTransform)
    {
        Bounds rect = RectTransformUtility.CalculateRelativeRectTransformBounds(rectTransform.parent, rectTransform);

        rect.center = rectTransform.anchoredPosition;
        rect.size   = rect.size * 1F;
        return(rect);
    }
Example #24
0
    private bool RectOverlaps(RectTransform rectTrans1, RectTransform rectTrans2)
    {
        var b1 = RectTransformUtility.CalculateRelativeRectTransformBounds(_canvas.transform, rectTrans1);
        var b2 = RectTransformUtility.CalculateRelativeRectTransformBounds(_canvas.transform, rectTrans2);

        Debug.Log($"bound1: {b1} / bound2: {b2}");
        return(b1.Intersects(b2));
    }
Example #25
0
    /// <summary>
    /// 获取RectTransform的真实Bounds
    /// </summary>
    /// <param name="rectTrans"></param>
    /// <returns></returns>
    public static Bounds GetBounds(this RectTransform rectTrans)
    {
        var bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(rectTrans);

        bounds.center += rectTrans.position;
        bounds.size    = new Vector3(bounds.size.x * rectTrans.lossyScale.x, bounds.size.y * rectTrans.lossyScale.y, bounds.size.z * rectTrans.lossyScale.z);
        return(bounds);
    }
Example #26
0
        protected override void OnPopulateMesh(VertexHelper vbo)
        {
            vbo.Clear();

            // 填充顶点
            UIVertex vert = UIVertex.simpleVert;

            vert.color = color;

            Vector2 selfPiovt = rectTransform.pivot;
            Rect    selfRect  = rectTransform.rect;
            float   outerLx   = -selfPiovt.x * selfRect.width;
            float   outerBy   = -selfPiovt.y * selfRect.height;
            float   outerRx   = (1 - selfPiovt.x) * selfRect.width;
            float   outerTy   = (1 - selfPiovt.y) * selfRect.height;

            // 0 - Outer:LT
            vert.position = new Vector3(outerLx, outerTy);
            vbo.AddVert(vert);
            // 1 - Outer:RT
            vert.position = new Vector3(outerRx, outerTy);
            vbo.AddVert(vert);
            // 2 - Outer:RB
            vert.position = new Vector3(outerRx, outerBy);
            vbo.AddVert(vert);
            // 3 - Outer:LB
            vert.position = new Vector3(outerLx, outerBy);
            vbo.AddVert(vert);

            Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform, Box.transform);

            var _targetMin = bounds.min;
            var _targetMax = bounds.max;

            // 4 - Inner:LT
            vert.position = new Vector3(_targetMin.x, _targetMax.y);
            vbo.AddVert(vert);
            // 5 - Inner:RT
            vert.position = new Vector3(_targetMax.x, _targetMax.y);
            vbo.AddVert(vert);
            // 6 - Inner:RB
            vert.position = new Vector3(_targetMax.x, _targetMin.y);
            vbo.AddVert(vert);
            // 7 - Inner:LB
            vert.position = new Vector3(_targetMin.x, _targetMin.y);
            vbo.AddVert(vert);



            vbo.AddTriangle(4, 0, 1);
            vbo.AddTriangle(4, 1, 5);
            vbo.AddTriangle(5, 1, 2);
            vbo.AddTriangle(5, 2, 6);
            vbo.AddTriangle(6, 2, 3);
            vbo.AddTriangle(6, 3, 7);
            vbo.AddTriangle(7, 3, 0);
            vbo.AddTriangle(7, 0, 4);
        }
        private void NavigationScrollRect_INTERNAL(WindowComponentNavigation source, NavigationSide side)
        {
            var scrollRect = this.NavigationScrollRect();

            if (scrollRect == null)
            {
                return;
            }

            var next = source.GetNavigation(side);

            if (next == null)
            {
                if (side == NavigationSide.Up)
                {
                    scrollRect.verticalNormalizedPosition = 1f;
                }
                else if (side == NavigationSide.Down)
                {
                    scrollRect.verticalNormalizedPosition = 0f;
                }
                else if (side == NavigationSide.Left)
                {
                    scrollRect.horizontalNormalizedPosition = 0f;
                }
                else if (side == NavigationSide.Right)
                {
                    scrollRect.horizontalNormalizedPosition = 1f;
                }
                return;
            }

            var containerRect = (scrollRect.transform as RectTransform).rect;
            var from          = RectTransformUtility.CalculateRelativeRectTransformBounds(this.GetRectTransform(), source.GetRectTransform());
            var to            = RectTransformUtility.CalculateRelativeRectTransformBounds(this.GetRectTransform(), next.GetRectTransform());
            var nextRect      = new Rect(to.center.XY(), to.size.XY());

            if (containerRect.Overlaps(nextRect) == true)
            {
                return;
            }

            var delta = (to.center - from.center).XY();

            if (side == NavigationSide.Left || side == NavigationSide.Right)
            {
                delta.y = 0f;
            }
            else
            {
                delta.x = 0f;
            }

            scrollRect.content.anchoredPosition    -= delta;
            scrollRect.horizontalNormalizedPosition = Mathf.Clamp01(scrollRect.horizontalNormalizedPosition);
            scrollRect.verticalNormalizedPosition   = Mathf.Clamp01(scrollRect.verticalNormalizedPosition);
        }
Example #28
0
    private void GetTargetMinMax(out Vector2 min, out Vector2 max, RectTransform targetTF)
    {
        //min = targetTF.offsetMin;
        //max = targetTF.offsetMax;
        Bounds bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(transform, targetTF);

        min = bounds.min;
        max = bounds.max;
    }
Example #29
0
    /// <summary>
    /// Graphic A中心点是否在Graphic B的矩形中
    /// </summary>
    /// <param name="_a">Graphic A</param>
    /// <param name="_b">Graphic B</param>
    /// <param name="_point">A中心点在B中的坐标</param>
    /// <returns>true:在,false:不在</returns>
    public static bool IsCenterPointInRectanle(this Graphic _a, Graphic _b, out Vector2 _point)
    {
        Bounds  bounds = RectTransformUtility.CalculateRelativeRectTransformBounds(_a.rectTransform);
        Vector3 offset = bounds.center * _a.canvas.transform.localScale.x;
        Vector2 point  = RectTransformUtility.WorldToScreenPoint(_a.canvas.worldCamera, _a.rectTransform.position + offset);

        RectTransformUtility.ScreenPointToLocalPointInRectangle(_b.rectTransform, point, _b.canvas.worldCamera, out _point);
        return(_b.GetPixelAdjustedRect().Contains(_point));
    }
        public static void AdjustInRectangle(this RectTransform src, RectTransform dst)
        {
            Rect    dstRect = dst.rect;
            Bounds  bounds  = RectTransformUtility.CalculateRelativeRectTransformBounds(dst, src);
            Rect    srcRect = new Rect(bounds.min, bounds.size);
            Vector2 offset  = srcRect.AdjustInRect(dstRect).min - (Vector2)bounds.min;

            src.localPosition += (Vector3)offset;
        }