private static void HandleShaderKeywords(Material material, AttributeDictionary materialAttributes) { if (materialAttributes.Any(a => a.Key.Contains("bevel"))) { material.EnableKeyword("BEVEL_ON"); } if (materialAttributes.Any(a => a.Key.StartsWith("underlay"))) { var underlayType = materialAttributes.GetValue("underlayType"); if (underlayType == "Inner") { material.EnableKeyword("UNDERLAY_INNER"); } else if (underlayType != "None") { material.EnableKeyword("UNDERLAY_ON"); } } if (materialAttributes.Any(a => a.Key.StartsWith("glow"))) { material.EnableKeyword("GLOW_ON"); } if (materialAttributes.ContainsKey("bevelType")) { var type = Enum.Parse(typeof(BevelType), materialAttributes["bevelType"]); material.SetFloat("_ShaderFlags", (int)type); } }
public static void AddIfMissing(this AttributeDictionary attributes, string key, int value) { var any = attributes.Any(x => x.Key == key); if (!any) { attributes.Add(key, value); } }
public override void ApplyAttributes(AttributeDictionary attributes) { var pagedRectTagHandler = PagedRectTagHandler.CurrentPagedRectTagHandler; if (pagedRectTagHandler == null) { Debug.Log("[XmlLayout] Error: Pagination: Unable to locate PagedRect instance."); } else { var pagedRectInstance = pagedRectTagHandler.currentInstanceTransform.GetComponent <PagedRect>(); var sizeAttributes = new string[] { "width", "height" }; if (attributes.Any(a => sizeAttributes.Contains(a.Key))) { // try and preserve default positioning of the pagination container if width or height attributes are provided if (!attributes.ContainsKey("rectAlignment")) { if (pagedRectTagHandler.tagType.Contains("Vertical")) { attributes.Add("rectAlignment", "MiddleLeft"); } else { attributes.Add("rectAlignment", "LowerCenter"); } } var viewportRectTransform = (RectTransform)pagedRectInstance.GetComponentInChildren <Viewport>().transform; // try and resize the viewport if the pagination container size changes if (pagedRectTagHandler.tagType.Contains("Vertical")) { if (attributes.ContainsKey("width")) { var rectAlignment = attributes.GetValue("rectAlignment") ?? "MiddleLeft"; if (rectAlignment.Contains("Left")) { viewportRectTransform.offsetMin = new Vector2(attributes.GetValue <float>("width"), viewportRectTransform.offsetMin.y); viewportRectTransform.offsetMax = new Vector2(0, viewportRectTransform.offsetMax.y); } else if (rectAlignment.Contains("Right")) { viewportRectTransform.offsetMin = new Vector2(0, viewportRectTransform.offsetMin.y); viewportRectTransform.offsetMax = new Vector2(-attributes.GetValue <float>("width"), viewportRectTransform.offsetMax.y); } } } else { if (attributes.ContainsKey("height")) { var rectAlignment = attributes.GetValue("rectAlignment") ?? "LowerCenter"; if (rectAlignment.Contains("Lower")) { viewportRectTransform.offsetMin = new Vector2(viewportRectTransform.offsetMin.x, attributes.GetValue <float>("height")); viewportRectTransform.offsetMax = new Vector2(viewportRectTransform.offsetMax.x, 0); } else if (rectAlignment.Contains("Upper")) { viewportRectTransform.offsetMin = new Vector2(viewportRectTransform.offsetMin.x, 0); viewportRectTransform.offsetMax = new Vector2(viewportRectTransform.offsetMax.x, -attributes.GetValue <float>("height")); } } } } var pagination = pagedRectInstance.Pagination; var backupTransform = this.currentInstanceTransform; this.SetInstance(pagination.transform as RectTransform, this.currentXmlLayoutInstance); base.ApplyAttributes(attributes); this.SetInstance(backupTransform, this.currentXmlLayoutInstance); } }
public override void ApplyAttributes(AttributeDictionary attributesToApply) { base.ApplyAttributes(attributesToApply); var toggle = primaryComponent as Toggle; var checkMark = toggle.graphic as Image; var textComponent = currentXmlElement.GetComponentInChildren <Text>(); if (currentXmlElement.childElements.Any()) { var childAttributes = new AttributeDictionary(); var child = currentXmlElement.childElements.First(); if (child.tagType == "Text") { // Replace the original text component with the child element child.rectTransform.SetParent(textComponent.rectTransform.parent); if (Application.isPlaying) { GameObject.Destroy(textComponent.gameObject); } else { GameObject.DestroyImmediate(textComponent.gameObject); } textComponent = child.GetComponent <Text>(); if (!child.HasAttribute("alignment")) { childAttributes.Add("alignment", "MiddleLeft"); } } #if TEXTMESHPRO_PRESENT else if (child.tagType == "TextMeshPro") { // Replace the original text component with the child element child.rectTransform.SetParent(textComponent.rectTransform.parent); if (Application.isPlaying) { GameObject.Destroy(textComponent.gameObject); } else { GameObject.DestroyImmediate(textComponent.gameObject); } if (!child.HasAttribute("alignment")) { childAttributes.Add("alignment", "Left"); } } #endif if (!child.HasAttribute("text")) { if (attributesToApply.ContainsKey("text")) { childAttributes.Add("text", attributesToApply["text"]); } else { // if the child has no text, and the parent has no text, disable the text component if (!currentXmlElement.HasAttribute("text")) { childAttributes.Add("active", "false"); } } } else { // override the parent value if need be attributesToApply.SetValue("text", child.GetAttribute("text")); } // default padding value as per standard toggle element if (!child.HasAttribute("padding")) { childAttributes.Add("padding", "23 5 2 1"); } if (!child.HasAttribute("flexibleWidth")) { childAttributes.Add("flexibleWidth", "1"); } if (childAttributes.Any()) { child.ApplyAttributes(childAttributes); } } if (attributesToApply.ContainsKey("checkcolor")) { checkMark.color = attributesToApply["checkcolor"].ToColor(); } var targetGraphic = toggle.targetGraphic as Image; if (attributesToApply.ContainsKey("togglewidth")) { var width = float.Parse(attributesToApply["togglewidth"]); /*targetGraphic.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); * checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); * * if (textComponent != null) * { * textComponent.rectTransform.localPosition = new Vector2(width, textComponent.rectTransform.localPosition.y); * }*/ var toggleLayoutElement = targetGraphic.GetComponent <LayoutElement>() ?? targetGraphic.gameObject.AddComponent <LayoutElement>(); toggleLayoutElement.preferredWidth = width; toggleLayoutElement.minWidth = width; } if (attributesToApply.ContainsKey("toggleheight")) { var height = float.Parse(attributesToApply["toggleheight"]); /*targetGraphic.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); * checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);*/ var toggleLayoutElement = targetGraphic.GetComponent <LayoutElement>() ?? targetGraphic.gameObject.AddComponent <LayoutElement>(); toggleLayoutElement.preferredHeight = height; toggleLayoutElement.minHeight = height; } if (ToggleGroupTagHandler.CurrentToggleGroupInstance != null) { var xmlLayoutToggleGroupInstance = ToggleGroupTagHandler.CurrentToggleGroupInstance; xmlLayoutToggleGroupInstance.AddToggle(toggle); xmlLayoutToggleGroupInstance.UpdateToggleElement(toggle); toggle.onValueChanged.AddListener((e) => { if (e) { var value = xmlLayoutToggleGroupInstance.GetValueForElement(toggle); xmlLayoutToggleGroupInstance.SetSelectedValue(value); } }); } // Text attributes if (textComponent != null) { // disable the text component if there is no text textComponent.gameObject.SetActive(!String.IsNullOrEmpty(textComponent.text)); var tagHandler = XmlLayoutUtilities.GetXmlTagHandler("Text"); tagHandler.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"]); } tagHandler.ApplyAttributes(textAttributes); // disable the XmlElement component, it can interfere with mouse clicks/etc. textComponent.GetComponent <XmlElement>().enabled = false; } if (!attributesToApply.ContainsKey("text") && !currentXmlElement.attributes.ContainsKey("text")) { var background = (Image)toggle.targetGraphic; var backgroundTransform = background.rectTransform; if (!attributesToApply.ContainsKey("dontModifyBackground")) { backgroundTransform.anchorMin = new Vector2(0.5f, 0.5f); backgroundTransform.anchorMax = new Vector2(0.5f, 0.5f); backgroundTransform.anchoredPosition3D = new Vector3(0, 0, 0); } } if (attributesToApply.ContainsKey("togglebackgroundimage")) { targetGraphic.sprite = attributesToApply["togglebackgroundimage"].ToSprite(); } if (attributesToApply.ContainsKey("togglecheckmarkimage")) { checkMark.sprite = attributesToApply["togglecheckmarkimage"].ToSprite(); } if (attributesToApply.ContainsKey("togglecheckmarkimagepreserveaspect")) { checkMark.preserveAspect = attributesToApply["togglecheckmarkimagepreserveaspect"].ToBoolean(); } if (attributesToApply.ContainsKey("togglecheckmarksize")) { float checkSize = float.Parse(attributesToApply["togglecheckmarksize"]); //var checkMarkLayoutElement = checkMark.GetComponent<LayoutElement>(); //checkMarkLayoutElement.preferredWidth = checkSize; //checkMarkLayoutElement.preferredHeight = checkSize; checkMark.rectTransform.anchorMin = new Vector2(0.5f, 0.5f); checkMark.rectTransform.anchorMax = new Vector2(0.5f, 0.5f); checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, checkSize); checkMark.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, checkSize); } }