void Start() { XMLUI.Run(); TestView = XMLUI.CreateView <TestView>(); testButton.onClick.AddListener(OnTestButtonClicked); }
internal void InitializeText(XmlNode _xmlNode, XmlNode _style) { string fontName = _xmlNode.GetStringValue("Font", _style); Font font = XMLUI.FindFont(fontName); int fontSize = _xmlNode.GetIntValue("Size", _style); string upColorHex = _xmlNode.GetStringValue("Color", _style); string downColorHex = _xmlNode.GetStringValue("Down", _style); float spacing = _xmlNode.GetFloatValue("Spacing", _style); TextAnchor alignment = (TextAnchor)Enum.Parse(typeof(TextAnchor), _xmlNode.GetStringValue("Alignment", _style)); bool autoWrap = _xmlNode.GetBoolValue("AutoWrap", _style); bool bestFit = _xmlNode.GetBoolValue("BestFit", _style); int minSize = _xmlNode.GetIntValue("MinSize", _style); int maxSize = _xmlNode.GetIntValue("MaxSize", _style); string text = _xmlNode.GetStringValue("Text", _style); UpColor = ColorUtility.HexToColor(upColorHex); DownColor = ColorUtility.HexToColor(downColorHex); Property <string> property = null; string propertyName = string.Empty; var result = Regex.Matches(text, @"{[^{}\d]*}") .Cast <Match>() .Select(p => p.Value) .ToList(); if (result.Count > 0) { propertyName = result[0].Substring(1, result[0].Length - 2); property = (Property <string>)ReflectionUtility.FindProperty(this, propertyName); } InitializeTextComponent(font, FontStyle.Normal, fontSize, spacing, true, alignment, autoWrap ? HorizontalWrapMode.Wrap : HorizontalWrapMode.Overflow, VerticalWrapMode.Truncate, bestFit, minSize, maxSize, UpColor, text, property); bool isLabel = _xmlNode.GetBoolValue("Label"); if (isLabel) { (Parent as Button).Labels.Add(this); } }
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); } }
public static void ReadLayoutXmlDocument(XmlDocument _layoutXml, UIView _rootUIView, bool _root, UIView _classRoot, XmlNode _styleXml) { XmlElement layoutXmlRoot = _layoutXml.DocumentElement; List <XmlNode> layoutXmlNodes = layoutXmlRoot.GetAllChildrenXMLNodesRecursively(); Dictionary <XmlNode, UIView> viewByNodes = new Dictionary <XmlNode, UIView>(); viewByNodes.Add(layoutXmlRoot, _rootUIView); _rootUIView.ClassRoot = _classRoot ?? _rootUIView; if (_root) { ApplyLayoutXmlNode(layoutXmlRoot, _rootUIView, _styleXml); } foreach (XmlNode childLayoutXmlNode in layoutXmlNodes) { if (childLayoutXmlNode.NodeType == XmlNodeType.Comment) { continue; } XmlNode parentNode = childLayoutXmlNode.ParentNode; UIView childUIView = XMLUI.CreateView(childLayoutXmlNode.Name); childUIView.ClassRoot = _classRoot ?? _rootUIView; if (parentNode == layoutXmlRoot) { childUIView.Parent = _rootUIView; } else { childUIView.Parent = viewByNodes[parentNode]; } viewByNodes.Add(childLayoutXmlNode, childUIView); ApplyLayoutXmlNode(childLayoutXmlNode, childUIView, _styleXml); } }
void OnTestButtonClicked() { //TestView.TestString.Value = System.DateTime.Now.ToLongTimeString(); XMLUI.Dump(); }
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); } }