Example #1
0
        /// <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);
        }
        /// <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);
        }
Example #3
0
        /// <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);
        }