Beispiel #1
0
        public UIScrollRect(float x, float y, float width, float height, Layout layout, Vector2 pivot, bool isNeedMask)
        {
#if UNITY_EDITOR
            Name = "ScrollRect";
#endif

            this.scrollrectWidth  = width;
            this.scrollrectHeight = height;
            this.isNeedMask       = isNeedMask;
            this.layout           = layout;

            Pivot = pivot;
            SetPosition(x, y);
            Size = new Vector2(width, height);

            // 1. ScrollRect 组件
            scrollRect            = gameObject.AddComponent <ScrollRect>();
            scrollRect.horizontal = layout == Layout.Horizontal || layout == Layout.Grid;
            scrollRect.vertical   = layout == Layout.Vertical || layout == Layout.Grid;

            // 2. 裁切区域
            if (isNeedMask)
            {
                viewport      = new UIMask(0, 0, 0, 0, false);
                viewport.Name = "Viewport";
                base.AddChild(viewport);
                viewport.AnchorMin = Vector2.zero;
                viewport.AnchorMax = Vector2.one;
            }

            // 3. 内容区
            content       = new UINode(0, 0);
            content.Name  = "Content";
            ContentWidth  = width;
            ContentHeight = height;

            // 4. 给ScrollRect赋值
            if (isNeedMask)
            {
                viewport.AddChild(content);
            }
            else
            {
                base.AddChild(content);
            }

            // 5. 修复Anchor
            //if (layout == Layout.Horizontal)
            //{
            //    content.AnchorMin = UIUtils.UpperLeft;
            //    content.AnchorMax = UIUtils.UpperRight;
            //}
            //else if (layout == Layout.Vertical)
            //{
            //    content.AnchorMin = UIUtils.LowerLeft;
            //    content.AnchorMax = UIUtils.UpperLeft;
            //}
            //else if (layout == Layout.Grid)
            //{
            //    content.AnchorMin = UIUtils.UpperLeft;
            //    content.AnchorMax = UIUtils.UpperRight;
            //}

            content.AnchorMin = Anchor.UpperLeft;
            content.AnchorMax = Anchor.UpperLeft;

            scrollRect.content = content.RectTransform;
            if (isNeedMask)
            {
                scrollRect.viewport = viewport.RectTransform;
            }
        }