Esempio n. 1
0
        public static GameObject CreateButton(Resources resources)
        {
            GameObject buttonRoot = CreateUIElementRoot("IMC Button", s_ThickElementSize);

            GameObject childText = new GameObject("Text");

            SetParentAndAlign(childText, buttonRoot);

            IMCImage image = buttonRoot.AddComponent <IMCImage>();

            image.sprite = resources.standard;
            image.type   = IMCImage.Type.Sliced;
            image.color  = s_DefaultSelectableColor;

            IMCButton bt = buttonRoot.AddComponent <IMCButton>();

            SetDefaultColorTransitionValues(bt);

            IMCText text = childText.AddComponent <IMCText>();

            text.raycastTarget = false;

            text.text      = "IMC Button";
            text.alignment = TextAnchor.MiddleCenter;
            SetDefaultTextValues(text);

            RectTransform textRectTransform = childText.GetComponent <RectTransform>();

            textRectTransform.anchorMin = Vector2.zero;
            textRectTransform.anchorMax = Vector2.one;
            textRectTransform.sizeDelta = Vector2.zero;

            return(buttonRoot);
        }
Esempio n. 2
0
 private static void SetDefaultTextValues(IMCText lbl)
 {
     // Set text values we want across UI elements in default controls.
     // Don't set values which are the same as the default values for the Text component,
     // since there's no point in that, and it's good to keep them as consistent as possible.
     lbl.color = s_TextColor;
 }
Esempio n. 3
0
        public static void UntrackText(IMCText t)
        {
            if (t.font == null)
            {
                return;
            }

            List <IMCText> texts;

            m_Tracked.TryGetValue(t.font, out texts);

            if (texts == null)
            {
                return;
            }

            texts.Remove(t);

            if (texts.Count == 0)
            {
                m_Tracked.Remove(t.font);

                // There is a global textureRebuilt event for all fonts, so once the last Text reference goes away, remove our delegate
                if (m_Tracked.Count == 0)
                {
                    Font.textureRebuilt -= RebuildForFont;
                }
            }
        }
Esempio n. 4
0
        public static void TrackText(IMCText t)
        {
            if (t.font == null)
            {
                return;
            }

            List <IMCText> exists;

            m_Tracked.TryGetValue(t.font, out exists);
            if (exists == null)
            {
                // The textureRebuilt event is global for all fonts, so we add our delegate the first time we register *any* Text
                if (m_Tracked.Count == 0)
                {
                    Font.textureRebuilt += RebuildForFont;
                }

                exists = new List <IMCText>();
                m_Tracked.Add(t.font, exists);
            }

            if (!exists.Contains(t))
            {
                exists.Add(t);
            }
        }
Esempio n. 5
0
        public static GameObject CreateToggle(Resources resources)
        {
            // Set up hierarchy
            GameObject toggleRoot = CreateUIElementRoot("IMC Toggle", s_ThinElementSize);

            GameObject background = CreateUIObject("Background", toggleRoot);
            GameObject checkmark  = CreateUIObject("Checkmark", background);
            GameObject childLabel = CreateUIObject("Label", toggleRoot);

            // Set up components
            IMCToggle toggle = toggleRoot.AddComponent <IMCToggle>();

            toggle.isOn = true;

            IMCImage bgImage = background.AddComponent <IMCImage>();

            bgImage.sprite = resources.standard;
            bgImage.type   = IMCImage.Type.Sliced;
            bgImage.color  = s_DefaultSelectableColor;

            IMCImage checkmarkImage = checkmark.AddComponent <IMCImage>();

            checkmarkImage.sprite = resources.checkmark;

            IMCText label = childLabel.AddComponent <IMCText>();

            label.text    = "Toggle";
            label.raycast = false;
            SetDefaultTextValues(label);

            //toggle.backGround = bgImage;
            toggle.graphic       = checkmarkImage;
            toggle.targetGraphic = bgImage;
            SetDefaultColorTransitionValues(toggle);

            RectTransform bgRect = background.GetComponent <RectTransform>();

            bgRect.anchorMin        = new Vector2(0f, 1f);
            bgRect.anchorMax        = new Vector2(0f, 1f);
            bgRect.anchoredPosition = new Vector2(10f, -10f);
            bgRect.sizeDelta        = new Vector2(kThinHeight, kThinHeight);

            RectTransform checkmarkRect = checkmark.GetComponent <RectTransform>();

            checkmarkRect.anchorMin        = new Vector2(0.5f, 0.5f);
            checkmarkRect.anchorMax        = new Vector2(0.5f, 0.5f);
            checkmarkRect.anchoredPosition = Vector2.zero;
            checkmarkRect.sizeDelta        = new Vector2(20f, 20f);

            RectTransform labelRect = childLabel.GetComponent <RectTransform>();

            labelRect.anchorMin = new Vector2(0f, 0f);
            labelRect.anchorMax = new Vector2(1f, 1f);
            labelRect.offsetMin = new Vector2(23f, 1f);
            labelRect.offsetMax = new Vector2(-5f, -2f);

            return(toggleRoot);
        }
