Пример #1
0
        public override void ApplyAttributes(AttributeDictionary attributesToApply)
        {
            base.ApplyAttributes(attributesToApply);

            var dropdown              = primaryComponent as Dropdown;
            var templateComponent     = dropdown.template.GetComponent <Image>();
            var itemTemplateComponent = dropdown.template.GetComponentInChildren <Toggle>();
            var textComponent         = dropdown.captionText;

            var textTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text");

            textTagHandler.SetInstance(textComponent.rectTransform, this.currentXmlLayoutInstance);

            var textAttributes = new AttributeDictionary(
                attributesToApply.Where(a => TextTagHandler.TextAttributes.Contains(a.Key, StringComparer.OrdinalIgnoreCase))
                .ToDictionary(a => a.Key, b => b.Value));

            if (attributesToApply.ContainsKey("textshadow"))
            {
                textAttributes.Add("shadow", attributesToApply["textshadow"]);
            }
            if (attributesToApply.ContainsKey("textoutline"))
            {
                textAttributes.Add("outline", attributesToApply["textoutline"]);
            }
            if (attributesToApply.ContainsKey("textcolor"))
            {
                textAttributes.Add("color", attributesToApply["textcolor"]);
            }
            if (attributesToApply.ContainsKey("textalignment"))
            {
                textAttributes.Add("alignment", attributesToApply["textalignment"]);
            }

            textTagHandler.ApplyAttributes(textAttributes);
            // disable the XmlElement component, it can interfere with mouse clicks/etc.
            textComponent.GetComponent <XmlElement>().enabled = false;

            var xmlLayoutDropdown = dropdown.GetComponent <XmlLayoutDropdown>();
            var arrow             = xmlLayoutDropdown.Arrow;

            if (attributesToApply.ContainsKey("arrowimage"))
            {
                arrow.sprite = attributesToApply["arrowimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("arrowcolor"))
            {
                arrow.color = attributesToApply["arrowcolor"].ToColor();
            }

            // dropdownHeight property
            if (attributesToApply.ContainsKey("dropdownheight"))
            {
                dropdown.template.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, float.Parse(attributesToApply["dropdownheight"]));
            }

            // Apply text attributes to the item template
            var itemTemplate   = xmlLayoutDropdown.ItemTemplate;
            var itemTagHandler = XmlLayoutUtilities.GetXmlTagHandler("Toggle");
            var itemAttributes = attributesToApply.Clone();

            if (attributesToApply.ContainsKey("itemheight"))
            {
                var itemHeight = float.Parse(attributesToApply["itemheight"]);
                (itemTemplate.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemHeight);

                // it's also necessary to set the height of the content transform, otherwise we end up with weird issues
                var contentTransform = templateComponent.GetComponentInChildren <ScrollRect>().content;

                contentTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemHeight);
            }

            if (attributesToApply.ContainsKey("checkcolor"))
            {
                itemAttributes.Add("togglecheckmarkcolor", attributesToApply["checkcolor"]);
            }

            if (attributesToApply.ContainsKey("checksize"))
            {
                itemAttributes.Add("togglecheckmarksize", attributesToApply["checksize"]);
            }

            if (attributesToApply.ContainsKey("checkimage"))
            {
                itemAttributes.Add("togglecheckmarkimage", attributesToApply["checkimage"]);
            }

            if (attributesToApply.ContainsKey("checkimagepreserveaspect"))
            {
                itemAttributes.Add("togglecheckmarkimagepreserveaspect", attributesToApply["checkimagepreserveaspect"]);
            }

            // don't attempt to apply data source attributes to the item template
            itemAttributes.Remove("vm-dataSource");
            itemAttributes.Remove("vm-options");

            itemAttributes.Remove("color");
            itemAttributes.Remove("colors");

            // this attribute is checked by the Toggle to see if changes to the background should be permitted
            // (used by regular toggles to center the background if there is no text)
            itemAttributes.Add("dontModifyBackground", "");

            var itemXmlElement = itemTemplate.transform.GetComponent <XmlElement>();

            if (itemXmlElement == null)
            {
                itemTagHandler.SetInstance(itemTemplate.transform as RectTransform, this.currentXmlLayoutInstance);
                itemTagHandler.ApplyAttributes(itemAttributes);

                itemXmlElement = itemTemplate.transform.GetComponent <XmlElement>();
            }
            else
            {
                itemXmlElement.ApplyAttributes(itemAttributes);
            }

            if (itemXmlElement != null)
            {
                itemXmlElement.enabled = false;
            }

            if (attributesToApply.ContainsKey("itembackgroundcolors"))
            {
                itemTemplateComponent.colors = attributesToApply["itembackgroundcolors"].ToColorBlock();
            }

            if (attributesToApply.ContainsKey("dropdownbackgroundcolor"))
            {
                dropdown.template.GetComponent <Image>().color = attributesToApply["dropdownbackgroundcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("dropdownbackgroundimage"))
            {
                dropdown.template.GetComponent <Image>().sprite = attributesToApply["dropdownbackgroundimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("itemtextcolor"))
            {
                var itemTextComponent = dropdown.itemText;
                itemTextComponent.color = attributesToApply["itemtextcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("scrollbarcolors"))
            {
                xmlLayoutDropdown.DropdownScrollbar.colors = attributesToApply["scrollbarcolors"].ToColorBlock();
            }

            if (attributesToApply.ContainsKey("scrollbarimage"))
            {
                xmlLayoutDropdown.DropdownScrollbar.image.sprite = attributesToApply["scrollbarimage"].ToSprite();
            }

            if (attributesToApply.ContainsKey("scrollbarbackgroundcolor"))
            {
                xmlLayoutDropdown.DropdownScrollbar.GetComponent <Image>().color = attributesToApply["scrollbarbackgroundcolor"].ToColor();
            }

            if (attributesToApply.ContainsKey("scrollbarbackgroundimage"))
            {
                xmlLayoutDropdown.DropdownScrollbar.GetComponent <Image>().sprite = attributesToApply["scrollbarbackgroundimage"].ToSprite();
            }

            foreach (var attribute in attributesToApply)
            {
                SetPropertyValue(templateComponent, attribute.Key, attribute.Value);
            }

            // data source
#if !ENABLE_IL2CPP
            if (attributesToApply.ContainsKey("vm-options"))
            {
                xmlLayoutDropdown.optionsDataSource = attributesToApply["vm-options"];
            }

            if (attributesToApply.ContainsKey("vm-dataSource"))
            {
                HandleDataSourceAttribute(attributesToApply.GetValue("vm-dataSource"), attributesToApply.GetValue("vm-options"));
            }
#endif
        }