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 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); }
public bool SaveNewDocument( VisualElement documentRootElement, bool isSaveAs, out bool needsFullRefresh, string manualUxmlPath = null) { needsFullRefresh = false; ClearUndo(); // Re-use or ask the user for the UXML path. var newUxmlPath = uxmlPath; if (string.IsNullOrEmpty(newUxmlPath) || isSaveAs) { if (!string.IsNullOrEmpty(manualUxmlPath)) { newUxmlPath = manualUxmlPath; } else { newUxmlPath = BuilderDialogsUtility.DisplaySaveFileDialog("Save UXML", null, null, "uxml"); if (newUxmlPath == null) // User cancelled the save dialog. { return(false); } } } List <BuilderDocumentOpenUSS> savedUSSFiles = new List <BuilderDocumentOpenUSS>(); // Save USS files. foreach (var openUSSFile in m_OpenUSSFiles) { if (openUSSFile.SaveToDisk(visualTreeAsset)) { savedUSSFiles.Add(openUSSFile); } } var oldUxmlTest = m_VisualTreeAssetBackup?.GenerateUXML(m_OpenendVisualTreeAssetOldPath, true); // Save UXML files // Saving all open UXML files to ensure references correct upon changes in child documents. foreach (var openUXMLFile in openUXMLFiles) { openUXMLFile.PreSaveSyncBackup(); } bool shouldSave = m_OpenendVisualTreeAssetOldPath != newUxmlPath; var uxmlText = visualTreeAsset.GenerateUXML(newUxmlPath, true); if (uxmlText != null) { if (!shouldSave && m_VisualTreeAssetBackup) { shouldSave = oldUxmlTest != uxmlText; } if (shouldSave) { WriteUXMLToFile(newUxmlPath, uxmlText); } } // Once we wrote all the files to disk, we refresh the DB and reload // the files from the AssetDatabase. m_DocumentBeingSavedExplicitly = true; try { AssetDatabase.Refresh(); } finally { m_DocumentBeingSavedExplicitly = false; } // Reorder document after reimporting VisualTreeAssetUtilities.ReOrderDocument(m_VisualTreeAsset); // Check if any USS assets have changed reload them. foreach (var openUSSFile in savedUSSFiles) { needsFullRefresh |= openUSSFile.PostSaveToDiskChecksAndFixes(); } // Check if any UXML assets have changed and reload them. // Saving all open UXML files to ensure references correct upon changes in child subdocuments. foreach (var openUXMLFile in openUXMLFiles) { needsFullRefresh |= openUXMLFile.PostSaveToDiskChecksAndFixes(this == openUXMLFile ? newUxmlPath : null, needsFullRefresh); } if (needsFullRefresh) { // Copy previous document settings. if (m_Settings != null) { m_Settings.UxmlGuid = AssetDatabase.AssetPathToGUID(newUxmlPath); m_Settings.UxmlPath = newUxmlPath; m_Settings.SaveSettingsToDisk(); } // Reset asset name. m_VisualTreeAsset.name = Path.GetFileNameWithoutExtension(newUxmlPath); m_OpenendVisualTreeAssetOldPath = newUxmlPath; } if (documentRootElement != null) { ReloadDocumentToCanvas(documentRootElement); } hasUnsavedChanges = false; return(true); }