Пример #1
0
        public static List <GameObject> CreateHorGridScroll(this GameObject gameobject, string masksprite, out GameObject scrollObj, Vector2 viewSize, float delta, GameObject prefab, int cnt, UIAlign align = UIAlign.CENTER, int collimit = -1, int rowlimit = -1, UIPluginType uitype = defaultType)
        {
            if (uitype == UIPluginType.UGUI)
            {
                GameObject scrollgameobject = gameobject.TryGetGameObject("Scroll");
                scrollObj = scrollgameobject;
                float totalx = 0f;
                if (prefab != null)
                {
                    totalx = (prefab.GetComponent <RectTransform>().sizeDelta.x + delta) * cnt;
                }

                ScrollViewExpand rect = scrollgameobject.TryAddComponent <ScrollViewExpand>();
                rect.vertical     = false;
                rect.horizontal   = true;
                rect.movementType = ScrollRect.MovementType.Elastic;
                rect.inertia      = true;
                //rect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
                //rect.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
                rect.GetComponent <RectTransform>().sizeDelta = viewSize;

                GameObject viewPortgameobject = scrollgameobject.TryGetGameObject("Viewport");
                GameObject Gridgameobject     = viewPortgameobject.TryGetGameObject("Grid");

                Mask viewmask = viewPortgameobject.TryAddComponent <Mask>();
                viewmask.showMaskGraphic = false;

                ImageExpand viewimage = viewPortgameobject.TryAddComponent <ImageExpand>();
                viewimage.raycastTarget = true;
                viewimage.type          = Image.Type.Sliced;
                viewimage.fillCenter    = true;

                if (SpriteAtlasMgr.mIns != null)
                {
                    SpriteAtlasMgr.mIns.ChangeSprite(viewimage, masksprite);
                }

                GridLayoutGroup group = Gridgameobject.TryAddComponent <GridLayoutGroup>();
                group.spacing     = new Vector2(delta, 0);
                group.startCorner = GridLayoutGroup.Corner.UpperLeft;
                group.startAxis   = GridLayoutGroup.Axis.Horizontal;

                RectTransform grouprect = group.GetComponent <RectTransform>();
                grouprect.sizeDelta = new Vector2(totalx, 0);
                grouprect.anchorMin = new Vector2(0, 1);
                grouprect.anchorMax = new Vector2(1, 1);
                grouprect.pivot     = new Vector2(0, 1);

                if (collimit != -1 && collimit >= 0)
                {
                    group.constraint      = GridLayoutGroup.Constraint.FixedColumnCount;
                    group.constraintCount = collimit;
                }
                else if (rowlimit != -1 && rowlimit >= 0)
                {
                    group.constraint      = GridLayoutGroup.Constraint.FixedRowCount;
                    group.constraintCount = rowlimit;
                }

                if (align == UIAlign.CENTER)
                {
                    group.childAlignment = TextAnchor.MiddleCenter;
                }
                else if (align == UIAlign.CENTER_DOWN)
                {
                    group.childAlignment = TextAnchor.LowerCenter;
                }
                else if (align == UIAlign.CENTER_TOP)
                {
                    group.childAlignment = TextAnchor.UpperCenter;
                }
                else if (align == UIAlign.LEFT_DOWN)
                {
                    group.childAlignment = TextAnchor.LowerLeft;
                }
                else if (align == UIAlign.LEFT_TOP)
                {
                    group.childAlignment = TextAnchor.UpperLeft;
                }
                else if (align == UIAlign.RIGHT_DOWN)
                {
                    group.childAlignment = TextAnchor.LowerRight;
                }
                else if (align == UIAlign.RIGHT_TOP)
                {
                    group.childAlignment = TextAnchor.UpperRight;
                }

                List <GameObject> gameObjectList = null;
                if (cnt > 0)
                {
                    gameObjectList = ListPool.TrySpawn <List <GameObject> >();
                    for (int i = 0; i < cnt; ++i)
                    {
                        GameObject childgameobject = GameObject.Instantiate(prefab);
                        if (i == 0)
                        {
                            RectTransform childrect = childgameobject.GetComponent <RectTransform>();
                            if (childrect != null)
                            {
                                group.cellSize = childrect.sizeDelta;
                            }
                        }
                        Gridgameobject.AddInstance(childgameobject);
                        gameObjectList.Add(childgameobject);
                    }
                }

                RectTransform viewrecttransform = viewPortgameobject.GetComponent <RectTransform>();
                viewrecttransform.anchorMin = Vector2.zero;
                viewrecttransform.anchorMax = new Vector2(1, 1);
                viewrecttransform.pivot     = new Vector2(0, 1);
                viewrecttransform.sizeDelta = Vector2.zero;

                rect.content  = grouprect;
                rect.viewport = viewrecttransform;
                return(gameObjectList);
            }
            scrollObj = null;
            return(null);
        }
