Search Unity

Help with Basic ASync Loading (Locally)

Discussion in 'Addressables' started by EricMay, Feb 3, 2020.

  1. EricMay

    EricMay

    Joined:
    Feb 2, 2018
    Posts:
    6
    Hello friends,

    I have spent the past few days learning about Addressables as they seem quite powerful and perfect for what I'm developing, but I am having a lot of trouble understanding some basic implementation for it. Particularly with loading/unloading local assets asynchronously. If anyone has the time to help me out (maybe there's sample code or project I can see that does this? Everything I have found so far involves remote loading) I would be very gracious.

    Here's what I have so far...

    I have a folder in my Unity project with a bunch of prefabs I've made, looks like this:


    I then turn them into Addressable Assets with the following code:


    It works well enough (I think...) though I'm probably not creating that new Settings file properly as it gives me warning meesage (no big deal for now):


    This creates the following:


    This brings me to where I am stuck. I want to load + instantiate some of these prefabs, and also unload them + destroy them at runtime. Note that I'd ideally like to use the Addressables.LoadAssets function, but I'm not sure how. Here's the (not-working) code I have so far (simplified):


    And the errors it's giving me:


    I'm not sure what I'm doing wrong, the address I'm feeding in should be correct, it comes back as "Succeeded" yet still fails? I find the documentation for Addressables outdated and not very helpful.

    Any and all help is greatly appreciated. Thank you for your time!
     
  2. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    You have to instantiate in Object_Completed.

    You can do this to capture the cell.

    Code (CSharp):
    1. objectHandle.Completed += handle =>
    2. {
    3.     cell.instance = (GameObject) Instantiate(handle.Result);
    4. }
     
  3. EricMay

    EricMay

    Joined:
    Feb 2, 2018
    Posts:
    6
    (EDITED)

    Thank you for your reply! I think we're getting closer but still getting some errors:


    Here's the code (simplified, just doing it with one of the cells for testing):


    Apologies, I'm not so great with delegates.
     
    Last edited: Feb 3, 2020
  4. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    You're telling it to load an object every frame. That's 60 times a second on a typical machine. Try running it from Start just to see if it works.
    Also, check your address. That's a really long address, try shortening it to make it more comprehensible. You can change the asset's address in the inspector or via the Addressables groups window.
     
    EricMay likes this.
  5. EricMay

    EricMay

    Joined:
    Feb 2, 2018
    Posts:
    6
    Hey ProtoTerminator, good suggestion.

    I tried simplifying and doing it at the Start, but I'm still getting that error. Here's the setup:

    Addressables:


    Code:


    Errors:


    Perhaps it has something to do with the prefab being greyed-out in that Addressables window? Do I have to build each prefab as its own separate addressable asset?
     
  6. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Hm, looks like you have the folder as addressable. I haven't tried using addressable folders, that could be an issue, not really sure.
    Also try LoadAsset<GameObject> instead of Object.
     
    EricMay likes this.
  7. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Oh, I think I see your issue. You're adding another callback to Completed when that method itself is already called from the Completed! Remove
    handle1.Complete += ...
    and just
    Instantiate(handle1.Result)
    .
     
    EricMay likes this.
  8. EricMay

    EricMay

    Joined:
    Feb 2, 2018
    Posts:
    6
    Yessss it works! (Well, sort of hehe)

    For some strange reason I'm still getting those same errors for my cell prefabs. I even tried unpacking them and re-prefabify them... But what's weird is that it works with a simple cube prefab. Check it out:


    You answered my question though, you've been so helpful I really appreciate it ProtoTerminator. I'll try to figure this one out and post my solution (if I find it) here if you're curious! Strange one this is.
     
  9. ProtoTerminator

    ProtoTerminator

    Joined:
    Nov 19, 2013
    Posts:
    586
    Not sure without seeing your code, but it looks like you're trying to load the cube's address, and when you make the cell addressable, you're not setting the same address, so it fails. I also have no idea what your mouse is doing off screen.
     
    EricMay likes this.
  10. araki_yuta

    araki_yuta

    Joined:
    Jan 21, 2020
    Posts:
    14
    May be don't use brackets on your filenames? those seems to signal subassets
     
    EricMay likes this.
  11. EricMay

    EricMay

    Joined:
    Feb 2, 2018
    Posts:
    6
    Yesss this fixed it!! No [] allowed. Thanks a lot araki_yuta!

    Also thanks ProtoTerminator once again for your help, I'd be lost without you. Appreciate it a lot!

    Looking forward to learning more about addressables now that this has been resolved.