static Scrollbar CreateScrollbar(RectTransform widget, MarkupTheme theme, bool isHorizontal) { Image img; var width = theme.GetScrollbarWidth(); // background. var rt = MarkupUtils.CreateUiObject(null, widget); var sb = rt.gameObject.AddComponent <Scrollbar> (); img = rt.gameObject.AddComponent <Image> (); img.sprite = theme.GetScrollbarSprite(MarkupTheme.ScrollbarState.Background); img.color = theme.GetScrollbarColor(MarkupTheme.ScrollbarState.Background); img.type = Image.Type.Sliced; if (isHorizontal) { rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.right; rt.pivot = Vector2.zero; rt.sizeDelta = new Vector2(0f, width); } else { rt.anchorMin = Vector2.right; rt.anchorMax = Vector2.one; rt.pivot = Vector2.one; rt.sizeDelta = new Vector2(width, 0f); } // handle. rt = MarkupUtils.CreateUiObject(null, rt); img = rt.gameObject.AddComponent <Image> (); img.sprite = theme.GetScrollbarSprite(MarkupTheme.ScrollbarState.Handle); img.color = theme.GetScrollbarColor(MarkupTheme.ScrollbarState.Handle); img.type = Image.Type.Sliced; img.raycastTarget = false; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.offsetMin = Vector2.zero; rt.offsetMax = Vector2.zero; sb.direction = isHorizontal ? Scrollbar.Direction.LeftToRight : Scrollbar.Direction.BottomToTop; sb.handleRect = rt; sb.transition = Selectable.Transition.None; return(sb); }
/// <summary> /// Create "scrollView" 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 = "scrollView"; #endif float amount; string attrValue; Scrollbar horScroll = null; Scrollbar verScroll = null; var anchorMin = Vector2.zero; var anchorMax = Vector2.one; var offsetMin = Vector3.zero; var offsetMax = Vector3.zero; var needHorScroll = true; var needVerScroll = true; var theme = MarkupUtils.GetTheme(node, container); var scrollView = widget.gameObject.AddComponent <ScrollRect> (); // viewport. var viewport = MarkupUtils.CreateUiObject(ViewportName, widget); viewport.gameObject.AddComponent <RectMask2D> (); viewport.gameObject.AddComponent <NonVisualWidget> ().raycastTarget = false; viewport.anchorMin = Vector2.zero; viewport.anchorMax = Vector2.one; viewport.sizeDelta = Vector2.zero; viewport.pivot = Vector2.up; // content. var content = MarkupUtils.CreateUiObject(ContentName, viewport); attrValue = node.GetAttribute(HashedContentSize); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length > 0 && !string.IsNullOrEmpty(parts[0])) { if (float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.5f; offsetMin.x = -amount; offsetMax.x = amount; anchorMin.x = 0.5f; anchorMax.x = 0.5f; } } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.5f; offsetMin.y = -amount; offsetMax.y = amount; anchorMin.y = 0.5f; anchorMax.y = 0.5f; } } } content.anchorMin = anchorMin; content.anchorMax = anchorMax; content.offsetMin = offsetMin; content.offsetMax = offsetMax; attrValue = node.GetAttribute(HashedClamp); if (string.CompareOrdinal(attrValue, "true") == 0) { scrollView.movementType = ScrollRect.MovementType.Clamped; } attrValue = node.GetAttribute(HashedDrag); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length > 0 && string.CompareOrdinal(parts[0], "false") == 0) { scrollView.horizontal = false; } if (parts.Length > 1 && string.CompareOrdinal(parts[1], "false") == 0) { scrollView.vertical = false; } } attrValue = node.GetAttribute(HashedBar); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length > 0 && string.CompareOrdinal(parts[0], "false") == 0) { needHorScroll = false; } if (parts.Length > 1 && string.CompareOrdinal(parts[1], "false") == 0) { needVerScroll = false; } } if (needHorScroll) { horScroll = CreateScrollbar(widget, theme, true); MarkupUtils.SetNav(horScroll, node, container.UseNavigation); } if (needVerScroll) { verScroll = CreateScrollbar(widget, theme, false); MarkupUtils.SetNav(verScroll, node, container.UseNavigation); } attrValue = node.GetAttribute(HashedOnChange); if (!string.IsNullOrEmpty(attrValue)) { widget.gameObject.AddComponent <NonVisualWidget> (); widget.gameObject.AddComponent <UiScrollViewAction> ().SetGroup(attrValue); } scrollView.content = content; scrollView.viewport = viewport; scrollView.horizontalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollView.verticalScrollbarVisibility = ScrollRect.ScrollbarVisibility.AutoHideAndExpandViewport; scrollView.horizontalScrollbar = horScroll; scrollView.verticalScrollbar = verScroll; scrollView.decelerationRate = 0.01f; MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); scrollView.normalizedPosition = Vector2.up; return(content); }
/// <summary> /// Create "toggle" 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 = "toggle"; #endif var tr = widget.transform; Image img; RectTransform rt; Vector2 size; string attrValue; var toggle = widget.gameObject.AddComponent <Toggle> (); var theme = MarkupUtils.GetTheme(node, container); var isInteractive = false; // background. rt = MarkupUtils.CreateUiObject(BackgroundImageName, tr); img = rt.gameObject.AddComponent <Image> (); img.sprite = theme.GetToggleSprite(MarkupTheme.ToggleState.Background); img.type = Image.Type.Sliced; img.raycastTarget = false; size = theme.GetToggleSize(MarkupTheme.ToggleState.Background); rt.anchorMin = new Vector2(0f, 0.5f); rt.anchorMax = new Vector2(0f, 0.5f); rt.offsetMin = new Vector2(0f, -size.y * 0.5f); rt.offsetMax = new Vector2(size.x, size.y * 0.5f); toggle.targetGraphic = img; // foreground. rt = MarkupUtils.CreateUiObject(ForegroundImageName, rt); img = rt.gameObject.AddComponent <Image> (); rt.anchorMin = new Vector2(0.5f, 0.5f); rt.anchorMax = new Vector2(0.5f, 0.5f); rt.sizeDelta = theme.GetToggleSize(MarkupTheme.ToggleState.Foreground); img.sprite = theme.GetToggleSprite(MarkupTheme.ToggleState.Foreground); img.type = Image.Type.Sliced; img.raycastTarget = false; toggle.graphic = img; // content. rt = MarkupUtils.CreateUiObject(ContentName, tr); rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.offsetMin = Vector2.right * size.x; rt.offsetMax = Vector2.zero; attrValue = node.GetAttribute(HashedGroup); if (!string.IsNullOrEmpty(attrValue)) { var groupGo = container.GetNamedNode(attrValue); if ((object)groupGo != null) { toggle.group = groupGo.GetComponent <ToggleGroup> (); } } attrValue = node.GetAttribute(HashedCheck); if (string.CompareOrdinal(attrValue, "true") == 0) { toggle.isOn = true; } attrValue = node.GetAttribute(HashedOnChange); if (!string.IsNullOrEmpty(attrValue)) { widget.gameObject.AddComponent <NonVisualWidget> (); widget.gameObject.AddComponent <UiToggleAction> ().SetGroup(attrValue); isInteractive = true; } toggle.transition = Selectable.Transition.None; toggle.interactable = isInteractive; MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); MarkupUtils.SetNav(toggle, node, container.UseNavigation); return(rt); }
/// <summary> /// Create "slider" 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 = "slider"; #endif var slider = widget.gameObject.AddComponent <Slider> (); var theme = MarkupUtils.GetTheme(node, container); Image img; RectTransform rt; var direction = Slider.Direction.LeftToRight; var minValue = 0f; var maxValue = 1f; var useInts = false; var dataValue = 0f; var isInteractive = false; // background. rt = MarkupUtils.CreateUiObject(BackgroundImageName, widget); img = rt.gameObject.AddComponent <Image> (); img.sprite = theme.GetSliderSprite(MarkupTheme.SliderState.Background); img.color = theme.GetSliderColor(MarkupTheme.SliderState.Background); img.type = Image.Type.Sliced; rt.anchorMin = Vector2.zero; rt.anchorMax = Vector2.one; rt.sizeDelta = Vector2.zero; // foreground. rt = MarkupUtils.CreateUiObject(ForegroundImageName, widget); img = rt.gameObject.AddComponent <Image> (); img.sprite = theme.GetSliderSprite(MarkupTheme.SliderState.Foreground); img.color = theme.GetSliderColor(MarkupTheme.SliderState.Foreground); img.type = Image.Type.Sliced; img.raycastTarget = false; rt.sizeDelta = Vector2.zero; slider.fillRect = rt; string attrValue; attrValue = node.GetAttribute(HashedHandle); var useHandle = string.CompareOrdinal(attrValue, "true") == 0; if (useHandle) { var handle = MarkupUtils.CreateUiObject(null, widget); slider.handleRect = handle; rt = MarkupUtils.CreateUiObject(HandleImageName, handle); img = rt.gameObject.AddComponent <Image> (); img.raycastTarget = false; img.sprite = theme.GetSliderSprite(MarkupTheme.SliderState.Handle); img.color = theme.GetSliderColor(MarkupTheme.SliderState.Handle); img.type = Image.Type.Sliced; img.SetNativeSize(); handle.sizeDelta = img.rectTransform.sizeDelta; } attrValue = node.GetAttribute(HashedRtl); if (string.CompareOrdinal(attrValue, "true") == 0) { direction = Slider.Direction.RightToLeft; } float amount; attrValue = node.GetAttribute(HashedRange); if (!string.IsNullOrEmpty(attrValue)) { var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length > 0 && !string.IsNullOrEmpty(parts[0])) { if (float.TryParse(parts[0], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { minValue = amount; } } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { maxValue = amount; } } if (parts.Length > 2 && string.CompareOrdinal(parts[2], "true") == 0) { useInts = true; } } attrValue = node.GetAttribute(HashedValue); if (!string.IsNullOrEmpty(attrValue)) { if (float.TryParse(attrValue, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { dataValue = amount; } } attrValue = node.GetAttribute(HashedOnChange); if (!string.IsNullOrEmpty(attrValue)) { widget.gameObject.AddComponent <UiSliderAction> ().SetGroup(attrValue); isInteractive = true; } slider.minValue = minValue; slider.maxValue = maxValue; slider.wholeNumbers = useInts; slider.value = dataValue; slider.direction = direction; slider.transition = Selectable.Transition.None; MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); MarkupUtils.SetNav(slider, node, container.UseNavigation); slider.interactable = useHandle && isInteractive; return(widget); }
/// <summary> /// Create "input" 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 = "input"; #endif var input = widget.gameObject.AddComponent <InputField> (); string attrValue; string font = null; var align = TextAnchor.MiddleLeft; var style = FontStyle.Normal; var size = 24; var isInteractive = false; var theme = MarkupUtils.GetTheme(node, container); var img = widget.gameObject.AddComponent <Image> (); img.type = Image.Type.Sliced; img.sprite = theme.GetInputSprite(); img.color = theme.GetInputColor(MarkupTheme.InputState.Background); var ph = MarkupUtils.CreateUiObject(null, widget); var phText = ph.gameObject.AddComponent <Text> (); phText.fontStyle = FontStyle.Italic; var txt = MarkupUtils.CreateUiObject(null, widget); var inText = txt.gameObject.AddComponent <Text> (); inText.supportRichText = false; 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)) { phText.gameObject.AddComponent <TextLocalization> ().SetToken(attrValue); } else { phText.text = node.Value; } var margin = theme.GetInputMargin(); var marginMin = new Vector2(margin, margin); var marginMax = -marginMin; ph.anchorMin = Vector2.zero; ph.anchorMax = Vector2.one; ph.offsetMin = marginMin; ph.offsetMax = marginMax; txt.anchorMin = Vector2.zero; txt.anchorMax = Vector2.one; txt.offsetMin = marginMin; txt.offsetMax = marginMax; phText.raycastTarget = false; inText.raycastTarget = false; phText.alignment = align; inText.alignment = align; phText.font = container.GetFont(font); inText.font = phText.font; phText.color = theme.GetInputColor(MarkupTheme.InputState.Placeholder); if (!MarkupUtils.SetColor(inText, node)) { inText.color = Color.black; } phText.fontStyle = theme.GetInputPlaceholderStyle(); inText.fontStyle = style; phText.fontSize = size; inText.fontSize = size; input.targetGraphic = img; input.transition = Selectable.Transition.None; input.placeholder = phText; input.textComponent = inText; input.selectionColor = theme.GetInputColor(MarkupTheme.InputState.Selection); MarkupUtils.SetSize(widget, node); MarkupUtils.SetRotation(widget, node); MarkupUtils.SetOffset(widget, node); MarkupUtils.SetHidden(widget, node); MarkupUtils.SetNav(input, node, container.UseNavigation); attrValue = node.GetAttribute(HashedOnChange); if (!string.IsNullOrEmpty(attrValue)) { widget.gameObject.AddComponent <UiInputAction> ().SetGroup(attrValue); isInteractive = true; } input.interactable = isInteractive; return(widget); }