Пример #2
0
        public static void CreateScrollView(MenuCommand command)
        {
            GameObject go = CreateObj("Scroll View", Selection.activeObject);

            GameObject viewport = CreateObj("Viewport", go);
            GameObject content  = CreateObj("Content", viewport);

            GameObject Hor_Scrollbar = CreateObj("Hor_Scrollbar", go);
            GameObject HS_sliderArea = CreateObj("Sliding Area", Hor_Scrollbar);
            GameObject HS_handle     = CreateObj("Handle", HS_sliderArea);

            GameObject Ver_Scrollbar = CreateObj("Ver_Scrollbar", go);
            GameObject VS_sliderArea = CreateObj("Slideing Area", Ver_Scrollbar);
            GameObject VS_handle     = CreateObj("Handle", VS_sliderArea);

            RectTransform TotalRect = go.GetComponent <RectTransform>();

            TotalRect.sizeDelta = new Vector2(200, 200);

            RectTransform viewRect = SetRectTransForm(viewport, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0, 1));

            viewRect.sizeDelta = new Vector2(-17, -17);
            //content

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

            conRect.sizeDelta = new Vector2(0, 300);
            conRect.anchorMin = new Vector2(0, 1);
            conRect.anchorMax = new Vector2(1, 1);
            conRect.pivot     = new Vector2(0, 1);

            //Hor scorllbar
            ScrollbarExpand hbar = Hor_Scrollbar.AddComponent <ScrollbarExpand>();

            ClearRays(hbar);
            AddImage(Hor_Scrollbar, Color.white);

            ImageExpand hs_hanlder_image = AddImage(HS_handle, Color.white, kStandardSpritePath);

            ClearRays(hs_hanlder_image);
            hbar.targetGraphic = hs_hanlder_image;
            hbar.handleRect    = HS_handle.GetComponent <RectTransform>();

            var hbarRect = SetRectTransForm(Hor_Scrollbar, new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 0));
            var hbarArea = SetRectTransForm(HS_sliderArea, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f));
            var hsRect   = SetRectTransForm(HS_handle, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f));

            hbarRect.sizeDelta = new Vector2(-17, 20);
            hbarArea.sizeDelta = new Vector2(-20, -20);
            hsRect.sizeDelta   = new Vector2(20, 20);

            //Ver scorllbar
            ScrollbarExpand vbar = Ver_Scrollbar.AddComponent <ScrollbarExpand>();

            vbar.direction = Scrollbar.Direction.BottomToTop;
            ClearRays(vbar);

            AddImage(Ver_Scrollbar, Color.white);
            vbar.handleRect = VS_handle.GetComponent <RectTransform>();

            ImageExpand vs_hanlder_image = AddImage(VS_handle, Color.white, kStandardSpritePath);

            vbar.targetGraphic = vs_hanlder_image;
            ClearRays(vs_hanlder_image);

            var vsbarRect = SetRectTransForm(Ver_Scrollbar, new Vector2(1, 0), new Vector2(1, 1), new Vector2(1, 1));
            var vsbarArea = SetRectTransForm(VS_sliderArea, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f));
            var vsRect    = SetRectTransForm(VS_handle, new Vector2(0, 0), new Vector2(1, 1), new Vector2(0.5f, 0.5f));

            vsbarRect.sizeDelta = new Vector2(20, -17);
            vsbarArea.sizeDelta = new Vector2(-20, -20);
            vsRect.sizeDelta    = new Vector2(20, 20);
            //scroll
            ScrollViewExpand scroll = go.AddComponent <ScrollViewExpand>();

            scroll.horizontalScrollbar           = hbar;
            scroll.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scroll.horizontalScrollbarSpacing    = -3;

            scroll.verticalScrollbar           = vbar;
            scroll.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scroll.verticalScrollbarSpacing    = -3;

            scroll.content = content.GetComponent <RectTransform>();
            ClearRays(scroll);

            ImageExpand image = go.AddComponent <ImageExpand>();

            image.sprite     = AssetDatabase.GetBuiltinExtraResource <Sprite>(kBackgroundSpriteResourcePath);
            image.color      = whiteColor;
            image.type       = Image.Type.Sliced;
            image.fillCenter = true;
            ClearRays(image);

            viewport.AddComponent <Mask>().showMaskGraphic = false;
            ImageExpand vimage = viewport.AddComponent <ImageExpand>();

            vimage.sprite     = AssetDatabase.GetBuiltinExtraResource <Sprite>(kBackgroundSpriteResourcePath);
            vimage.color      = whiteColor;
            vimage.type       = Image.Type.Sliced;
            vimage.fillCenter = true;
            ClearRays(vimage);

            scroll.viewport = viewRect;
            PlaceUIElementRoot(go, command);
        }