private void DrawUxmlDump(VisualElement selectedElement)
 {
     this.m_UxmlDumpExpanded = EditorGUILayout.Foldout(this.m_UxmlDumpExpanded, UIElementsDebugger.Styles.uxmlContent);
     if (this.m_UxmlDumpExpanded)
     {
         EditorGUI.BeginChangeCheck();
         this.m_DumpId = EditorGUILayout.TextField("Template id", this.m_DumpId, new GUILayoutOption[0]);
         this.m_UxmlDumpStyleFields = EditorGUILayout.Toggle("Include style fields", this.m_UxmlDumpStyleFields, new GUILayoutOption[0]);
         this.m_NewLineOnAttributes = EditorGUILayout.Toggle("Line breaks on attributes", this.m_NewLineOnAttributes, new GUILayoutOption[0]);
         this.m_AutoNameElements    = EditorGUILayout.Toggle("Auto name elements", this.m_AutoNameElements, new GUILayoutOption[0]);
         if (EditorGUI.EndChangeCheck())
         {
             this.m_SelectedElementUxml = null;
         }
         if (this.m_SelectedElementUxml == null)
         {
             UxmlExporter.ExportOptions exportOptions = UxmlExporter.ExportOptions.None;
             if (this.m_UxmlDumpStyleFields)
             {
                 exportOptions = UxmlExporter.ExportOptions.StyleFields;
             }
             if (this.m_NewLineOnAttributes)
             {
                 exportOptions |= UxmlExporter.ExportOptions.NewLineOnAttributes;
             }
             if (this.m_AutoNameElements)
             {
                 exportOptions |= UxmlExporter.ExportOptions.AutoNameElements;
             }
             this.m_SelectedElementUxml = UxmlExporter.Dump(selectedElement, this.m_DumpId ?? "template", exportOptions);
         }
         EditorGUILayout.TextArea(this.m_SelectedElementUxml, new GUILayoutOption[0]);
     }
 }
Example #2
0
        private static void Recurse(XElement parent, Dictionary <XNamespace, string> nsToPrefix, HashSet <string> usings, VisualElement ve, UxmlExporter.ExportOptions options)
        {
            string namespaceName = ve.GetType().Namespace ?? "";
            string typeName      = ve.typeName;
            Dictionary <string, string> dictionary = new Dictionary <string, string>();
            XElement xElement;
            string   text;

            if (ve is TemplateContainer)
            {
                string templateId = ((TemplateContainer)ve).templateId;
                xElement = new XElement(templateId);
                usings.Add(templateId);
            }
            else if (nsToPrefix.TryGetValue(namespaceName, out text))
            {
                xElement = new XElement(namespaceName + typeName);
            }
            else
            {
                xElement = new XElement(typeName);
            }
            parent.Add(xElement);
            foreach (KeyValuePair <string, string> current in dictionary)
            {
                xElement.SetAttributeValue(current.Key, current.Value);
            }
            string text2 = (!(ve is BaseTextElement)) ? "" : (ve as BaseTextElement).text;

            if (!string.IsNullOrEmpty(ve.name) && ve.name[0] != '_')
            {
                xElement.SetAttributeValue("name", ve.name);
            }
            else if ((options & UxmlExporter.ExportOptions.AutoNameElements) == UxmlExporter.ExportOptions.AutoNameElements)
            {
                string value = ve.GetType().Name + text2.Replace(" ", "");
                xElement.SetAttributeValue("name", value);
            }
            if (!string.IsNullOrEmpty(text2))
            {
                xElement.SetAttributeValue("text", text2);
            }
            IEnumerable <string> classes = ve.GetClasses();

            if (classes.Any <string>())
            {
                xElement.SetAttributeValue("class", string.Join(" ", classes.ToArray <string>()));
            }
            if (!(ve is TemplateContainer))
            {
                foreach (VisualElement current2 in ve.Children())
                {
                    UxmlExporter.Recurse(xElement, nsToPrefix, usings, current2, options);
                }
            }
        }
Example #3
0
        public static string Dump(VisualElement selectedElement, string templateId, UxmlExporter.ExportOptions options)
        {
            Dictionary <XNamespace, string> dictionary = new Dictionary <XNamespace, string>
            {
                {
                    "UnityEngine.Experimental.UIElements",
                    "ui"
                }
            };
            HashSet <string> hashSet   = new HashSet <string>();
            XDocument        xDocument = new XDocument();
            XElement         xElement  = new XElement("UXML");

            xDocument.Add(xElement);
            UxmlExporter.Recurse(xElement, dictionary, hashSet, selectedElement, options);
            foreach (KeyValuePair <XNamespace, string> current in dictionary)
            {
                xElement.Add(new XAttribute(XNamespace.Xmlns + current.Value, current.Key));
            }
            foreach (string current2 in from x in hashSet
                     orderby x descending
                     select x)
            {
                xElement.AddFirst(new XElement("Using", new object[]
                {
                    new XAttribute("alias", current2),
                    new XAttribute("path", current2)
                }));
            }
            XmlWriterSettings settings = new XmlWriterSettings
            {
                Indent              = true,
                IndentChars         = "  ",
                NewLineChars        = "\n",
                OmitXmlDeclaration  = true,
                NewLineOnAttributes = ((options & UxmlExporter.ExportOptions.NewLineOnAttributes) == UxmlExporter.ExportOptions.NewLineOnAttributes),
                NewLineHandling     = NewLineHandling.Replace
            };
            StringBuilder stringBuilder = new StringBuilder();

            using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, settings))
            {
                xDocument.Save(xmlWriter);
            }
            return(stringBuilder.ToString());
        }