public static int GenerateNewId(VisualTreeAsset vta, VisualElementAsset vea)
        {
            int parentHash;

            if (!vea.HasParent())
            {
                parentHash = vta.GetHashCode();
            }
            else
            {
                parentHash = vea.parentId;
            }

            var guid = System.Guid.NewGuid().GetHashCode();

            return((vta.GetNextChildSerialNumber() + 585386304) * -1521134295 + parentHash + guid);
        }
Ejemplo n.º 2
0
        int GetRecursiveHashCode(VisualTreeAsset vta)
        {
            int hashCode;

#if UNITY_2020_1_OR_NEWER
            unchecked
            {
                hashCode = vta.contentHash;

                // When an attribute is edited, we need to recreate the whole UI
                // since UIElements has no way to update existing VisualElements
                // from the asset.
                hashCode = (hashCode * 397) ^ EditorUtility.GetDirtyCount(vta);

                foreach (var asset in vta.templateDependencies)
                {
                    hashCode = (hashCode * 397) ^ GetRecursiveHashCode(asset);
                }

                foreach (var asset in vta.stylesheets)
                {
                    if (asset != null)
                    {
                        hashCode = (hashCode * 397) ^ asset.contentHash;
                        hashCode = (hashCode * 397) ^ EditorUtility.GetDirtyCount(asset);
                    }
                }
            }
#else
            unchecked
            {
                hashCode = vta.GetHashCode();

                // When an attribute is edited, we need to recreate the whole UI
                // since UIElements has no way to update existing VisualElements
                // from the asset.
                hashCode = (hashCode * 397) ^ EditorUtility.GetDirtyCount(vta);

                // We can't detect changes to sub-UXML files (referenced templates) in 2019.3
                // We can't detect external content change to stylesheets in 2019.3
            }
#endif

            return(hashCode);
        }
Ejemplo n.º 3
0
        void LoadXml(XElement elt, VisualElementAsset parent, VisualTreeAsset vta, int orderInDocument)
        {
            VisualElementAsset vea = ResolveType(elt, vta);

            if (vea == null)
            {
                return;
            }

            int parentHash;

            if (parent == null)
            {
                vea.parentId = 0;
                parentHash   = vta.GetHashCode();
            }
            else
            {
                vea.parentId = parent.id;
                parentHash   = parent.id;
            }

            // id includes the parent id, meaning it's dependent on the whole direct hierarchy
            vea.id = (vta.GetNextChildSerialNumber() + 585386304) * -1521134295 + parentHash;
            vea.orderInDocument = orderInDocument;

            bool startedRule = ParseAttributes(elt, vea, vta, parent);

            // each vea will creates 0 or 1 style rule, with one or more properties
            // they don't have selectors and are directly referenced by index
            // it's then applied during tree cloning
            vea.ruleIndex = startedRule ? m_Builder.EndRule() : -1;
            var templateAsset = vea as TemplateAsset;

            if (templateAsset != null)
            {
                vta.templateAssets.Add(templateAsset);
            }
            else
            {
                vta.visualElementAssets.Add(vea);
            }

            if (elt.HasElements)
            {
                foreach (XElement child in elt.Elements())
                {
                    if (child.Name.LocalName == k_StyleReferenceNode)
                    {
                        LoadStyleReferenceNode(vea, child);
                    }
                    else if (templateAsset != null && child.Name.LocalName == k_AttributeOverridesNode)
                    {
                        LoadAttributeOverridesNode(templateAsset, child);
                    }
                    else
                    {
                        ++orderInDocument;
                        LoadXml(child, vea, vta, orderInDocument);
                    }
                }
            }
        }