/// <summary> /// Process "offset" attribute of node. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> public static void SetOffset(RectTransform widget, XmlNode node) { var point = Vector3.zero; float amount; var attrValue = node.GetAttribute(HashedOffset); 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)) { point.x = amount; } } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { point.y = amount; } } } widget.localPosition = point; }
/// <summary> /// Force cleanup / create widgets infrastructure from attached xml-schema. /// </summary> public void CreateVisuals() { ValidateGenerators(); if ((object)_defaultFont == null) { _defaultFont = _fonts.Count > 0 ? _fonts[0] : Resources.GetBuiltinResource <Font> ("Arial.ttf"); } if ((object)_defaultTheme == null) { _defaultTheme = _themes.Count > 0 ? _themes[0] : ScriptableObject.CreateInstance <MarkupTheme> (); } if (_xmlTree == null) { _xmlTree = LoadXml(_markupPath); } ClearVisuals(); CreateVisualNode(_xmlTree, MarkupUtils.CreateUiObject("root", transform)); _isVisualized = true; }
/// <summary> /// Process "rotation" attribute of node. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> public static void SetRotation(RectTransform widget, XmlNode node) { var angles = Vector3.zero; float amount; var attrValue = node.GetAttribute(HashedRotation); 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)) { if (parts.Length > 1) { angles.x = amount; } else { angles.z = amount; } } } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { if (float.TryParse(parts[1], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { angles.y = amount; } } if (parts.Length > 2 && !string.IsNullOrEmpty(parts[2])) { if (float.TryParse(parts[2], NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { angles.z = amount; } } } widget.localRotation = Quaternion.Euler(angles); }
/// <summary> /// Process "size" attribute of node. /// </summary> /// <param name="widget">Ui widget.</param> /// <param name="node">Xml node.</param> public static void SetSize(RectTransform widget, XmlNode node) { var anchorMin = Vector2.zero; var anchorMax = Vector2.one; var offsetMin = Vector3.zero; var offsetMax = Vector3.zero; string amountStr; float amount; string attrValue; attrValue = node.GetAttribute(HashedSize); if (!string.IsNullOrEmpty(attrValue)) { int percentIdx; var parts = MarkupUtils.SplitAttrValue(attrValue); if (parts.Length > 0 && !string.IsNullOrEmpty(parts[0])) { amountStr = parts[0]; percentIdx = amountStr.IndexOf('%'); if (percentIdx != -1) { // relative. if (float.TryParse( amountStr.Substring(0, percentIdx), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.01f * 0.5f; anchorMin.x = 0.5f - amount; anchorMax.x = 0.5f + amount; } } else { // absolute. if (float.TryParse(amountStr, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.5f; anchorMin.x = 0.5f; anchorMax.x = 0.5f; offsetMin.x = -amount; offsetMax.x = amount; } } } if (parts.Length > 1 && !string.IsNullOrEmpty(parts[1])) { amountStr = parts[1]; percentIdx = amountStr.IndexOf('%'); if (percentIdx != -1) { // relative. if (float.TryParse( amountStr.Substring(0, percentIdx), NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.01f * 0.5f; anchorMin.y = 0.5f - amount; anchorMax.y = 0.5f + amount; } } else { // absolute. if (float.TryParse(amountStr, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out amount)) { amount *= 0.5f; anchorMin.y = 0.5f; anchorMax.y = 0.5f; offsetMin.y = -amount; offsetMax.y = amount; } } } } widget.anchorMin = anchorMin; widget.anchorMax = anchorMax; widget.offsetMin = offsetMin; widget.offsetMax = offsetMax; }
void CreateVisualNode(XmlNode xmlTree, RectTransform root) { if (xmlTree == null) { return; } Func <RectTransform, XmlNode, MarkupContainer, RectTransform> generator; var isFound = Generators.TryGetValue(xmlTree.NameHash, out generator); if (!isFound) { generator = BoxNode.Create; } var tr = MarkupUtils.CreateUiObject(null, root); var contentTr = generator(tr, xmlTree, this); #if UNITY_EDITOR if (!isFound) { tr.name = string.Format("unknown-replaced-with-{0}", tr.name); Debug.LogWarningFormat(tr, "Unknown hashed-node \"{0}\" - box-node will be used instead", xmlTree.NameHash); } #endif if ((object)_canvas == null) { _canvas = tr.GetComponentInChildren <Canvas> (); } var nodeName = xmlTree.GetAttribute(HashedName); if (!string.IsNullOrEmpty(nodeName)) { #if UNITY_EDITOR tr.name = string.Format("{0}-{1}", tr.name, nodeName); #endif var nodeNameHash = nodeName.GetStableHashCode(); if (_namedNodes.ContainsKey(nodeNameHash)) { #if UNITY_EDITOR Debug.LogWarning("Duplicate name: " + nodeName); #endif } else { _namedNodes[nodeNameHash] = tr; } } var children = xmlTree.Children; if (children.Count > 0) { if ((object)contentTr != null) { for (int i = 0, iMax = children.Count; i < iMax; i++) { CreateVisualNode(children[i], contentTr); } } else { Debug.LogWarning("Node not supported children.", tr); } } }