/// <summary> /// Create "box" node. If children supported - GameObject container for them should be returned. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> /// <param name="container">Markup container.</param> public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container) { #if UNITY_EDITOR widget.name = "box"; #endif MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); if (MarkupUtils.ValidateInteractive(widget, node, container.DragTreshold)) { widget.gameObject.AddComponent <NonVisualWidget> (); } return(widget); }
/// <summary> /// Create "button" node. If children supported - GameObject container for them should be returned. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> /// <param name="container">Markup container.</param> public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container) { #if UNITY_EDITOR widget.name = "button"; #endif var btn = widget.gameObject.AddComponent <Button> (); var img = widget.gameObject.AddComponent <Image> (); string attrValue; var transition = Selectable.Transition.ColorTint; attrValue = node.GetAttribute(HashedBlend); switch (attrValue) { case "sprites": transition = Selectable.Transition.SpriteSwap; break; case "none": transition = Selectable.Transition.None; break; } var theme = MarkupUtils.GetTheme(node, container); switch (transition) { case Selectable.Transition.ColorTint: var colors = btn.colors; colors.normalColor = theme.GetButtonColor(MarkupTheme.ButtonState.Normal); colors.pressedColor = theme.GetButtonColor(MarkupTheme.ButtonState.Pressed); colors.highlightedColor = theme.GetButtonColor(MarkupTheme.ButtonState.Highlighted); colors.disabledColor = theme.GetButtonColor(MarkupTheme.ButtonState.Disabled); btn.colors = colors; break; case Selectable.Transition.SpriteSwap: var sprites = btn.spriteState; sprites.pressedSprite = theme.GetButtonSprite(MarkupTheme.ButtonState.Pressed); sprites.highlightedSprite = theme.GetButtonSprite(MarkupTheme.ButtonState.Highlighted); sprites.disabledSprite = theme.GetButtonSprite(MarkupTheme.ButtonState.Disabled); btn.spriteState = sprites; break; } img.sprite = theme.GetButtonSprite(MarkupTheme.ButtonState.Normal); img.type = Image.Type.Sliced; btn.targetGraphic = img; btn.transition = transition; MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); MarkupUtils.SetNav(btn, node, container.UseNavigation); attrValue = node.GetAttribute(HashedDisabled); var disabled = string.CompareOrdinal(attrValue, "true") == 0; btn.interactable = !disabled && MarkupUtils.ValidateInteractive(widget, node, container.DragTreshold); return(widget); }
/// <summary> /// Create "image" node. If children supported - GameObject container for them should be returned. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> /// <param name="container">Markup container.</param> public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container) { #if UNITY_EDITOR widget.name = "image"; #endif Image img = null; RawImage tex = null; string attrValue; var useImg = true; var ignoreSize = false; var fillCenter = true; attrValue = node.GetAttribute(HashedRaw); if (string.CompareOrdinal(attrValue, "true") == 0) { useImg = false; tex = widget.gameObject.AddComponent <RawImage> (); } else { img = widget.gameObject.AddComponent <Image> (); img.type = Image.Type.Sliced; } attrValue = node.GetAttribute(HashedPath); if (!string.IsNullOrEmpty(attrValue)) { if (useImg) { // Image. var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length == 2) { var atlas = container.GetAtlas(parts[0]); if ((object)atlas != null) { img.sprite = atlas.Get(parts[1]); } } } else { // RawImage. tex.texture = Resources.Load <Texture2D> (attrValue); } } if (useImg) { attrValue = node.GetAttribute(HashedNativeSize); ignoreSize = (object)img.sprite != null && string.CompareOrdinal(attrValue, "true") == 0; } if (useImg && ignoreSize) { img.SetNativeSize(); } else { MarkupUtils.SetSize(widget, node); } attrValue = node.GetAttribute(HashedMask); if (string.CompareOrdinal(attrValue, "true") == 0) { widget.gameObject.AddComponent <Mask> (); } if (useImg) { attrValue = node.GetAttribute(HashedFillCenter); if (string.CompareOrdinal(attrValue, "false") == 0) { fillCenter = false; } img.fillCenter = fillCenter; } if (useImg && !MarkupUtils.SetColor(img, node)) { img.color = Color.white; } MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); var isInteractive = MarkupUtils.ValidateInteractive(widget, node, container.DragTreshold); if (useImg) { img.raycastTarget = isInteractive; } else { tex.raycastTarget = isInteractive; } return(widget); }
/// <summary> /// Create "text" node. If children supported - GameObject container for them should be returned. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> /// <param name="container">Markup container.</param> public static RectTransform Create(RectTransform widget, XmlNode node, MarkupContainer container) { #if UNITY_EDITOR widget.name = "text"; #endif var txt = widget.gameObject.AddComponent <Text> (); string attrValue; string font = null; var align = TextAnchor.MiddleCenter; var style = FontStyle.Normal; var size = 24; attrValue = node.GetAttribute(HashedFontName); if (!string.IsNullOrEmpty(attrValue)) { font = attrValue; } attrValue = node.GetAttribute(HashedFontSize); if (!string.IsNullOrEmpty(attrValue)) { int.TryParse(attrValue, out size); } attrValue = node.GetAttribute(HashedFontStyle); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); for (var i = 0; i < parts.Length; i++) { switch (parts[i]) { case "bold": style |= FontStyle.Bold; break; case "italic": style |= FontStyle.Italic; break; } } } attrValue = node.GetAttribute(HashedAlign); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); var alignHor = 1; var alignVer = 3; for (var i = 0; i < parts.Length; i++) { switch (parts[i]) { case "left": alignHor = 0; break; case "right": alignHor = 2; break; case "top": alignVer = 0; break; case "bottom": alignVer = 6; break; } } align = (TextAnchor)(alignHor + alignVer); } attrValue = node.GetAttribute(HashedLocalize); if (!string.IsNullOrEmpty(attrValue)) { widget.gameObject.AddComponent <TextLocalization> ().SetToken(attrValue); } else { txt.text = node.Value; } txt.alignment = align; txt.font = container.GetFont(font); txt.fontStyle = style; txt.fontSize = size; if (!MarkupUtils.SetColor(txt, node)) { txt.color = Color.black; } MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); txt.raycastTarget = MarkupUtils.ValidateInteractive(widget, node, container.DragTreshold); return(widget); }