public static void RemoveStyleSheet(this VisualElementAsset vea, string styleSheetPath)
        {
            if (!vea.GetStyleSheetPaths().Contains(styleSheetPath))
            {
                return;
            }

            vea.GetStyleSheetPaths().RemoveAll((s) => s == styleSheetPath);
        }
        public static void AddStyleSheetPath(this VisualElementAsset vea, string styleSheetPath)
        {
            if (string.IsNullOrEmpty(styleSheetPath) || vea.GetStyleSheetPaths().Contains(styleSheetPath))
            {
                return;
            }

            vea.GetStyleSheetPaths().Add(styleSheetPath);
        }
Exemplo n.º 3
0
        static void GetAllReferencedStyleSheets(VisualElementAsset vea, HashSet <StyleSheet> sheets)
        {
#if UNITY_2019_3_OR_NEWER
            var styleSheets = vea.stylesheets;
            if (styleSheets != null)
            {
                foreach (var styleSheet in styleSheets)
                {
                    if (styleSheet != null) // Possible if the path is not valid.
                    {
                        sheets.Add(styleSheet);
                    }
                }
            }
#endif
            var styleSheetPaths = vea.GetStyleSheetPaths();
            if (styleSheetPaths != null)
            {
                foreach (var sheetPath in styleSheetPaths)
                {
                    var sheetAsset = AssetDatabase.LoadAssetAtPath <StyleSheet>(sheetPath);
                    if (sheetAsset == null)
                    {
                        sheetAsset = Resources.Load <StyleSheet>(sheetPath);
                        if (sheetAsset == null)
                        {
                            continue;
                        }
                    }

                    sheets.Add(sheetAsset);
                }
            }
        }
Exemplo n.º 4
0
        static void GetAllReferencedStyleSheets(VisualElementAsset vea, HashSet <StyleSheet> sheets)
        {
            var styleSheets = vea.stylesheets;

            if (styleSheets != null)
            {
                foreach (var styleSheet in styleSheets)
                {
                    if (styleSheet != null) // Possible if the path is not valid.
                    {
                        sheets.Add(styleSheet);
                    }
                }
            }

            var styleSheetPaths = vea.GetStyleSheetPaths();

            if (styleSheetPaths != null)
            {
                foreach (var sheetPath in styleSheetPaths)
                {
                    var sheetAsset = BuilderPackageUtilities.LoadAssetAtPath <StyleSheet>(sheetPath);
                    if (sheetAsset == null)
                    {
                        sheetAsset = Resources.Load <StyleSheet>(sheetPath);
                        if (sheetAsset == null)
                        {
                            continue;
                        }
                    }

                    sheets.Add(sheetAsset);
                }
            }
        }
        public static void ClearStyleSheets(this VisualElementAsset vea)
        {
#if UNITY_2019_3_OR_NEWER
            vea.stylesheets.Clear();
#endif
            vea.GetStyleSheetPaths().Clear();
        }
