Ejemplo n.º 1
0
        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);
        }