public override void Update() { UpdateTextElements(); // Early out: no tracker found for panel. if (m_LiveReloadVisualTreeAssetTrackers.Count == 0 && m_LiveReloadStyleSheetAssetTracker == null) { return; } if (IsEditorPlaying.Invoke()) { long currentTimeMs = Panel.TimeSinceStartupMs(); if (currentTimeMs < m_LastUpdateTimeMs + kMinUpdateDelayMs) { return; } m_LastUpdateTimeMs = currentTimeMs; } // There's no need to reload anything if there are no changes. if (m_PreviousInMemoryAssetsVersion == UIElementsUtility.m_InMemoryAssetsVersion) { return; } // This value is updated by the UI Builder whenever an asset is changed. // We update here to prevent unnecessary checking of asset changes. m_PreviousInMemoryAssetsVersion = UIElementsUtility.m_InMemoryAssetsVersion; foreach (var tracker in m_LiveReloadVisualTreeAssetTrackers) { tracker.CheckTrackedAssetsDirty(); } if (m_LiveReloadStyleSheetAssetTracker != null && m_LiveReloadStyleSheetAssetTracker.CheckTrackedAssetsDirty()) { panel.DirtyStyleSheets(); } }
public override void Update() { if (!enable) { return; } // Windows can decide to enable/disable live reload of text elements, // For example, the UI Builder will only refresh changes made to font assets and not the rest. if ((enabledTrackers & LiveReloadTrackers.Text) != 0) { UpdateTextElements(); } // Windows can also decide to skip document live reload, and only do text elements. // The UI Builder will skip the live reload of hierarchy and styles, because it is already editing them. if ((enabledTrackers & LiveReloadTrackers.Document) == 0) { return; } // Early out: no tracker found for panel. if (m_EditorVisualTreeAssetTracker == null && m_RuntimeVisualTreeAssetTrackers.Count == 0) { return; } if (EditorApplication.isPlaying) { long currentTimeMs = Panel.TimeSinceStartupMs(); if (currentTimeMs < m_LastUpdateTimeMs + kMinUpdateDelayMs) { return; } m_LastUpdateTimeMs = currentTimeMs; } // There's no need to reload anything if there are no changes. if (m_PreviousInMemoryAssetsVersion == UIElementsUtility.m_InMemoryAssetsVersion) { return; } // This value is updated by the UI Builder whenever an asset is changed. // We update here to prevent unnecessary checking of asset changes. m_PreviousInMemoryAssetsVersion = UIElementsUtility.m_InMemoryAssetsVersion; bool shouldRefreshStyles = false; // We iterate on the assets to avoid calling GetDirtyCount for the same asset more than once. // In Editor this seems very likely and in Runtime we're assuming there are not multiple panels going // around, or if there are they're not using the same UXMLs but we may have to revisit this if we ever // detect that to be the case (to once again avoid calling GetDirtyCount multiple times on the same asset). foreach (var trackedAssetEntry in m_AssetToTrackerMap) { var trackedAsset = trackedAssetEntry.Key; var trackersEntry = trackedAssetEntry.Value; var dirtyCount = EditorUtility.GetDirtyCount(trackedAsset); // We keep a trace of the number of elements to minimize the cost of LiveReload on Layout/Style changes. // We also keep a trace of the number of inline rules, we need to recreate UI when they are added/removed. // Same goes for attribute changes, we need to re-Init elements that changed, so we recreate UI to simplify things. var elementCount = trackedAsset.visualElementAssets.Count; var inlinePropertiesCount = trackedAsset.inlineSheet.rules.Sum(r => r.properties.Length); var attributePropertiesDirtyCount = trackedAsset.GetAttributePropertiesDirtyCount(); bool shouldRecreateUI = false; if (dirtyCount != trackersEntry.m_LastDirtyCount) { trackersEntry.m_LastDirtyCount = dirtyCount; if (elementCount != trackersEntry.m_LastElementCount || inlinePropertiesCount != trackersEntry.m_LastInlinePropertiesCount || attributePropertiesDirtyCount != trackersEntry.m_LastAttributePropertiesDirtyCount) { trackersEntry.m_LastElementCount = elementCount; trackersEntry.m_LastInlinePropertiesCount = inlinePropertiesCount; trackersEntry.m_LastAttributePropertiesDirtyCount = attributePropertiesDirtyCount; shouldRecreateUI = true; } else { shouldRefreshStyles = true; } } foreach (var tracker in trackersEntry.m_Trackers) { // Update the dirty count on the tracker to keep the information correct everywhere. tracker.UpdateAssetTrackerCounts(trackedAsset, dirtyCount, elementCount, inlinePropertiesCount, attributePropertiesDirtyCount); // Add to list to make sure we only call each tracker only once. if (shouldRecreateUI) { m_TrackersToRefresh.Add(tracker); } } } foreach (var tracker in m_TrackersToRefresh) { tracker.OnTrackedAssetChanged(); } m_TrackersToRefresh.Clear(); if (shouldRefreshStyles || m_LiveReloadStyleSheetAssetTracker.CheckTrackedAssetsDirty()) { panel.DirtyStyleSheets(); panel.UpdateInlineStylesRecursively(); } }
public override void Update() { if (!enable) { return; } UpdateTextElements(); // Early out: no tracker found for panel. if (m_EditorVisualTreeAssetTracker == null && m_RuntimeVisualTreeAssetTrackers.Count == 0) { return; } if (EditorApplication.isPlaying) { long currentTimeMs = Panel.TimeSinceStartupMs(); if (currentTimeMs < m_LastUpdateTimeMs + kMinUpdateDelayMs) { return; } m_LastUpdateTimeMs = currentTimeMs; } // There's no need to reload anything if there are no changes. if (m_PreviousInMemoryAssetsVersion == UIElementsUtility.m_InMemoryAssetsVersion) { return; } // This value is updated by the UI Builder whenever an asset is changed. // We update here to prevent unnecessary checking of asset changes. m_PreviousInMemoryAssetsVersion = UIElementsUtility.m_InMemoryAssetsVersion; // We iterate on the assets to avoid calling GetDirtyCount for the same asset more than once. // In Editor this seems very likely and in Runtime we're assuming there are not multiple panels going // around, or if there are they're not using the same UXMLs but we may have to revisit this if we ever // detect that to be the case (to once again avoid calling GetDirtyCount multiple times on the same asset). foreach (var trackedAssetEntry in m_AssetToTrackerMap) { var trackedAsset = trackedAssetEntry.Key; var trackersEntry = trackedAssetEntry.Value; int dirtyCount = EditorUtility.GetDirtyCount(trackedAsset); if (dirtyCount != trackersEntry.m_LastDirtyCount) { trackersEntry.m_LastDirtyCount = dirtyCount; foreach (var tracker in trackersEntry.m_Trackers) { // Update the dirty count on the tracker to keep the information correct everywhere. tracker.UpdateAssetDirtyCount(trackedAsset, dirtyCount); // Add to list to make sure we only call each tracker only once. m_TrackersToRefresh.Add(tracker); } } } foreach (var tracker in m_TrackersToRefresh) { tracker.OnTrackedAssetChanged(); } m_TrackersToRefresh.Clear(); if (m_LiveReloadStyleSheetAssetTracker.CheckTrackedAssetsDirty()) { panel.DirtyStyleSheets(); } }