Exemple #1
0
    internal void InitializeButton(XmlNode _xmlNode, XmlNode _styleXml)
    {
        string methodName = _xmlNode.GetStringValue("OnClick");

        if (string.IsNullOrEmpty(methodName) == false)
        {
            MethodInfo = ReflectionUtility.FindMethod(this, methodName);
        }

        UpSprite = Image.sprite;

        var downImgName = _xmlNode.GetStringValue("Down", _styleXml);

        if (string.IsNullOrEmpty(downImgName) == false)
        {
            DownSprite = Resources.Load <Sprite>(downImgName);
        }
        else
        {
            DownSprite = Image.sprite;
        }

        string param = _xmlNode.GetStringValue("Text", _styleXml);

        if (string.IsNullOrEmpty(param) == false)
        {
            var styleId = "Default";
            var style   = _xmlNode.GetStringValue("Style");
            if (string.IsNullOrEmpty(style) == false)
            {
                styleId = style;
            }

            var styleXml = XMLUI.GetStyleXml("Text", styleId + "_ButtonLabel");
            styleXml.Attributes["Text"].Value = param;

            var view = XMLUI.CreateView <Text>(ClassRoot, styleXml);
            view.Parent = this;
            Labels.Add(view);
        }
    }
Exemple #2
0
    public static void ApplyLayoutXmlNode(XmlNode _xmlNode, UIView _view, XmlNode _styleXml)
    {
        // GameObject Name
        string name = _xmlNode.GetStringValue("Name");

        if (string.IsNullOrEmpty(name) == false)
        {
            _view.GameObject.name = name;
        }

        // Canvas
        bool isRoot = _xmlNode.GetBoolValue("Root");

        _view.InitializeCanvasRender(isRoot);

        bool ignoreInput = _xmlNode.GetBoolValue("IgnoreInput");

        if (ignoreInput == false)
        {
            _view.InitializeInput();
        }

        // Find Style Data
        string styleId = "Default";

        if (_xmlNode.Attributes["Style"] != null)
        {
            styleId = _xmlNode.Attributes["Style"].Value;
        }

        XmlNode styleXml = _styleXml ?? XMLUI.GetStyleXml(_view.Name, styleId);

        _view.StyleId = styleId;

        // width and height
        float width  = _xmlNode.GetFloatValue("Width", styleXml);
        float height = _xmlNode.GetFloatValue("Height", styleXml);

        _view.RectTransform.sizeDelta = new Vector2(width, height);

        // anchor & offset
        string anchorValue = _xmlNode.GetStringValue("Anchor", styleXml);
        float  offsetX     = _xmlNode.GetFloatValue("OffsetX", styleXml);
        float  offsetY     = _xmlNode.GetFloatValue("OffsetY", styleXml);

        eAnchor anchor = eAnchor.MiddleCenter;

        if (string.IsNullOrEmpty(anchorValue) == false)
        {
            anchor = (eAnchor)Enum.Parse(typeof(eAnchor), anchorValue);
        }

        _view.RectTransform.SetAnchor(anchor, offsetX, offsetY);

        // margin
        string marginValue = _xmlNode.GetStringValue("Margin");

        if (string.IsNullOrEmpty(marginValue) == false)
        {
            string[] margins = _xmlNode.Attributes["Margin"].Value.Split(',');
            float    left    = float.Parse(margins[0]);
            float    right   = float.Parse(margins[1]);
            float    top     = float.Parse(margins[2]);
            float    bottom  = float.Parse(margins[3]);

            Vector2 offsetMin = new Vector2(left, bottom);
            Vector2 offsetMax = new Vector2(-right, -top);

            _view.RectTransform.offsetMin = offsetMin;
            _view.RectTransform.offsetMax = offsetMax;
        }

        // Image
        string imgName = _xmlNode.GetStringValue("Image", styleXml);

        if (string.IsNullOrEmpty(imgName) == false)
        {
            var imgType = (UnityEngine.UI.Image.Type)Enum.Parse(typeof(UnityEngine.UI.Image.Type), _xmlNode.GetStringValue("Type", styleXml));
            _view.InitializeImageComponent(imgName, imgType);
        }

        Type viewType = _view.GetType();

        // Text
        if (viewType == typeof(Text))
        {
            Text text = _view as Text;
            text.InitializeText(_xmlNode, styleXml);
        }
        else if (viewType == typeof(Button))
        {
            Button button = _view as Button;
            button.InitializeButton(_xmlNode, styleXml);
        }
    }