コード例 #1
0
        public static GameObject CreateLoopVerticalScrollRect(DefaultControls.Resources resources)
        {
            GameObject root = CreateUIElementRoot("LoopVerticalScrollRect", new Vector2(200, 200));

            root.GetAddComponent <Image>().color = Color.gray;
            GameObject viewPort = CreateViewPort(root.transform);
            GameObject content  = CreateUIObject("Content", viewPort);

            RectTransform contentRT = content.GetComponent <RectTransform>();

            contentRT.anchorMin = new Vector2(0, 1f);
            contentRT.anchorMax = new Vector2(1, 1f);
            contentRT.sizeDelta = new Vector2(0, 200);
            contentRT.pivot     = new Vector2(0, 1f);

            // Setup UI components.

            LoopVerticalScrollRect scrollRect = root.AddComponent <LoopVerticalScrollRect>();

            scrollRect.content                       = contentRT;
            scrollRect.viewport                      = null;
            scrollRect.horizontalScrollbar           = null;
            scrollRect.verticalScrollbar             = null;
            scrollRect.horizontal                    = false;
            scrollRect.vertical                      = true;
            scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
            scrollRect.verticalScrollbarVisibility   = LoopScrollRect.ScrollbarVisibility.Permanent;
            scrollRect.horizontalScrollbarSpacing    = 0;
            scrollRect.verticalScrollbarSpacing      = 0;

            CreateHorizontalDefaultScrollBar(root.transform, scrollRect);
            CreateVerticalDefaultScrollBar(root.transform, scrollRect);

            Image img = viewPort.AddComponent <Image>();

            img.raycastTarget = true;
            Mask mask = viewPort.AddComponent <Mask>();

            mask.showMaskGraphic = false;

            VerticalLayoutGroup layoutGroup = content.AddComponent <VerticalLayoutGroup>();

            layoutGroup.childAlignment         = TextAnchor.UpperCenter;
            layoutGroup.childForceExpandWidth  = true;
            layoutGroup.childForceExpandHeight = false;

            ContentSizeFitter sizeFitter = content.AddComponent <ContentSizeFitter>();

            sizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            sizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

            return(root);
        }
コード例 #2
0
        public static GameObject CreateLoopVerticalScrollRect(DefaultControls.Resources resources)
        {
            GameObject root = CreateUIElementRoot("Loop Vertical Scroll Rect", new Vector2(200, 200));

            GameObject content = CreateUIObject("Content", root);

            RectTransform contentRT = content.GetComponent <RectTransform>();

            contentRT.anchorMin = new Vector2(0.5f, 1);
            contentRT.anchorMax = new Vector2(0.5f, 1);
            contentRT.sizeDelta = new Vector2(200, 0);
            contentRT.pivot     = new Vector2(0.5f, 1);

            // Setup UI components.

            LoopVerticalScrollRect scrollRect = root.AddComponent <LoopVerticalScrollRect>();

            scrollRect.content                       = contentRT;
            scrollRect.viewport                      = null;
            scrollRect.horizontalScrollbar           = null;
            scrollRect.verticalScrollbar             = null;
            scrollRect.horizontal                    = false;
            scrollRect.vertical                      = true;
            scrollRect.horizontalScrollbarVisibility = LoopScrollRect.ScrollbarVisibility.Permanent;
            scrollRect.verticalScrollbarVisibility   = LoopScrollRect.ScrollbarVisibility.Permanent;
            scrollRect.horizontalScrollbarSpacing    = 0;
            scrollRect.verticalScrollbarSpacing      = 0;

            root.AddComponent <RectMask2D>();
            root.AddComponent <YYEmpty4RaycastBtn>(); //为了在任何view的位置都可以拖动

            VerticalLayoutGroup layoutGroup = content.AddComponent <VerticalLayoutGroup>();

            layoutGroup.childAlignment         = TextAnchor.UpperCenter;
            layoutGroup.childControlHeight     = true;
            layoutGroup.childControlWidth      = true;
            layoutGroup.childForceExpandWidth  = false;
            layoutGroup.childForceExpandHeight = false;

            ContentSizeFitter sizeFitter = content.AddComponent <ContentSizeFitter>();

            sizeFitter.horizontalFit = ContentSizeFitter.FitMode.Unconstrained;
            sizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

            return(root);
        }