Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Some very basic questions..

Discussion in 'Addressables' started by Quatum1000, Aug 28, 2018.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    [2010.2.5f1]

    Hi,
    At first, the addressable stuff is a great solution to prevent using the assetbundles directly. I hope this will be continued.

    I'd like to use addressables instead of collect from the resource folder. I followed all the tasks the documentation provided. I wanted to load a single asset from the local group.

    1) Dragged the Resource folder into DefaultLocalGroup.
    1.jpg
    2)
    02.jpg

    And using this simple script to read from, but I miss the "Object Key" ?
    I tried several key namings. Noway to load any of the assets.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AddressableAssets;
    3.  
    4. public class TEST_AdressablesLoadAsset : MonoBehaviour {
    5.     public Material LampDiskHalo = null;
    6.  
    7.     void Start () {
    8.  
    9.         var mr = this.GetComponent<MeshRenderer>();
    10.  
    11.         //LampDiskHalo = Resources.Load("MatTextures/LampDiskHalo", typeof(Material)) as Material;
    12.    
    13.         LampDiskHalo = Addressables.LoadAsset<Material>("ManagerLightsDisk/Rescouces/MatTextures/LampDiskHalo") as Material;
    14.  
    15.         if (LampDiskHalo) mr.sharedMaterial = LampDiskHalo;
    16.         else Debug.Log("LampDiskHalo not loaded = null");
    17.  
    18.        // Also not
    19.  
    20.        var operation = Addressables.LoadAsset<Material>("ManagerLightsDisk/Resources        /MatTextures/LampDiskHalo");
    21.        operation.Completed += (op) =>
    22.        {
    23.            LampDiskHalo = op.Result;
    24.        };
    25.        if (LampDiskHalo) mr.sharedMaterial = LampDiskHalo;
    26.  
    27.     }
    28. }
    29.  
    EDIT
    This happen when loading the demo.
    10.jpg

    Thank you.
     
    Last edited: Aug 29, 2018
  2. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091
    LoadAsset is not a synchronous call but asynchronous. Meaning you should set the loaded material after you've loaded the asset. In your case you're not waiting for it to be completed before you're setting the material.

    Fair warning: I have not tested the code for validity
    Code (CSharp):
    1. // Your load operation which is Asynchronous (Loads over multiple frames)
    2. var operation = Addressables.LoadAsset<Material>("ManagerLightsDisk/Resources/MatTextures/LampDiskHalo");
    3. // A subscription on the Completed event (Executed after the asset is loaded)
    4. operation.Completed += (op) =>
    5. {
    6.     // Checking whether the asset has been loaded successfully
    7.     if (op.Result != null)
    8.     {
    9.         // Setting the material
    10.         mr.sharedMaterial = op.Result;
    11.     }
    12. };
    Also the demo is old, it doesn't work with the current version of the addressable asset system
     
    Quatum1000 likes this.
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Thank you.. But cannot get it to work. :-/

    * Does your example should work without a co-routine?

    * And is the naming convention correct based on my example?
    ("ManagerLightsDisk/Resources/MatTextures/LampDiskHalo")
     
  4. MaskedMouse

    MaskedMouse

    Joined:
    Jul 8, 2014
    Posts:
    1,091
    Yes it works without coroutine that is what the Completed event is for. You’re subscribing a method to the event which is executed after the asset is loaded from the bundle.

    You’re loading an asset by it’s label name, make sure the label is right as it is error prone.
    You could alternatively extend the AssetReference class so it is of type material. Then just drag drop the addressable asset in there as serialized reference.

    Have a look at this example.
    It is using no additional coroutine and loads the material async. If it is loaded successfully it sets the material if not it'll show an error.
    You can drag drop the material into the MaterialReference slot in the inspector as long as it is an addressable asset.

    Code (CSharp):
    1. public class MaterialLoadClass : MonoBehaviour
    2. {
    3.     public AssetReferenceMaterial MaterialReference;
    4.  
    5.     public void Start()
    6.     {
    7.         var loadOperation = MaterialReference.LoadAsset();
    8.         loadOperation.Completed += LoadOperationOnCompleted;
    9.     }
    10.  
    11.     private void LoadOperationOnCompleted(IAsyncOperation<Material> obj)
    12.     {
    13.         if (obj.Result == null)
    14.         {
    15.             Debug.LogError("Couldn't load material from bundle");
    16.             return;
    17.         }
    18.  
    19.         var rendererComponent = gameObject.GetComponent<Renderer>();
    20.         rendererComponent.sharedMaterial = obj.Result;
    21.     }
    22. }
    23.  
    24. [Serializable]
    25. public class AssetReferenceMaterial : AssetReferenceT<Material> { }
     
    Last edited: Aug 29, 2018
  5. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    I think @MaskedMouse has largely answered the question asked (thanks!!) but I do want to add one more bit of info.

    It's a bug in our system that you were able to put the Resources directory into a group. That will actually cause build issues because the content in Resources is pulled into the player build automatically AND we're trying to put it into bundles. If you had put the folder MatTextures into addressables, we would have popped up with a message saying basically "we need to move this out of Resources. We'll move it to a new folder called Resources_moved. You cool with that?" (roughly). We'd then move that content into a new folder, and it wouldn't try to be in the player build. The bug is that you marked the actual Resources directory as addressable. Sounds like we missed that case. I've opened a bug to make sure we prevent that (or suggest auto-renaming it "Resources_moved").

    -Bill
     
    Quatum1000 likes this.
  6. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Yes.
    You should check for other system folders as well, "plugins", "editor", and "c#" is also a secret system folder.

    But a small thing is not clear with the direct naming an asset. If I want load a single asset that was with several items in a folder (that was moved into eg. Resources_moved. All the files are bundled)
    eg.

    View attachment 294133

    A) Can I load a single asset directly by name (not by label)? Addressables.LoadAsset<Material>("ManagerLightsDisk/Resources_moved/MatTextures/LampDiskHalo"); Or can single assets only loaded by the labels? My question was never answered.

    B) And if I load an asset by a single label or name. Does the addressable system load the single asset out of the bundle, or will the bundle completely into memory as a collection?

    Thank you!
     
    Last edited: Sep 2, 2018
  7. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    A) you have to load by address, but by default, the address is the path. If you move things from resources, then the default address is what the resources loading path had been, which is path from Resources to extension. (e.g. "Assets/Resources/thing/x.png" would have been "thing/x", so it remains so by default once moved)
    B) if 100 things are in a bundle and you load one of them, only that one is loaded. The complication of bundles arises when you unload. If you had loaded 10 things from that bundle, then released 9, all will still be in memory. Releasing the last asset will unload the bundle.

    Does that answer your questions?
     
    Quatum1000 likes this.
  8. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Thank you.. not completely.

    * When load a single material, does the addressable system will load all textures / shader automatically if they are referenced inside of the same bundle? Not sure what happen if they already loaded into the system.
    * Bug - On click "Export Entries" and select [Cancel] the asset is marked exported, but it's not.
    * What is the use-case of simplify entries name?
    * Right click does not open the drop down menu until the mouse is moved.
    * Would it possible to load any existing AddressableEntryCollection.asset into the Addressables editor, and work with as usual?
    * Did you have thought about a machine protection and encryption, so after download a payed AddressableEntryCollection.asset it cannot be shadred/used on another machine?
     
  9. unity_bill

    unity_bill

    Joined:
    Apr 11, 2017
    Posts:
    1,053
    ok, I'll try to respond to each of those in...

    1. Unity's internal loading deals with loading dependencies. Our system figures out where the dependencies are. So if you load a material that depends on the textures, our system knows where the material is (where the bundle is) and where the textures are (same bundle or others). We load all the needed bundles, then the material, and Unity deals with loading the textures, etc.
    2. Thanks, we'll note this in our bug tracker.
    3. It just strips the file path. Purely a convenience thing.
    4. noted as well. Not sure if this is our fault or something internal to the Unity engine, but we'll look into it.
    5 & 6. the entry collection is not for distribution of assets to a player. Those are for saving a setup of what's addressable. This would be used by other packages (which currently means internal to Unity) or used when you want to save a partial address layout across projects. This feature is more in place as preparation for a potential future where more and more packages exist, and they might want to mark some of their own stuff as addressable automatically.

    -Bill
     
    Quatum1000 likes this.
  10. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Thanks for the info.