From e745f92af10a843c4aca8bf8f8641fb001229be6 Mon Sep 17 00:00:00 2001 From: Favo Yang Date: Tue, 26 Apr 2022 20:10:00 +0800 Subject: [PATCH] fix: avoid the selection.activeObject being reset to null when editing an addressable prefab #71 --- Editor/AddressableImporter.cs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Editor/AddressableImporter.cs b/Editor/AddressableImporter.cs index ec6961d..b4d1932 100644 --- a/Editor/AddressableImporter.cs +++ b/Editor/AddressableImporter.cs @@ -16,8 +16,23 @@ using UnityEditor.Experimental.SceneManagement; #endif +[InitializeOnLoad] public class AddressableImporter : AssetPostprocessor { + // The selection active object + static UnityEngine.Object selectionActiveObject = null; + + static AddressableImporter() + { + Selection.selectionChanged += OnSelectionChanged; + + } + + static void OnSelectionChanged() + { + selectionActiveObject = Selection.activeObject; + } + static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) { // Skip if all imported and deleted assets are addressables configurations. @@ -46,8 +61,9 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse if (importSettings.rules == null || importSettings.rules.Count == 0) return; + // Cache the selection active object + var cachedSelectionActiveObject = selectionActiveObject; var dirty = false; - // Apply import rules. var prefabStage = PrefabStageUtility.GetCurrentPrefabStage(); #if UNITY_2020_1_OR_NEWER @@ -91,6 +107,9 @@ static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAsse if (dirty) { AssetDatabase.SaveAssets(); + // Restore the cached selection active object to avoid the current selection being set to null by + // saving changed Addressable groups (#71). + Selection.activeObject = cachedSelectionActiveObject; } }