Skip to content

Commit

Permalink
fix: avoid the selection.activeObject being reset to null when editin…
Browse files Browse the repository at this point in the history
…g an addressable prefab #71
  • Loading branch information
favoyang committed Apr 26, 2022
1 parent ff9ef02 commit e745f92
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion Editor/AddressableImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand Down

0 comments on commit e745f92

Please sign in to comment.