Esempio n. 6
0
        public static GameObject CreateInputField(Resources resources)
        {
            GameObject root = CreateUIElementRoot("IMC InputField", s_ThickElementSize);

            GameObject childPlaceholder = CreateUIObject("Placeholder", root);
            GameObject childText        = CreateUIObject("Text", root);

            IMCImage image = root.AddComponent <IMCImage>();

            image.sprite = resources.inputField;
            image.type   = IMCImage.Type.Sliced;
            image.color  = s_DefaultSelectableColor;

            IMCInputField inputField = root.AddComponent <IMCInputField>();

            SetDefaultColorTransitionValues(inputField);

            IMCText text = childText.AddComponent <IMCText>();

            text.text            = "";
            text.supportRichText = false;
            SetDefaultTextValues(text);

            IMCText placeholder = childPlaceholder.AddComponent <IMCText>();

            placeholder.text      = "Enter text...";
            placeholder.fontStyle = FontStyle.Italic;
            // Make placeholder color half as opaque as normal text color.
            Color placeholderColor = text.color;

            placeholderColor.a *= 0.5f;
            placeholder.color   = placeholderColor;

            RectTransform textRectTransform = childText.GetComponent <RectTransform>();

            textRectTransform.anchorMin = Vector2.zero;
            textRectTransform.anchorMax = Vector2.one;
            textRectTransform.sizeDelta = Vector2.zero;
            textRectTransform.offsetMin = new Vector2(10, 6);
            textRectTransform.offsetMax = new Vector2(-10, -7);

            RectTransform placeholderRectTransform = childPlaceholder.GetComponent <RectTransform>();

            placeholderRectTransform.anchorMin = Vector2.zero;
            placeholderRectTransform.anchorMax = Vector2.one;
            placeholderRectTransform.sizeDelta = Vector2.zero;
            placeholderRectTransform.offsetMin = new Vector2(10, 6);
            placeholderRectTransform.offsetMax = new Vector2(-10, -7);

            inputField.textComponent = text;
            inputField.placeholder   = placeholder;

            return(root);
        }
Esempio n. 7
0
        public static GameObject CreateText(Resources resources)
        {
            GameObject go = CreateUIElementRoot("IMC Text", s_ThickElementSize);

            IMCText lbl = go.AddComponent <IMCText>();

            lbl.text            = "IMCUI Text";
            lbl.supportRichText = false;
            lbl.raycastTarget   = false;
            SetDefaultTextValues(lbl);

            return(go);
        }