Exemplo n.º 6
0
        static void GenerateUXMLRecursive(
            VisualTreeAsset vta, string vtaPath, VisualElementAsset root,
            Dictionary <int, List <VisualElementAsset> > idToChildren,
            StringBuilder stringBuilder, int depth, bool writingToFile)
        {
            Indent(stringBuilder, depth);

            stringBuilder.Append("<");
            AppendElementTypeName(root, stringBuilder);

            // Add all non-style attributes.
            AppendElementNonStyleAttributes(root, stringBuilder, writingToFile);

            // Add style classes to class attribute.
            if (root.classes != null && root.classes.Length > 0)
            {
                stringBuilder.Append(" class=\"");
                for (int i = 0; i < root.classes.Length; i++)
                {
                    if (i > 0)
                    {
                        stringBuilder.Append(" ");
                    }

                    stringBuilder.Append(root.classes[i]);
                }
                stringBuilder.Append("\"");
            }

            // Add inline StyleSheet attribute.
            if (root.ruleIndex != -1)
            {
                if (vta.inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    StyleRule r = vta.inlineSheet.rules[root.ruleIndex];

                    if (r.properties != null && r.properties.Length > 0)
                    {
                        var ruleBuilder   = new StringBuilder();
                        var exportOptions = new UssExportOptions();
                        exportOptions.propertyIndent = string.Empty;
                        StyleSheetToUss.ToUssString(vta.inlineSheet, exportOptions, r, ruleBuilder);
                        var ruleStr = ruleBuilder.ToString();

                        // Need to remove newlines here before we give it to
                        // AppendElementAttribute() so we don't add "&#10;" everywhere.
                        ruleStr = ruleStr.Replace("\n", " ");
                        ruleStr = ruleStr.Replace("\r", "");
                        ruleStr = ruleStr.Trim();

                        AppendElementAttribute("style", ruleStr, stringBuilder);
                    }
                }
            }

            // If we have no children, avoid adding the full end tag and just end the open tag.
            bool hasChildTags = false;

            // Add special children.
            // TODO: What's the difference between these two ifdef options?
#if false
            var styleSheets = root.stylesheets;
            if (styleSheets != null && styleSheets.Count > 0)
            {
                bool newLineAdded = false;

                foreach (var styleSheet in styleSheets)
                {
                    var path = AssetDatabase.GetAssetPath(styleSheet);
                    ProcessStyleSheetPath(
                        vtaPath,
                        path, stringBuilder, depth,
                        ref newLineAdded, ref hasChildTags);
                }
            }
#else
            var styleSheetPaths = root.GetStyleSheetPaths();
            if (styleSheetPaths != null && styleSheetPaths.Count > 0)
            {
                bool newLineAdded = false;

                foreach (var path in styleSheetPaths)
                {
                    ProcessStyleSheetPath(
                        vtaPath,
                        path, stringBuilder, depth,
                        ref newLineAdded, ref hasChildTags);
                }
            }
#endif

            var templateAsset = root as TemplateAsset;
            if (templateAsset != null && templateAsset.attributeOverrides != null && templateAsset.attributeOverrides.Count > 0)
            {
                if (!hasChildTags)
                {
                    stringBuilder.Append(">");
                    stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
                }

                var overridesMap = new Dictionary <string, List <TemplateAsset.AttributeOverride> >();
                foreach (var attributeOverride in templateAsset.attributeOverrides)
                {
                    if (!overridesMap.ContainsKey(attributeOverride.m_ElementName))
                    {
                        overridesMap.Add(attributeOverride.m_ElementName, new List <TemplateAsset.AttributeOverride>());
                    }

                    overridesMap[attributeOverride.m_ElementName].Add(attributeOverride);
                }
                foreach (var attributeOverridePair in overridesMap)
                {
                    var elementName = attributeOverridePair.Key;
                    var overrides   = attributeOverridePair.Value;

                    Indent(stringBuilder, depth + 1);
                    stringBuilder.Append("<AttributeOverrides");
                    AppendElementAttribute("element-name", elementName, stringBuilder);

                    foreach (var attributeOverride in overrides)
                    {
                        AppendElementAttribute(attributeOverride.m_AttributeName, attributeOverride.m_Value, stringBuilder);
                    }

                    stringBuilder.Append(" />");
                    stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
                }

                hasChildTags = true;
            }

            // Iterate through child elements.
            List <VisualElementAsset> children;
            if (idToChildren != null && idToChildren.TryGetValue(root.id, out children) && children.Count > 0)
            {
                if (!hasChildTags)
                {
                    stringBuilder.Append(">");
                    stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
                }

                children.Sort(VisualTreeAssetUtilities.CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    GenerateUXMLRecursive(
                        vta, vtaPath, childVea, idToChildren, stringBuilder,
                        depth + 1, writingToFile);
                }

                hasChildTags = true;
            }

            if (hasChildTags)
            {
                Indent(stringBuilder, depth);
                stringBuilder.Append("</");
                AppendElementTypeName(root, stringBuilder);
                stringBuilder.Append(">");
                stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
            }
            else
            {
                stringBuilder.Append(" />");
                stringBuilder.Append(BuilderConstants.NewlineCharFromEditorSettings);
            }
        }
 public static void ClearStyleSheets(this VisualElementAsset vea)
 {
     vea.stylesheets.Clear();
     vea.GetStyleSheetPaths().Clear();
 }
 public static void RemoveStyleSheetPath(this VisualElementAsset vea, string styleSheetPath)
 {
     vea.GetStyleSheetPaths().RemoveAll((s) => s == styleSheetPath);
 }
