private void Awake()
 {
     rectTransform   = GetComponent <RectTransform>();
     layoutGroup     = GetComponent <LayoutGroup>();
     layoutGroupType = layoutGroup.GetType() == typeof(VerticalLayoutGroup)
         ? LayoutGroupType.Vertical
         : LayoutGroupType.Horizontal;
 }
        private void Update()
        {
            if (rectTransform == null)
            {
                rectTransform = GetComponent <RectTransform>();
            }

            if (layoutGroup == null)
            {
                layoutGroup     = GetComponent <LayoutGroup>();
                layoutGroupType = layoutGroup.GetType() == typeof(VerticalLayoutGroup)
                    ? LayoutGroupType.Vertical
                    : LayoutGroupType.Horizontal;
            }

            Transform containerTransform;
            var       childCount = (containerTransform = transform).childCount;

            if (childCount == 0)
            {
                return;
            }

            childCount = 0;
            for (var i = 0; i < containerTransform.childCount; i++)
            {
                if (containerTransform.GetChild(i).gameObject.activeInHierarchy)
                {
                    childCount++;
                }
            }

            var firstChildRectTransform = containerTransform.GetChild(0).GetComponent <RectTransform>();
            var childSizeDelta          = firstChildRectTransform.sizeDelta;

            var padding   = layoutGroup.padding;
            var sizeDelta = rectTransform.sizeDelta;

            switch (layoutGroupType)
            {
            case LayoutGroupType.Vertical:
                sizeDelta.y  = childCount * (childSizeDelta.y + padding.top + padding.bottom + spacing);
                sizeDelta.y *= 0.5f;
                break;

            case LayoutGroupType.Horizontal:
                sizeDelta.x = childCount * (childSizeDelta.x + padding.left + padding.right);
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            rectTransform.sizeDelta = sizeDelta;
        }
Beispiel #3
0
 void Start()
 {
     layoutGroup   = GetComponent <LayoutGroup>();
     rectTransform = GetComponent <RectTransform>();
     // Save type of layout group so it does not have to be checked again later
     if (layoutGroup.GetType() == typeof(HorizontalLayoutGroup))
     {
         layoutGroupType       = LayoutGroupType.Horizontal;
         horizontalLayoutGroup = (HorizontalLayoutGroup)layoutGroup;
     }
     else if (layoutGroup.GetType() == typeof(VerticalLayoutGroup))
     {
         layoutGroupType     = LayoutGroupType.Vertical;
         verticalLayoutGroup = (VerticalLayoutGroup)layoutGroup;
     }
     else if (layoutGroup.GetType() == typeof(GridLayoutGroup))
     {
         layoutGroupType = LayoutGroupType.Grid;
         gridLayoutGroup = (GridLayoutGroup)layoutGroup;
     }
 }
        public static ScrollRect CreateScrollView(GameObject parent = null, string name = "Scroll View", LayoutGroupType layoutGroupType = LayoutGroupType.Vertical)
        {
            Resources resources = GetStandardResources();


            GameObject root = CreateUIElementRoot(name, new Vector2(200, 200));

            SetParentAndAlign(root, parent);
            root.layer = LayerMask.NameToLayer(kUILayerName);

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

            // Sub controls.

            GameObject hScrollbar = CreateScrollbar().gameObject;

            hScrollbar.name = "Scrollbar Horizontal";
            SetParentAndAlign(hScrollbar, root);
            RectTransform hScrollbarRT = hScrollbar.GetComponent <RectTransform>();

            hScrollbarRT.anchorMin = Vector2.zero;
            hScrollbarRT.anchorMax = Vector2.right;
            hScrollbarRT.pivot     = Vector2.zero;
            hScrollbarRT.sizeDelta = new Vector2(0, hScrollbarRT.sizeDelta.y);

            GameObject vScrollbar = CreateScrollbar().gameObject;

            vScrollbar.name = "Scrollbar Vertical";
            SetParentAndAlign(vScrollbar, root);
            vScrollbar.GetComponent <Scrollbar>().SetDirection(Scrollbar.Direction.BottomToTop, true);
            RectTransform vScrollbarRT = vScrollbar.GetComponent <RectTransform>();

            vScrollbarRT.anchorMin = Vector2.right;
            vScrollbarRT.anchorMax = Vector2.one;
            vScrollbarRT.pivot     = Vector2.one;
            vScrollbarRT.sizeDelta = new Vector2(vScrollbarRT.sizeDelta.x, 0);

            // Setup RectTransforms.

            // Make viewport fill entire scroll view.
            RectTransform viewportRT = viewport.GetComponent <RectTransform>();

            viewportRT.anchorMin = Vector2.zero;
            viewportRT.anchorMax = Vector2.one;
            viewportRT.sizeDelta = Vector2.zero;
            viewportRT.pivot     = Vector2.up;

            // Make context match viewpoprt width and be somewhat taller.
            // This will show the vertical scrollbar and not the horizontal one.
            RectTransform contentRT = content.GetComponent <RectTransform>();

            contentRT.anchorMin = Vector2.up;
            contentRT.anchorMax = Vector2.one;
            contentRT.sizeDelta = new Vector2(0, 300);
            contentRT.pivot     = Vector2.up;

            // Setup UI components.

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

            scrollRect.content                       = contentRT;
            scrollRect.viewport                      = viewportRT;
            scrollRect.horizontalScrollbar           = hScrollbar.GetComponent <Scrollbar>();
            scrollRect.verticalScrollbar             = vScrollbar.GetComponent <Scrollbar>();
            scrollRect.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scrollRect.verticalScrollbarVisibility   = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            scrollRect.horizontalScrollbarSpacing    = -3;
            scrollRect.verticalScrollbarSpacing      = -3;

            Image rootImage = root.AddComponent <Image>();

            rootImage.sprite = resources.background;
            rootImage.type   = Image.Type.Sliced;
            rootImage.color  = s_PanelColor;

            Mask viewportMask = viewport.AddComponent <Mask>();

            viewportMask.showMaskGraphic = false;

            Image viewportImage = viewport.AddComponent <Image>();

            viewportImage.sprite = resources.mask;
            viewportImage.type   = Image.Type.Sliced;

            switch (layoutGroupType)
            {
            case LayoutGroupType.Grid:
                content.AddComponent <GridLayoutGroup>();
                break;

            case LayoutGroupType.Horizontal:
                content.AddComponent <HorizontalLayoutGroup>();
                break;

            case LayoutGroupType.Vertical:
                content.AddComponent <VerticalLayoutGroup>();
                break;
            }

            return(scrollRect);
        }
Beispiel #5
0
        public KsmGuiWindow
        (
            LayoutGroupType topLayout,
            bool destroyOnClose       = true,
            float opacity             = 0.8f,
            bool isDraggable          = false, int dragOffset = 0,
            TextAnchor groupAlignment = TextAnchor.UpperLeft,
            float groupSpacing        = 0f,
            TextAnchor screenAnchor   = TextAnchor.MiddleCenter,
            TextAnchor windowPivot    = TextAnchor.MiddleCenter,
            int posX = 0, int posY = 0
        ) : base(null)
        {
            this.destroyOnClose = destroyOnClose;

            TopTransform.SetAnchorsAndPosition(screenAnchor, windowPivot, posX, posY);
            TopTransform.SetParentFixScale(KsmGuiMasterController.Instance.KsmGuiTransform);
            TopTransform.localScale = Vector3.one;

            // our custom lock manager
            InputLockManager = TopObject.AddComponent <KsmGuiInputLock>();
            InputLockManager.rectTransform = TopTransform;

            // if draggable, add the stock dragpanel component
            IsDraggable = isDraggable;
            if (IsDraggable)
            {
                DragPanel            = TopObject.AddComponent <DragPanel>();
                DragPanel.edgeOffset = dragOffset;
            }

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

            img.sprite = Textures.KsmGuiSpriteBackground;
            img.type   = Image.Type.Sliced;
            img.color  = new Color(1.0f, 1.0f, 1.0f, opacity);

            SizeFitter = TopObject.AddComponent <ContentSizeFitter>();
            SizeFitter.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            SizeFitter.verticalFit   = ContentSizeFitter.FitMode.PreferredSize;

            if (topLayout == LayoutGroupType.Vertical)
            {
                LayoutGroup = TopObject.AddComponent <VerticalLayoutGroup>();
            }
            else
            {
                LayoutGroup = TopObject.AddComponent <HorizontalLayoutGroup>();
            }

            LayoutGroup.spacing                = groupSpacing;
            LayoutGroup.padding                = new RectOffset(5, 5, 5, 5);
            LayoutGroup.childControlHeight     = true;
            LayoutGroup.childControlWidth      = true;
            LayoutGroup.childForceExpandHeight = false;
            LayoutGroup.childForceExpandWidth  = false;
            LayoutGroup.childAlignment         = groupAlignment;

            // close on scene changes
            GameEvents.onGameSceneLoadRequested.Add(OnSceneChange);
        }