public void LoadDocument(VisualTreeAsset visualTreeAsset, VisualElement documentElement) { NewDocument(documentElement); if (visualTreeAsset == null) { return; } m_VisualTreeAssetBackup = visualTreeAsset.DeepCopy(); m_VisualTreeAsset = visualTreeAsset; m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); // Load styles. var styleSheetsUsed = m_VisualTreeAsset.GetAllReferencedStyleSheets(); for (int i = 0; i < styleSheetsUsed.Count; ++i) { AddStyleSheetToDocument(styleSheetsUsed[i], null); } m_OpenendVisualTreeAssetOldPath = AssetDatabase.GetAssetPath(m_VisualTreeAsset); hasUnsavedChanges = false; m_Settings = BuilderDocumentSettings.CreateOrLoadSettingsObject(m_Settings, uxmlPath); ReloadDocumentToCanvas(documentElement); }
bool PostSaveToDiskChecksAndFixes(string newUxmlPath, bool needsFullRefresh) { var oldVTAReference = m_VisualTreeAsset; var oldUxmlPath = uxmlPath; var hasNewUxmlPath = !string.IsNullOrEmpty(newUxmlPath) && newUxmlPath != oldUxmlPath; var localUxmlPath = !string.IsNullOrEmpty(newUxmlPath) ? newUxmlPath : oldUxmlPath; m_VisualTreeAsset = BuilderPackageUtilities.LoadAssetAtPath <VisualTreeAsset>(localUxmlPath); var newIsDifferentFromOld = m_VisualTreeAsset != oldVTAReference; // If we have a new uxmlPath, it means we're saving as and we need to reset the // original document to stock. if (hasNewUxmlPath && newIsDifferentFromOld && !string.IsNullOrEmpty(oldUxmlPath)) { m_DocumentBeingSavedExplicitly = true; AssetDatabase.ImportAsset(oldUxmlPath, ImportAssetOptions.ForceUpdate); m_DocumentBeingSavedExplicitly = false; } needsFullRefresh |= newIsDifferentFromOld; // Check if the UXML asset has changed and reload it. if (needsFullRefresh) { // To get all the selection markers into the new assets. m_VisualTreeAssetBackup.DeepOverwrite(m_VisualTreeAsset); m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); } return(needsFullRefresh); }
public void LoadDocument(VisualTreeAsset visualTreeAsset, VisualElement documentElement) { NewDocument(documentElement); if (visualTreeAsset == null) { return; } m_VisualTreeAssetBackup = visualTreeAsset.DeepCopy(); m_VisualTreeAsset = visualTreeAsset; m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); // Load styles. // TODO: For now we only support one stylesheet so we just load the first one we find. var styleSheetsUsed = m_VisualTreeAsset.GetAllReferencedStyleSheets(); var styleSheet = styleSheetsUsed.Count > 0 ? styleSheetsUsed[0] : null; m_MainStyleSheetBackup = null; if (styleSheet != null) { m_MainStyleSheetBackup = styleSheet.DeepCopy(); } m_MainStyleSheet = styleSheet; m_OpenendMainStyleSheetOldPath = AssetDatabase.GetAssetPath(styleSheet); hasUnsavedChanges = false; CreateOrLoadSettingsObject(); ReloadDocumentToCanvas(documentElement); SaveToDisk(); }
public void OnAfterLoadFromDisk() { // Very important we convert asset references to paths here after a restore. if (m_VisualTreeAsset != null) { m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); } }
public void PostLoadDocumentStyleSheetCleanup() { m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); // Load styles. var styleSheetsUsed = m_VisualTreeAsset.GetAllReferencedStyleSheets(); for (int i = 0; i < styleSheetsUsed.Count; ++i) { AddStyleSheetToDocument(styleSheetsUsed[i], null); } #if !UNITY_2019_4 // For the near to mid term, we have this code that cleans up any // existing root element stylesheets. RemoveLegacyStyleSheetsFromRootAssets(); #endif hasUnsavedChanges = false; }
void LoadFromDisk() { var path = diskJsonFilePath; if (!File.Exists(path)) { return; } var json = File.ReadAllText(path); EditorJsonUtility.FromJsonOverwrite(json, this); // Very important we convert asset references to paths here after a restore. if (m_VisualTreeAsset != null) { m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); } }
public bool SaveNewDocument(string uxmlPath, string ussPath, VisualElement documentRootElement, bool isSaveAs) { if (!isSaveAs) { var ussInstanceId = mainStyleSheet.GetInstanceID().ToString(); m_VisualTreeAsset.FixStyleSheetPaths(ussInstanceId, ussPath); // Fix old paths if the uss filename/path has since been changed. m_VisualTreeAsset.ReplaceStyleSheetPaths(ussOldPath, ussPath); #if UNITY_2019_3_OR_NEWER AddStyleSheetToAllRootElements(ussPath); #endif } else { visualTreeAsset.ReplaceStyleSheetPaths(ussOldPath, ussPath); } // Need to save a backup before the AssetDatabase.Refresh(). var tempVisualTreeAsset = m_VisualTreeAsset.DeepCopy(); var tempMainStyleSheet = m_MainStyleSheet.DeepCopy(); WriteToFiles(uxmlPath, ussPath); m_DocumentBeingSavedExplicitly = true; try { AssetDatabase.Refresh(); } finally { m_DocumentBeingSavedExplicitly = false; } var loadedVisualTreeAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(uxmlPath); var loadedStyleSheet = AssetDatabase.LoadAssetAtPath <StyleSheet>(ussPath); bool needFullRefresh = loadedVisualTreeAsset != m_VisualTreeAsset || loadedStyleSheet != m_MainStyleSheet; if (needFullRefresh && documentRootElement != null) { NewDocument(documentRootElement); // Re-init setting. if (m_Settings != null) { m_Settings.UxmlGuid = AssetDatabase.AssetPathToGUID(uxmlPath); m_Settings.UxmlPath = uxmlPath; SaveSettingsToDisk(); } } else { m_VisualTreeAsset.ClearUndo(); m_MainStyleSheet.ClearUndo(); ClearBackups(); } // Recreate backups. m_VisualTreeAssetBackup = loadedVisualTreeAsset.DeepCopy(); m_MainStyleSheetBackup = loadedStyleSheet.DeepCopy(); // To get all the selection markers into the new assets. tempVisualTreeAsset.DeepOverwrite(loadedVisualTreeAsset); tempMainStyleSheet.DeepOverwrite(loadedStyleSheet); // Destroy temps. tempVisualTreeAsset.Destroy(); tempMainStyleSheet.Destroy(); m_VisualTreeAsset = loadedVisualTreeAsset; m_MainStyleSheet = loadedStyleSheet; // Reset asset name. m_VisualTreeAsset.name = Path.GetFileNameWithoutExtension(uxmlPath); m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); m_OpenendVisualTreeAssetOldPath = uxmlPath; m_OpenendMainStyleSheetOldPath = ussPath; hasUnsavedChanges = false; SaveToDisk(); return(needFullRefresh); }
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); } } } // Save USS files. foreach (var openUSSFile in m_OpenUSSFiles) { openUSSFile.SaveToDisk(visualTreeAsset); } { // Save UXML file. // Need to save a backup before the AssetDatabase.Refresh(). if (m_VisualTreeAssetBackup == null) { m_VisualTreeAssetBackup = m_VisualTreeAsset.DeepCopy(); } else { m_VisualTreeAsset.DeepOverwrite(m_VisualTreeAssetBackup); } WriteUXMLToFile(newUxmlPath); } // 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; } // Check if any USS assets have changed reload them. foreach (var openUSSFile in m_OpenUSSFiles) { needsFullRefresh |= openUSSFile.PostSaveToDiskChecksAndFixes(); } { // Check if the UXML asset has changed and reload it. m_VisualTreeAsset = AssetDatabase.LoadAssetAtPath <VisualTreeAsset>(newUxmlPath); needsFullRefresh |= m_VisualTreeAsset != m_VisualTreeAssetBackup; } if (needsFullRefresh) { // Copy previous document settings. if (m_Settings != null) { m_Settings.UxmlGuid = AssetDatabase.AssetPathToGUID(newUxmlPath); m_Settings.UxmlPath = newUxmlPath; m_Settings.SaveSettingsToDisk(); } { // Fix up UXML asset. // To get all the selection markers into the new assets. m_VisualTreeAssetBackup.DeepOverwrite(m_VisualTreeAsset); // Reset asset name. m_VisualTreeAsset.name = Path.GetFileNameWithoutExtension(newUxmlPath); m_VisualTreeAsset.ConvertAllAssetReferencesToPaths(); m_OpenendVisualTreeAssetOldPath = newUxmlPath; } if (documentRootElement != null) { ReloadDocumentToCanvas(documentRootElement); } } hasUnsavedChanges = false; return(true); }