Beispiel #1
0
        public static GameObject CreateCascadeDropDown(Resources resoures)
        {
            GameObject go = new GameObject("IMC Cascade Dropdown");

            go.AddComponent <IMCCascadeDropdown>();
            RectTransform goRT = go.AddComponent <RectTransform>();

            goRT.anchoredPosition3D = Vector3.zero;
            go.AddComponent <CanvasGroup>();
            IMCDropdown[] dropdowns = new IMCDropdown[3];
            for (int i = 0; i < dropdowns.Length; i++)
            {
                dropdowns[i] = CreateDropdown(resoures).GetComponent <IMCDropdown>();
                RectTransform tempRT = dropdowns[i].GetComponent <RectTransform>();

                tempRT.pivot     = new Vector2(0, 1);
                tempRT.anchorMin = new Vector2(0, 1);
                tempRT.anchorMax = new Vector2(0, 1);
            }
            goRT.sizeDelta = new Vector2(dropdowns[0].sizeDelta.x * 3, dropdowns[0].sizeDelta.y);
            for (int i = 0; i < dropdowns.Length; i++)
            {
                dropdowns[i].transform.SetParent(goRT);
                dropdowns[i].GetComponent <RectTransform>().anchoredPosition3D = new Vector3(goRT.anchoredPosition3D.x + i * dropdowns[i].GetComponent <RectTransform>().sizeDelta.x, goRT.anchoredPosition3D.y, goRT.anchoredPosition3D.z);
            }

            return(go);
        }
Beispiel #2
0
            public virtual void OnCancel(BaseEventData eventData)
            {
                IMCDropdown dropdown = GetComponentInParent <IMCDropdown>();

                if (dropdown)
                {
                    dropdown.Hide();
                }
            }
Beispiel #3
0
 /// <summary>
 /// 通过参数dropDown返回对应m_dropDowns数组中的索引(如果没有对应的索引,则返回-1)
 /// </summary>
 public int GetDropDownsIndex(IMCDropdown dropDown)
 {
     if (dropDowns.Contains(dropDown))
     {
         for (int i = 0; i < dropDowns.Count; i++)
         {
             if (dropDown == dropDowns[i])
             {
                 return(i);
             }
         }
     }
     return(-1);
 }
Beispiel #4
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);
        }