private Image CreateBackgroundImage() { var image = currentInstanceTransform.gameObject.AddComponent <Image>(); image.color = Color.white; image.sprite = XmlLayoutUtilities.LoadResource <Sprite>("Sprites/Elements/UISprite_XmlLayout"); image.type = Image.Type.Sliced; return(image); }
private static void RegisterCustomTypeHandlers() { ConversionExtensions.RegisterCustomTypeConverter(typeof(TMP_FontAsset), (value, xmlLayout) => { var font = XmlLayoutUtilities.LoadResource <TMP_FontAsset>(value); if (font == null) { Debug.LogWarning("[XmlLayout][TextMesh Pro] Unable to load TMP Font Asset '" + value + "'."); } return(font); }); ConversionExtensions.RegisterCustomTypeConverter(typeof(TMPro.FontStyles), (value) => { var stylesEntries = value.Split('|'); FontStyles styles = FontStyles.Normal; foreach (var style in stylesEntries) { try { FontStyles s = (FontStyles)Enum.Parse(typeof(FontStyles), style); styles |= s; } catch { } } return(styles); }); ConversionExtensions.RegisterCustomTypeConverter(typeof(TMPro.VertexGradient), (value, xmlLayout) => { var colorBlock = value.ToColorBlock(xmlLayout); return(new TMPro.VertexGradient(colorBlock.normalColor, colorBlock.highlightedColor, colorBlock.pressedColor, colorBlock.disabledColor)); }); ConversionExtensions.RegisterCustomTypeConverter(typeof(TMPro.TMP_SpriteAsset), (value) => { var spriteAsset = XmlLayoutUtilities.LoadResource <TMP_SpriteAsset>(value); if (spriteAsset == null) { Debug.LogWarning("[XmlLayout][TextMesh Pro] Unable to load TMP Sprite Asset '" + value + "'."); } return(spriteAsset); }); }
void ChangeLanguage(string language) { this.selectedLanguage = language; if (language == "No Localization") { xmlLayout.SetLocalizationFile(null); return; } var languageFile = XmlLayoutUtilities.LoadResource <XmlLayoutLocalization>("Localization/" + language); if (languageFile == null) { Debug.LogWarningFormat("Warning: localization file for language '{0}' not found!", language); return; } xmlLayout.SetLocalizationFile(languageFile); }
public override void ApplyAttributes(AttributeDictionary attributesToApply) { var dropdownTransform = currentXmlElement.rectTransform.Find("Template") as RectTransform; var layoutElement = currentInstanceTransform.GetComponent <LayoutElement>(); if (layoutElement == null) { layoutElement = currentInstanceTransform.gameObject.AddComponent <LayoutElement>(); } // apply attributes as per usual base.ApplyAttributes(attributesToApply); if (ElementHasAttribute("dropdownheight", attributesToApply)) { dropdownTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, float.Parse(attributesToApply["dropdownheight"])); } var itemTemplate = CurrentDropdown.itemText.rectTransform.parent as RectTransform; if (ElementHasAttribute("itemHeight", attributesToApply)) { var itemHeight = float.Parse(currentXmlElement.GetAttribute("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 = currentXmlElement.rectTransform.Find("Template/Viewport/Content") as RectTransform; contentTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, itemHeight); } if (attributesToApply.ContainsKey("itemWidth")) { var itemWidth = attributesToApply["itemWidth"].ToFloat(); (itemTemplate.transform as RectTransform).SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, itemWidth); } var arrow = currentXmlElement.rectTransform.Find("Arrow").GetComponent <Image>(); if (attributesToApply.ContainsKey("arrowImage")) { arrow.sprite = attributesToApply["arrowImage"].ToSprite(); } if (attributesToApply.ContainsKey("arrowColor")) { arrow.color = attributesToApply["arrowColor"].ToColor(currentXmlLayoutInstance); } if (attributesToApply.ContainsKey("arrowOffset")) { arrow.rectTransform.anchoredPosition = attributesToApply["arrowOffset"].ToVector2(); } if (attributesToApply.ContainsKey("itemBackgroundColors")) { var toggle = itemTemplate.GetComponent <Toggle>(); toggle.colors = attributesToApply["itemBackgroundColors"].ToColorBlock(currentXmlLayoutInstance); } var dropdownBackground = dropdownTransform.GetComponent <Image>(); if (attributesToApply.ContainsKey("dropdownBackgroundColor")) { dropdownBackground.color = attributesToApply["dropdownBackgroundColor"].ToColor(currentXmlLayoutInstance); } if (attributesToApply.ContainsKey("dropdownBackgroundImage")) { dropdownBackground.sprite = attributesToApply["dropdownBackgroundImage"].ToSprite(); } var scrollbar = dropdownTransform.GetComponentInChildren <Scrollbar>(); var scrollbarImage = scrollbar.targetGraphic as Image; if (attributesToApply.ContainsKey("scrollbarColors")) { scrollbar.colors = attributesToApply["scrollbarColors"].ToColorBlock(currentXmlLayoutInstance); } if (attributesToApply.ContainsKey("scrollbarImage")) { scrollbarImage.sprite = attributesToApply["scrollbarImage"].ToSprite(); } var scrollbarBackground = scrollbar.GetComponent <Image>(); if (attributesToApply.ContainsKey("scrollbarBackgroundColor")) { scrollbarBackground.color = attributesToApply["scrollbarBackgroundColor"].ToColor(currentXmlLayoutInstance); } if (attributesToApply.ContainsKey("scrollbarBackgroundImage")) { scrollbarBackground.sprite = attributesToApply["scrollbarBackgroundImage"].ToSprite(); } if (attributesToApply.ContainsKey("scrollbarWidth")) { scrollbar.GetComponent <RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, attributesToApply["scrollbarWidth"].ToFloat()); } if (attributesToApply.ContainsKey("padding")) { var padding = attributesToApply["padding"].ToVector4(); var itemLabelTransform = currentInstanceTransform.Find("Label") as RectTransform; itemLabelTransform.offsetMin = new Vector2(padding.x, padding.w); itemLabelTransform.offsetMax = new Vector2(-padding.y, -padding.z); } var checkMark = itemTemplate.Find("Item Checkmark").GetComponent <Image>(); if (attributesToApply.ContainsKey("checkColor")) { checkMark.color = attributesToApply["checkColor"].ToColor(currentXmlLayoutInstance); } else { checkMark.color = new Color(0, 0, 0); } if (attributesToApply.ContainsKey("checkImage")) { checkMark.sprite = attributesToApply["checkImage"].ToSprite(); } else { checkMark.sprite = XmlLayoutUtilities.LoadResource <Sprite>("Sprites/Elements/Checkmark"); } if (attributesToApply.ContainsKey("checkSize")) { var size = attributesToApply["checkSize"].ToFloat(); checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, size); checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, size); } if (attributesToApply.ContainsKey("checkImagePreserveAspect")) { checkMark.preserveAspect = attributesToApply["checkMarkImagePreserveAspect"].ToBoolean(); } // data source #if !ENABLE_IL2CPP && MVVM_ENABLED if (attributesToApply.ContainsKey("vm-options")) { var xmlLayoutDropdown = currentXmlElement.GetComponent <XmlLayoutDropdown>(); xmlLayoutDropdown.optionsDataSource = attributesToApply["vm-options"]; } if (attributesToApply.ContainsKey("vm-dataSource")) { HandleDataSourceAttribute(attributesToApply.GetValue("vm-dataSource"), attributesToApply.GetValue("vm-options")); } #endif }