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); } }
public static void SetAnchor(this RectTransform _rectTransform, eAnchor _anchor, float _offsetX = 0f, float _offsetY = 0f) { switch (_anchor) { case (eAnchor.TopLeft): { _rectTransform.anchorMin = new Vector2(0, 1); _rectTransform.anchorMax = new Vector2(0, 1); break; } case (eAnchor.TopCenter): { _rectTransform.anchorMin = new Vector2(0.5f, 1); _rectTransform.anchorMax = new Vector2(0.5f, 1); break; } case (eAnchor.TopRight): { _rectTransform.anchorMin = new Vector2(1, 1); _rectTransform.anchorMax = new Vector2(1, 1); break; } case (eAnchor.MiddleLeft): { _rectTransform.anchorMin = new Vector2(0, 0.5f); _rectTransform.anchorMax = new Vector2(0, 0.5f); break; } case (eAnchor.MiddleCenter): { _rectTransform.anchorMin = new Vector2(0.5f, 0.5f); _rectTransform.anchorMax = new Vector2(0.5f, 0.5f); break; } case (eAnchor.MiddleRight): { _rectTransform.anchorMin = new Vector2(1, 0.5f); _rectTransform.anchorMax = new Vector2(1, 0.5f); break; } case (eAnchor.BottomLeft): { _rectTransform.anchorMin = new Vector2(0, 0); _rectTransform.anchorMax = new Vector2(0, 0); break; } case (eAnchor.BottomCenter): { _rectTransform.anchorMin = new Vector2(0.5f, 0); _rectTransform.anchorMax = new Vector2(0.5f, 0); break; } case (eAnchor.BottomRight): { _rectTransform.anchorMin = new Vector2(1, 0); _rectTransform.anchorMax = new Vector2(1, 0); break; } case (eAnchor.StretchTop): { _rectTransform.anchorMin = new Vector2(0, 1); _rectTransform.anchorMax = new Vector2(1, 1); break; } case (eAnchor.StretchMiddle): { _rectTransform.anchorMin = new Vector2(0, 0.5f); _rectTransform.anchorMax = new Vector2(1, 0.5f); break; } case (eAnchor.StretchBottom): { _rectTransform.anchorMin = new Vector2(0, 0); _rectTransform.anchorMax = new Vector2(1, 0); break; } case (eAnchor.StretchLeft): { _rectTransform.anchorMin = new Vector2(0, 0); _rectTransform.anchorMax = new Vector2(0, 1); break; } case (eAnchor.StretchCenter): { _rectTransform.anchorMin = new Vector2(0.5f, 0); _rectTransform.anchorMax = new Vector2(0.5f, 1); break; } case (eAnchor.StretchRight): { _rectTransform.anchorMin = new Vector2(1, 0); _rectTransform.anchorMax = new Vector2(1, 1); break; } case (eAnchor.Stretch): { _rectTransform.anchorMin = new Vector2(0, 0); _rectTransform.anchorMax = new Vector2(1, 1); break; } } var dx = 1; switch (_anchor) { case eAnchor.BottomRight: case eAnchor.MiddleRight: case eAnchor.StretchRight: case eAnchor.TopRight: dx = -1; break; } var dy = 1; switch (_anchor) { case eAnchor.StretchTop: case eAnchor.TopCenter: case eAnchor.TopLeft: case eAnchor.TopRight: dy = -1; break; } _rectTransform.anchoredPosition = new Vector3(_offsetX * dx, _offsetY * dy, 0); }