Esempio n. 8
0
        private IMCText CreateText(string name)
        {
            GameObject tempGo = new GameObject(name);

            tempGo.gameObject.AddComponent <RectTransform>();
            IMCText text = tempGo.AddComponent <IMCText>();

            text.alignment            = TextAnchor.MiddleCenter;
            text.font                 = Resources.GetBuiltinResource <Font>("Arial.ttf");
            text.color                = Color.black;
            text.text                 = "0%";
            text.resizeTextForBestFit = true;
            return(text);
        }
Esempio n. 9
0
        public static GameObject CreateSwitchButton(Resources resources)
        {
            GameObject root = CreateUIElementRoot("IMC Switch Button", s_ThinElementSize);

            GameObject background = CreateUIObject("Background", root);
            GameObject fillArea   = CreateUIObject("Fill Area", root);
            GameObject fill       = CreateUIObject("Fill", fillArea);
            GameObject handleArea = CreateUIObject("Handle Slide Area", root);
            GameObject handle     = CreateUIObject("Handle", handleArea);

            // Background
            IMCImage backgroundImage = background.AddComponent <IMCImage>();

            backgroundImage.sprite = resources.background;
            backgroundImage.type   = IMCImage.Type.Sliced;
            backgroundImage.color  = s_DefaultSelectableColor;
            RectTransform backgroundRect = background.GetComponent <RectTransform>();

            backgroundRect.anchorMin          = Vector2.zero;
            backgroundRect.anchorMax          = Vector2.one;
            backgroundRect.anchoredPosition3D = Vector3.zero;
            backgroundRect.sizeDelta          = new Vector2(0, 0);

            // Fill Area
            RectTransform fillAreaRect = fillArea.GetComponent <RectTransform>();

            fillAreaRect.anchorMin        = new Vector2(0, 0.25f);
            fillAreaRect.anchorMax        = new Vector2(1, 0.75f);
            fillAreaRect.anchoredPosition = new Vector2(-5, 0);
            fillAreaRect.sizeDelta        = new Vector2(-20, 0);

            // Fill
            IMCImage fillImage = fill.AddComponent <IMCImage>();

            fillImage.sprite = resources.standard;
            fillImage.type   = IMCImage.Type.Sliced;
            fillImage.color  = new Color(0, 0, 0, 0);

            RectTransform fillRect = fill.GetComponent <RectTransform>();

            fillRect.sizeDelta = new Vector2(10, 0);

            // Handle Area
            RectTransform handleAreaRect = handleArea.GetComponent <RectTransform>();

            handleAreaRect.sizeDelta = new Vector2(-20, 0);
            handleAreaRect.anchorMin = new Vector2(0, 0);
            handleAreaRect.anchorMax = new Vector2(1, 1);

            // Handle
            IMCImage handleImage = handle.AddComponent <IMCImage>();

            handleImage.sprite = resources.knob;
            handleImage.color  = s_DefaultSelectableColor;

            RectTransform handleRect = handle.GetComponent <RectTransform>();

            handleRect.anchorMin = Vector2.zero;
            handleRect.anchorMax = new Vector2(0, 1);
            handleRect.sizeDelta = new Vector2(20, 0);

            GameObject content = new GameObject("content");

            content.transform.SetParent(handleRect);
            IMCText text = content.AddComponent <IMCText>();

            text.anchorMin          = Vector2.zero;
            text.anchorMax          = Vector2.one;
            text.anchoredPosition3D = Vector3.zero;
            text.sizeDelta          = new Vector2(0, 0);
            text.fontSize           = 10;
            text.raycastTarget      = false;
            text.supportRichText    = false;
            text.color     = Color.black;
            text.alignment = TextAnchor.MiddleCenter;

            // Setup slider component
            IMCSwitchButton switchBtn = root.AddComponent <IMCSwitchButton>();

            root.AddComponent <CanvasGroup>();
            switchBtn.content    = text.GetComponent <IMCText>();
            switchBtn.fillRect   = fill.GetComponent <RectTransform>();
            switchBtn.handleRect = handle.GetComponent <RectTransform>();

            switchBtn.targetGraphic = handleImage;
            switchBtn.direction     = IMCSlider.Direction.LeftToRight;
            SetDefaultColorTransitionValues(switchBtn);

            return(root);
        }
