Am I missing the point of Addressables?

I’m trying to get started with Addressables for one simple purpose; to load assets without having to stick them inside a Resources folder and calling Resources.Load.

The problem I’m having is I absolutely cannot find a single bit of information as to actually how. Every single tutorial or video or blog post or online forum thread I find, or even the official Addressables documentation, talks about creating an “AddressableAsset” field in a script, which is then assigned to through the Editor (inspector). Which I’m pretty sure completely defies the point of the Addressables system existing.

I seriously cannot find anything on how to actually do the equivalent of “Resources.Load” where I just put in a path string. Am I just being dumb? I just woke up and had breakfast so it’s definitely not me being in a tired stupor.

Honestly, even when I google something like “Unity Addressables get asset path at runtime”, anything I find, tells me to assign stuff through the Editor, which would not be possible at runtime.

    public static async Task<T> LoadAsset<T>(string key) where T : ScriptableObject
    {
        Debug.Log("Try loading component: " + key);

        try
        {

#if UNITY_EDITOR
            if (!Application.isPlaying)
            {
                Debug.Log("Loading from AssetDatabase directly: " + key);
                return UnityEditor.AssetDatabase.LoadAssetAtPath<T>(key);
            }

            if (!File.Exists(key))
            {
                Debug.LogError("File not found: " + key);
            }
#endif
            var handle = Addressables.LoadAssetAsync<T>(key);
            await handle.Task;

          
            if (handle.Status == AsyncOperationStatus.Failed)
            {
                Debug.LogError($"Load component failed from packed data. Key={key}");
                return null;
            }

            var obj = handle.Result;


            return obj;
        }
        catch (Exception ex)
        {
            ex.Log();
            throw;
        }
    }

This is what I am using. Basically, what you are looking for is Addressables.LoadAssetAsync()

The key param is the path starting at the Assets folder (if you didn’t change the default value).
e.g. “Assets/Abc.prefab”

Note that while Resources.Load don’t need to include the file extension, the default key, when you mark an asset as addressable, includes the file extension just like the example above with .prefab.

I’m still completely lost. :frowning:

I hate asking people to do stuff for me, but how exactly would this look in my scenario? I have no idea what I’m doing here and I feel like when I started out programming, just trying everything without any clue what I’m actually doing.

Let’s say I have a “Something” prefab that’s just an empty GameObject, and it’s in “Assets/Prefabs/”, and all I want to do is assign it to a variable inside a LoadMyPrefab.cs script. What would the whole thing look like?

I’m assuming I’m correct in thinking I need to do “using UnityEngine.AddressableAssets;” because that’s the only thing I’ve found anywhere I’ve looked.

Setting up Addressables comprises of two parts:

  1. in the editor, you need to setup some files that declare what assets are able to be loaded in game. You cannot just load any file willy-nilly. If you want to introduce a new asset to load in your game, it needs to be “marked as addressable”. Which basically comes down to the file needs to somehow be referenced in an “Addressable Group” file.
    These are best viewed going through Window → AssetManagement → Addressables → Groups. (also check out settings here). long term, it sounds like you may just want to mark a few folders as addressables, and you’ll be able to load all the files in that folder.
  2. You can load assets in game a couple of ways, using the function Addressables.LoadAssetAsync(key);
    the key can be a few different things, either a string GUID, an string “key” which is stored in addressables data, and auto-set to the path of the asset at the time it was marked addressable; or an AssetReference object, which is a glorified wrapper for the GUID, but it has a nice editor inspector, and is useful if you want to have a property on a script to assign in the editor what asset to load.

I recommend spending some time reading the documentation and looking over example projects.

Assuming you have already mark the Assets/Prefabs/ path to use addressable,
in that case, LoadMyPrefab.cs may have something like this to load the object.

   AsyncOperationHandle<GameObject> goHandle = Addressables.LoadAssetAsync<GameObject>("Assets/Prefabs/Something.prefab");
   yield return goHandle;
   if(goHandle.Status == AsyncOperationStatus.Succeeded)
   {
       GameObject theGameObjectYourWant = goHandle.Result;
       //etc...
   }
1 Like

Does this page not explain everything you need to know?

1 Like

Thank you, that works. <3

It kind of does, but it always cooks my brain when the answer to a problem is buried between a thousand other pieces of information. Not to mention the docs aren’t exactly intuitive to read through, just line of text upon line of text on the same solid white background with no clear dividers.