How to load and instantiate Addressables in Editor script

I’m running the following code in an Editor script but it’s only instantiating the gameobjects sometimes. After searching for this topic, it seems that the issue is that the Editor would only run this async function when triggering an action. Trying to solve to issue myself has also made me notice that the method starts working after making a script change and the Editor does its usual domain reload / script rebuild.

void SpawnPrefab(ShapeData shapeData)
{
    Addressables.LoadAssetsAsync<GameObject>(shapeData.AddressableTags(), obj =>
    {
        GameObject _newShape = PrefabUtility.InstantiatePrefab(obj) as GameObject;
        (Object.targetObject as LevelBuilder).ApplyDataToShape(_newShape, shapeData);
        EditorReadParams(_newShape, shapeData);
    }, Addressables.MergeMode.Intersection, false);
}

My ideal outcome is to make the instantiation more consistent. To have it immediately instantiate the gameobject without having to trigger an action or domain reload.

The solution people have recommended is to use 1. AssetDatabase or 2. EditorCoroutines.

  1. If I were to use AssetDatabase instead how would I get the path to the GameObject? I rely on the Addressable’s labels to fetch the specific GameObject to instantiate.

  2. I have imported the EditorCoroutines package and tested it out. It still doesn’t work. I can confirm that the Coroutine is running but the AsyncOperationHandle doesn’t seem to be running. I have checked against it’s PercentCompleted (returning 0), Status (returning None), and GetDownloadStatus.Percent (returning 0).

Edit1 : Unity Editor is 2022.3.9f1 and Addressables package is 1.21.17

I have found a workaround by fetching the addressable entry.

GameObject GetAddressableObject(ShapeData shapeData)
{
    ///Same as addressable labels
    List<string> _shapeLabels = shapeData.AddressableTags();

    AddressableAssetSettings _addressableSettings = AddressableAssetSettingsDefaultObject.Settings;

    ///Get addressable group by name
    AddressableAssetGroup _group = _addressableSettings.FindGroup(c_AddressableShapePrefix + shapeData.ShapeEffect.AsFriendlyName());

    ///Find the entry that matches the strings in _shapeLabels.
    foreach(AddressableAssetEntry _entry in _group.entries)
    {
        bool _foundAsset = true;

        for (int i = 0; i < _shapeLabels.Count; i++)
        {
            if (!_entry.labels.Contains(_shapeLabels[i]))
            {
                _foundAsset = false;
                break;
            }
        }

        if(_foundAsset) return _entry.MainAsset as GameObject;
    }

    StringBuilder _warningMessage = new StringBuilder("Could not find ");

    for (int i = 0; i < _shapeLabels.Count; i++)
    {
        if (i > 0) _warningMessage.Append(LevelBuilder.c_Split);

        _warningMessage.Append(_shapeLabels[i]);
    }

    Debug.LogWarning(_warningMessage);

    return null;
}