Esempio n. 10
0
        public static GameObject CreateMessageBox(Resources resources)
        {
            GameObject go = CreateImage(resources);

            go.name = "IMCMessageBox";
            go.AddComponent <CanvasGroup>();
            IMCMessageBox mesBox = go.AddComponent <IMCMessageBox>();

            mesBox.background = go.GetComponent <IMCImage>();

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

            goRT.anchorMin = new Vector2(0.5f, 0.5f);
            goRT.anchorMax = new Vector2(0.5f, 0.5f);
            goRT.pivot     = new Vector2(0.5f, 0.5f);
            goRT.sizeDelta = new Vector2(400, 250);

            GameObject title = CreateText(resources);

            title.name = "Title";
            IMCText titleIMC = title.GetComponent <IMCText>();

            titleIMC.transform.SetParent(goRT);
            titleIMC.anchorMin          = new Vector2(0.5f, 1);
            titleIMC.anchorMax          = new Vector2(0.5f, 1);
            titleIMC.pivot              = new Vector2(0.5f, 1);
            titleIMC.sizeDelta          = new Vector2(400, 30);
            titleIMC.anchoredPosition3D = Vector3.zero;
            titleIMC.text      = "Title";
            titleIMC.fontSize  = 14;
            titleIMC.alignment = TextAnchor.MiddleCenter;
            mesBox.title       = titleIMC;

            GameObject content = CreateText(resources);

            content.name = "Content";
            IMCText contentIMC = content.GetComponent <IMCText>();

            contentIMC.transform.SetParent(goRT);
            contentIMC.anchorMin          = new Vector2(0.5f, 0.5f);
            contentIMC.anchorMax          = new Vector2(0.5f, 0.5f);
            contentIMC.pivot              = new Vector2(0.5f, 0.5f);
            contentIMC.sizeDelta          = new Vector2(400, 120);
            contentIMC.anchoredPosition3D = Vector3.zero;
            contentIMC.text      = "Content";
            contentIMC.fontSize  = 14;
            contentIMC.alignment = TextAnchor.MiddleCenter;
            mesBox.content       = contentIMC;

            GameObject    btns   = new GameObject("ButtonList");
            RectTransform btnsRT = btns.AddComponent <RectTransform>();

            btnsRT.SetParent(goRT);
            btnsRT.anchorMin          = new Vector2(0.5f, 0);
            btnsRT.anchorMax          = new Vector2(0.5f, 0);
            btnsRT.pivot              = new Vector2(0.5f, 0);
            btnsRT.sizeDelta          = new Vector2(380, 40);
            btnsRT.anchoredPosition3D = new Vector3(0, 10, 0);
            ContentSizeFitter btnsCSF = btns.AddComponent <ContentSizeFitter>();

            btnsCSF.horizontalFit = ContentSizeFitter.FitMode.PreferredSize;
            btnsCSF.verticalFit   = ContentSizeFitter.FitMode.Unconstrained;
            IMCHorizontalLayoutGroup btnsHLG = btns.AddComponent <IMCHorizontalLayoutGroup>();

            btnsHLG.spacing     = 5;
            mesBox.buttonParent = btns;
            for (int i = 0; i < 3; i++)
            {
                GameObject    tempBtn   = CreateButton(resources);
                LayoutElement tempBtnLE = tempBtn.AddComponent <LayoutElement>();
                tempBtn.transform.SetParent(btnsRT);
                tempBtnLE.preferredHeight = 35.9f;
                tempBtnLE.preferredWidth  = 124.4f;
                mesBox.btns.Add(tempBtn.GetComponent <IMCButton>());
            }

            GameObject closeBtn = CreateButton(resources);

            closeBtn.name = "closeBtn";
            RectTransform closeBtnRT = closeBtn.GetComponent <RectTransform>();

            closeBtnRT.SetParent(goRT);
            closeBtnRT.anchorMin          = Vector2.one;
            closeBtnRT.anchorMax          = Vector2.one;
            closeBtnRT.pivot              = Vector2.one;
            closeBtnRT.sizeDelta          = new Vector2(35, 30);
            closeBtnRT.anchoredPosition3D = Vector3.zero;
            IMCText closeText = closeBtn.GetComponent <IMCButton>().Text;

            closeText.text      = "X";
            closeText.fontSize  = 14;
            closeText.alignment = TextAnchor.MiddleCenter;
            mesBox.closeButton  = closeBtn.GetComponent <IMCButton>();

            return(go);
        }
