public static TemplateAsset AddTemplateInstance( this VisualTreeAsset vta, VisualElementAsset parent, string path) { var templateName = vta.GetTemplateNameFromPath(path); if (!vta.TemplateExists(templateName)) { var resolvedAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(path); if (resolvedAsset) { vta.RegisterTemplate(templateName, resolvedAsset); } else { vta.RegisterTemplate(templateName, path); } } var templateAsset = new TemplateAsset(templateName, BuilderConstants.UxmlInstanceTypeName); VisualTreeAssetUtilities.InitializeElement(templateAsset); templateAsset.AddProperty("template", templateName); return(VisualTreeAssetUtilities.AddElementToDocument(vta, templateAsset, parent) as TemplateAsset); }
public static void Swallow(this VisualTreeAsset vta, VisualElementAsset parent, VisualTreeAsset other) { var otherIdToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(other); if (parent == null) { parent = vta.GetRootUXMLElement(); } var nextOrderInDocument = (vta.visualElementAssets.Count + vta.templateAssets.Count) * BuilderConstants.VisualTreeAssetOrderIncrement; var assetsList = new List <VisualElementAsset>(); assetsList.AddRange(other.visualElementAssets); assetsList.AddRange(other.templateAssets); assetsList = assetsList.OrderBy(x => x.orderInDocument).ToList(); foreach (var asset in assetsList) { if (other.IsRootUXMLElement(asset)) { continue; } ReinitElementWithNewParentAsset( vta, parent, other, otherIdToChildren, asset, ref nextOrderInDocument); } foreach (var vea in other.visualElementAssets) { if (other.IsRootUXMLElement(vea)) { continue; } vta.visualElementAssets.Add(vea); } foreach (var vea in other.templateAssets) { if (!vta.TemplateExists(vea.templateAlias)) { vta.RegisterTemplate(vea.templateAlias, other.ResolveTemplate(vea.templateAlias)); } vta.templateAssets.Add(vea); } VisualTreeAssetUtilities.ReOrderDocument(vta); }
public static TemplateAsset AddTemplateInstance( this VisualTreeAsset vta, VisualElementAsset parent, string path) { var templateName = vta.GetTemplateNameFromPath(path); if (!vta.TemplateExists(templateName)) vta.RegisterTemplate(templateName, path); #if UNITY_2019_4 var templateAsset = new TemplateAsset(templateName); #else var templateAsset = new TemplateAsset(templateName, BuilderConstants.UxmlInstanceTypeName); #endif VisualTreeAssetUtilities.InitializeElement(templateAsset); templateAsset.AddProperty("template", templateName); return VisualTreeAssetUtilities.AddElementToDocument(vta, templateAsset, parent) as TemplateAsset; }
public static void Swallow(this VisualTreeAsset vta, VisualElementAsset parent, VisualTreeAsset other) { var otherIdToChildren = VisualTreeAssetUtilities.GenerateIdToChildren(other); if (parent == null) { parent = vta.GetRootUXMLElement(); } var nextOrderInDocument = (vta.visualElementAssets.Count + vta.templateAssets.Count) * BuilderConstants.VisualTreeAssetOrderIncrement; foreach (var vea in other.visualElementAssets) { if (other.IsRootUXMLElement(vea)) { continue; } ReinitElementWithNewParentAsset( vta, parent, other, otherIdToChildren, vea, ref nextOrderInDocument); vta.visualElementAssets.Add(vea); } foreach (var vea in other.templateAssets) { ReinitElementWithNewParentAsset( vta, parent, other, otherIdToChildren, vea, ref nextOrderInDocument); if (!vta.TemplateExists(vea.templateAlias)) { var path = other.GetPathFromTemplateName(vea.templateAlias); vta.RegisterTemplate(vea.templateAlias, path); } vta.templateAssets.Add(vea); } VisualTreeAssetUtilities.ReOrderDocument(vta); }
void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child) { bool hasPath = false; bool hasSrc = false; string name = null; string path = null; string src = null; foreach (var xAttribute in child.Attributes()) { switch (xAttribute.Name.LocalName) { case k_GenericPathAttr: hasPath = true; path = xAttribute.Value; break; case k_GenericSrcAttr: hasSrc = true; src = xAttribute.Value; break; case k_TemplateNameAttr: name = xAttribute.Value; if (String.IsNullOrEmpty(name)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.TemplateHasEmptyName, child, Error.Level.Fatal, child ); } break; default: logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownAttribute, xAttribute.Name.LocalName, Error.Level.Fatal, child ); break; } } if (hasPath == hasSrc) { logger.LogError(ImportErrorType.Semantic, hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute, null, Error.Level.Fatal, elt ); return; } if (String.IsNullOrEmpty(name)) { name = Path.GetFileNameWithoutExtension(path); } if (vta.TemplateExists(name)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateTemplateName, name, Error.Level.Fatal, elt ); return; } if (hasPath) { vta.RegisterTemplate(name, path); } else if (hasSrc) { string errorMessage, projectRelativePath; URIValidationResult result = URIHelpers.ValidAssetURL(assetPath, src, out errorMessage, out projectRelativePath); if (result != URIValidationResult.OK) { logger.LogError(ImportErrorType.Semantic, ConvertErrorCode(result), errorMessage, Error.Level.Fatal, elt); } else { Object asset = DeclareDependencyAndLoad(projectRelativePath); if (asset is VisualTreeAsset) { vta.RegisterTemplate(name, asset as VisualTreeAsset); } else { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, Error.Level.Fatal, elt); } } } }
void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child) { bool hasPath = false; string name = null; string path = null; foreach (var xAttribute in child.Attributes()) { switch (xAttribute.Name.LocalName) { case k_TemplatePathAttr: hasPath = true; path = xAttribute.Value; break; case k_TemplateNameAttr: name = xAttribute.Value; if (String.IsNullOrEmpty(name)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.TemplateHasEmptyName, child, Error.Level.Fatal, child ); } break; default: logger.LogError(ImportErrorType.Semantic, ImportErrorCode.UnknownAttribute, xAttribute.Name.LocalName, Error.Level.Fatal, child ); break; } } if (!hasPath) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.MissingPathAttributeOnTemplate, null, Error.Level.Fatal, elt ); return; } if (String.IsNullOrEmpty(name)) { name = Path.GetFileNameWithoutExtension(path); } if (vta.TemplateExists(name)) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.DuplicateTemplateName, name, Error.Level.Fatal, elt ); return; } vta.RegisterTemplate(name, path); }
void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child) { bool hasPath = false; bool hasSrc = false; string name = null; string path = null; string src = null; foreach (var xAttribute in child.Attributes()) { switch (xAttribute.Name.LocalName) { case k_GenericPathAttr: hasPath = true; path = xAttribute.Value; break; case k_GenericSrcAttr: hasSrc = true; src = xAttribute.Value; break; case k_TemplateNameAttr: name = xAttribute.Value; if (String.IsNullOrEmpty(name)) { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.TemplateHasEmptyName, child, child ); } break; default: LogError(vta, ImportErrorType.Semantic, ImportErrorCode.UnknownAttribute, xAttribute.Name.LocalName, child ); break; } } if (hasPath == hasSrc) { LogError(vta, ImportErrorType.Semantic, hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute, null, elt ); return; } if (String.IsNullOrEmpty(name)) { name = Path.GetFileNameWithoutExtension(path); } if (vta.TemplateExists(name)) { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.DuplicateTemplateName, name, elt ); return; } if (hasPath) { vta.RegisterTemplate(name, path); } else if (hasSrc) { var response = URIHelpers.ValidateAssetURL(assetPath, src); var result = response.result; var projectRelativePath = response.resolvedProjectRelativePath; if (response.hasWarningMessage) { logger.LogError(ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidURIProjectAssetPath, response.warningMessage, Error.Level.Warning, elt); } if (result != URIValidationResult.OK) { LogError(vta, ImportErrorType.Semantic, ConvertErrorCode(result), response.errorToken, elt); } else { var asset = response.resolvedQueryAsset; if (asset && m_Context != null) { m_Context.DependsOnSourceAsset(projectRelativePath); } else { asset = DeclareDependencyAndLoad(projectRelativePath); } if (asset is VisualTreeAsset treeAsset) { vta.RegisterTemplate(name, treeAsset); } else { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, projectRelativePath, elt); } } } }
void LoadTemplateNode(VisualTreeAsset vta, XElement elt, XElement child) { bool hasPath = false; bool hasSrc = false; string name = null; string path = null; string src = null; foreach (var xAttribute in child.Attributes()) { switch (xAttribute.Name.LocalName) { case k_GenericPathAttr: hasPath = true; path = xAttribute.Value; break; case k_GenericSrcAttr: hasSrc = true; src = xAttribute.Value; break; case k_TemplateNameAttr: name = xAttribute.Value; if (String.IsNullOrEmpty(name)) { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.TemplateHasEmptyName, child, child ); } break; default: LogError(vta, ImportErrorType.Semantic, ImportErrorCode.UnknownAttribute, xAttribute.Name.LocalName, child ); break; } } if (hasPath == hasSrc) { LogError(vta, ImportErrorType.Semantic, hasPath ? ImportErrorCode.TemplateSrcAndPathBothSpecified : ImportErrorCode.TemplateMissingPathOrSrcAttribute, null, elt ); return; } if (String.IsNullOrEmpty(name)) { name = Path.GetFileNameWithoutExtension(path); } if (vta.TemplateExists(name)) { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.DuplicateTemplateName, name, elt ); return; } if (hasPath) { vta.RegisterTemplate(name, path); } else if (hasSrc) { var(validationResponse, asset) = ValidateAndLoadResource(elt, vta, src); if (validationResponse.result == URIValidationResult.OK) { if (asset is VisualTreeAsset treeAsset) { vta.RegisterTemplate(name, treeAsset); } else { LogError(vta, ImportErrorType.Semantic, ImportErrorCode.ReferenceInvalidAssetType, validationResponse.resolvedProjectRelativePath, elt); } } } }