Exemplo n.º 9
0
        static VisualElement CloneSetupRecursively(VisualTreeAsset vta, VisualElementAsset root,
                                                   Dictionary <int, List <VisualElementAsset> > idToChildren, CreationContext context)
        {
            // This is needed because of asset reloads during domain reloads where the
            // stylesheet path might be a temporary instance id to a pure in-memory stylesheet.
            List <string> originalStyleSheets = null;

#if UNITY_2019_3_OR_NEWER
            if (root.stylesheetPaths != null && root.stylesheetPaths.Count > 0)
#else
            if (root.stylesheets != null && root.stylesheets.Count > 0)
#endif
            {
                originalStyleSheets = root.GetStyleSheetPaths();
                var strippedList = originalStyleSheets.Where(
                    (s) => !s.StartsWith(BuilderConstants.VisualTreeAssetStyleSheetPathAsInstanceIdSchemeName));

#if UNITY_2019_3_OR_NEWER
                root.stylesheetPaths = strippedList.ToList();
#else
                root.stylesheets = strippedList.ToList();
#endif
            }

#if UNITY_2019_3_OR_NEWER
            var resolvedSheets = new List <StyleSheet>();
            foreach (var sheetPath in root.stylesheetPaths)
            {
                resolvedSheets.Add(AssetDatabase.LoadAssetAtPath <StyleSheet>(sheetPath));
            }
            root.stylesheets = resolvedSheets;
#endif

            var ve = VisualTreeAsset.Create(root, context);

            // Restore stylesheets.
            if (originalStyleSheets != null)
#if UNITY_2019_3_OR_NEWER
            { root.stylesheetPaths = originalStyleSheets; }
#else
            { root.stylesheets = originalStyleSheets; }
#endif

            // Linking the new element with its VisualElementAsset.
            // All this copied code for this one line!
            ve.SetProperty(BuilderConstants.ElementLinkedVisualElementAssetVEPropertyName, root);

            // context.target is the created templateContainer
            if (root.id == context.visualTreeAsset.contentContainerId)
            {
                if (context.target is TemplateContainer)
                {
                    ((TemplateContainer)context.target).SetContentContainer(ve);
                }
                else
                {
                    Debug.LogError(
                        "Trying to clone a VisualTreeAsset with a custom content container into a element which is not a template container");
                }
            }

            // if the current element had a slot-name attribute, put it in the resulting slot mapping
            string slotName;
            if (context.slotInsertionPoints != null && vta.TryGetSlotInsertionPoint(root.id, out slotName))
            {
                context.slotInsertionPoints.Add(slotName, ve);
            }

            if (root.classes != null)
            {
                for (int i = 0; i < root.classes.Length; i++)
                {
                    ve.AddToClassList(root.classes[i]);
                }
            }

            if (root.ruleIndex != -1)
            {
                if (vta.inlineSheet == null)
                {
                    Debug.LogWarning("VisualElementAsset has a RuleIndex but no inlineStyleSheet");
                }
                else
                {
                    var rule = vta.inlineSheet.rules[root.ruleIndex];
#if UNITY_2020_1_OR_NEWER
                    ve.SetInlineRule(vta.inlineSheet, rule);
#elif UNITY_2019_3_OR_NEWER
                    var stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    s_StylePropertyReader.SetInlineContext(vta.inlineSheet, rule, root.ruleIndex);
                    stylesData.ApplyProperties(s_StylePropertyReader, null);
#else
                    var stylesData = new VisualElementStylesData(false);
                    ve.SetInlineStyles(stylesData);
                    var propIds = StyleSheetCache.GetPropertyIDs(vta.inlineSheet, root.ruleIndex);
                    stylesData.ApplyRule(vta.inlineSheet, Int32.MaxValue, rule, propIds);
#endif
                }
            }

            var templateAsset = root as TemplateAsset;
            if (templateAsset != null)
            {
                var templatePath = vta.GetPathFromTemplateName(templateAsset.templateAlias);
                ve.SetProperty(BuilderConstants.LibraryItemLinkedTemplateContainerPathVEPropertyName, templatePath);
            }

            List <VisualElementAsset> children;
            if (idToChildren.TryGetValue(root.id, out children))
            {
                children.Sort(VisualTreeAssetUtilities.CompareForOrder);

                foreach (VisualElementAsset childVea in children)
                {
                    // this will fill the slotInsertionPoints mapping
                    VisualElement childVe = CloneSetupRecursively(vta, childVea, idToChildren, context);
                    if (childVe == null)
                    {
                        continue;
                    }

                    // if the parent is not a template asset, just add the child to whatever hierarchy we currently have
                    // if ve is a scrollView (with contentViewport as contentContainer), this will go to the right place
                    if (templateAsset == null)
                    {
                        ve.Add(childVe);
                        continue;
                    }

                    int index = templateAsset.slotUsages == null
                        ? -1
                        : templateAsset.slotUsages.FindIndex(u => u.assetId == childVea.id);
                    if (index != -1)
                    {
                        VisualElement parentSlot;
                        string        key = templateAsset.slotUsages[index].slotName;
                        Assert.IsFalse(String.IsNullOrEmpty(key),
                                       "a lost name should not be null or empty, this probably points to an importer or serialization bug");
                        if (context.slotInsertionPoints == null ||
                            !context.slotInsertionPoints.TryGetValue(key, out parentSlot))
                        {
                            Debug.LogErrorFormat("Slot '{0}' was not found. Existing slots: {1}", key,
                                                 context.slotInsertionPoints == null
                                ? String.Empty
                                : String.Join(", ",
                                              System.Linq.Enumerable.ToArray(context.slotInsertionPoints.Keys)));
                            ve.Add(childVe);
                        }
                        else
                        {
                            parentSlot.Add(childVe);
                        }
                    }
                    else
                    {
                        ve.Add(childVe);
                    }
                }
            }

            if (templateAsset != null && context.slotInsertionPoints != null)
            {
                context.slotInsertionPoints.Clear();
            }

            return(ve);
        }