Esempio n. 11
0
        public static GameObject CreateDropdown(Resources resources)
        {
            GameObject root = CreateUIElementRoot("IMC Dropdown", s_ThickElementSize);

            GameObject label          = CreateUIObject("Label", root);
            GameObject arrow          = CreateUIObject("Arrow", root);
            GameObject template       = CreateUIObject("Template", root);
            GameObject viewport       = CreateUIObject("Viewport", template);
            GameObject content        = CreateUIObject("Content", viewport);
            GameObject item           = CreateUIObject("Item", content);
            GameObject itemBackground = CreateUIObject("Item Background", item);
            GameObject itemCheckmark  = CreateUIObject("Item Checkmark", item);
            GameObject itemLabel      = CreateUIObject("Item Label", item);

            // Sub controls.

            GameObject scrollbar = CreateScrollbar(resources);

            scrollbar.name = "Scrollbar";
            SetParentAndAlign(scrollbar, template);

            IMCScrollbar scrollbarScrollbar = scrollbar.GetComponent <IMCScrollbar>();

            scrollbarScrollbar.SetDirection(IMCScrollbar.Direction.BottomToTop, true);

            RectTransform vScrollbarRT = scrollbar.GetComponent <RectTransform>();

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

            // Setup item UI components.

            IMCText itemLabelText = itemLabel.AddComponent <IMCText>();

            itemLabelText.raycast = false;
            SetDefaultTextValues(itemLabelText);
            itemLabelText.alignment = TextAnchor.MiddleLeft;

            IMCImage itemBackgroundImage = itemBackground.AddComponent <IMCImage>();

            itemBackgroundImage.color = new Color32(245, 245, 245, 255);

            IMCImage itemCheckmarkImage = itemCheckmark.AddComponent <IMCImage>();

            itemCheckmarkImage.sprite = resources.checkmark;

            IMCToggle itemToggle = item.AddComponent <IMCToggle>();

            itemToggle.targetGraphic = itemBackgroundImage;
            itemToggle.graphic       = itemCheckmarkImage;
            itemToggle.isOn          = true;

            // Setup template UI components.

            IMCImage templateImage = template.AddComponent <IMCImage>();

            templateImage.sprite = resources.standard;
            templateImage.type   = IMCImage.Type.Sliced;

            IMCScrollRect templateScrollRect = template.AddComponent <IMCScrollRect>();

            templateScrollRect.content                     = (RectTransform)content.transform;
            templateScrollRect.viewport                    = (RectTransform)viewport.transform;
            templateScrollRect.horizontal                  = false;
            templateScrollRect.movementType                = IMCScrollRect.MovementType.Clamped;
            templateScrollRect.verticalScrollbar           = scrollbarScrollbar;
            templateScrollRect.verticalScrollbarVisibility = IMCScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport;
            templateScrollRect.verticalScrollbarSpacing    = -3;

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

            scrollRectMask.showMaskGraphic = false;

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

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

            // Setup dropdown UI components.

            IMCText labelText = label.AddComponent <IMCText>();

            SetDefaultTextValues(labelText);
            labelText.text      = "Option A";
            labelText.raycast   = false;
            labelText.alignment = TextAnchor.MiddleLeft;

            IMCImage arrowImage = arrow.AddComponent <IMCImage>();

            arrowImage.sprite = resources.dropdown;

            IMCImage backgroundImage = root.AddComponent <IMCImage>();

            backgroundImage.sprite = resources.standard;
            backgroundImage.color  = s_DefaultSelectableColor;
            backgroundImage.type   = IMCImage.Type.Sliced;

            IMCDropdown dropdown = root.AddComponent <IMCDropdown>();

            dropdown.targetGraphic = backgroundImage;
            SetDefaultColorTransitionValues(dropdown);
            dropdown.template    = template.GetComponent <RectTransform>();
            dropdown.captionText = labelText;
            dropdown.itemText    = itemLabelText;

            // Setting default Item list.
            itemLabelText.text = "Option A";
            dropdown.options.Add(new IMCDropdown.OptionData {
                text = "Option A"
            });
            dropdown.options.Add(new IMCDropdown.OptionData {
                text = "Option B"
            });
            dropdown.options.Add(new IMCDropdown.OptionData {
                text = "Option C"
            });

            // Set up RectTransforms.

            RectTransform labelRT = label.GetComponent <RectTransform>();

            labelRT.anchorMin = Vector2.zero;
            labelRT.anchorMax = Vector2.one;
            labelRT.offsetMin = new Vector2(10, 6);
            labelRT.offsetMax = new Vector2(-25, -7);

            RectTransform arrowRT = arrow.GetComponent <RectTransform>();

            arrowRT.anchorMin        = new Vector2(1, 0.5f);
            arrowRT.anchorMax        = new Vector2(1, 0.5f);
            arrowRT.sizeDelta        = new Vector2(20, 20);
            arrowRT.anchoredPosition = new Vector2(-15, 0);

            RectTransform templateRT = template.GetComponent <RectTransform>();

            templateRT.anchorMin        = new Vector2(0, 0);
            templateRT.anchorMax        = new Vector2(1, 0);
            templateRT.pivot            = new Vector2(0.5f, 1);
            templateRT.anchoredPosition = new Vector2(0, 2);
            templateRT.sizeDelta        = new Vector2(0, 150);

            RectTransform viewportRT = viewport.GetComponent <RectTransform>();

            viewportRT.anchorMin = new Vector2(0, 0);
            viewportRT.anchorMax = new Vector2(1, 1);
            viewportRT.sizeDelta = new Vector2(-18, 0);
            viewportRT.pivot     = new Vector2(0, 1);

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

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

            RectTransform itemRT = item.GetComponent <RectTransform>();

            itemRT.anchorMin = new Vector2(0, 0.5f);
            itemRT.anchorMax = new Vector2(1, 0.5f);
            itemRT.sizeDelta = new Vector2(0, 20);

            RectTransform itemBackgroundRT = itemBackground.GetComponent <RectTransform>();

            itemBackgroundRT.anchorMin = Vector2.zero;
            itemBackgroundRT.anchorMax = Vector2.one;
            itemBackgroundRT.sizeDelta = Vector2.zero;

            RectTransform itemCheckmarkRT = itemCheckmark.GetComponent <RectTransform>();

            itemCheckmarkRT.anchorMin        = new Vector2(0, 0.5f);
            itemCheckmarkRT.anchorMax        = new Vector2(0, 0.5f);
            itemCheckmarkRT.sizeDelta        = new Vector2(20, 20);
            itemCheckmarkRT.anchoredPosition = new Vector2(10, 0);

            RectTransform itemLabelRT = itemLabel.GetComponent <RectTransform>();

            itemLabelRT.anchorMin = Vector2.zero;
            itemLabelRT.anchorMax = Vector2.one;
            itemLabelRT.offsetMin = new Vector2(20, 1);
            itemLabelRT.offsetMax = new Vector2(-10, -2);

            template.SetActive(false);

            return(root);
        }