Search Unity

Question Using Addressables within a custom Visual Element

Discussion in 'UI Toolkit' started by DasOhmoff, May 19, 2021.

  1. DasOhmoff

    DasOhmoff

    Joined:
    Nov 8, 2018
    Posts:
    11
    Hello, thank you for your help.

    I was not sure where to post this question. So I posted it the Addressables-Forum-Section as well as the UIToolkit-Forum-Section, I hope this is not bad.

    I am trying to figure out how to use the Addressables within a custom UI visual element, I encountered some questions/problems that I would like to ask. Some example code:
    Code (CSharp):
    1.  
    2. public class CustomVisualElement : VisualElement
    3. {
    4.  
    5. public new class UxmlTraits : VisualElement.UxmlTraits
    6. {
    7.     public override void Init(VisualElement ve,
    8.                               IUxmlAttributes bag,
    9.                               CreationContext cc)
    10.     {
    11.         CustomVisualElement customVisualElement = (CustomVisualElement) ve;
    12.         customVisualElement.Clear();
    13.  
    14.         //PROBLEM 1: Cannot use coroutines, how to load asynchronously?
    15.         var loadUxmlOperation = Addressables.LoadAssetAsync<VisualTreeAsset>(...);
    16.         loadUxmlOperation.WaitForCompletion();
    17.  
    18.         //PROBLEM 2: Is using the Addressables instantiation API possible?
    19.  
    20.         //PROBLEM 3: This causes reference exceptions SOMETIMES.
    21.         customVisualElement.Add(loadUxmlOperation.Result.CloneTree().ElementAt(0));
    22.         customVisualElement.styleSheets.Add(loadUxmlOperation.
    23.                                             Result.stylesheets.First());
    24.  
    25.         //PROBLEM 4:
    26.         //Causes error: Cannot destroy element, use DestroyImmediate instead
    27.         Addressables.Release(loadUxmlOperation);
    28.     }
    29. }
    30.  
    31. public new class UxmlFactory : UxmlFactory<CustomVisualElement, UxmlTraits> { }
    32.  
    33. }
    34.  
    I commented some of problems that occur at the corresponding places (see in example above).

    Problem 1: Using coroutines in visual elements is not possible. So how would one load the assets asynchronously in visual elements?
    Problem 2: Is there a way to use the Addressables instantiation API with visual elements? Should this even be possible and does that make sense? What is the take on this matter?
    Problem 3: Loading through Addressables causes SOMETIMES errors to occur. Sometimes the results are perfectly fine, and sometimes they are null, how to handle that?
    Problem 4: Releasing Addressables in visual elements causes errors, how does releasing Addressables in visual elements work?

    Question: What is the very best way to handle Addressables and Visual Elements?
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    780
    Hello,

    I cannot comment about "the best way" to use Addressables with Visual Elements yet, but let me try to offer some advice (keep in mind I have not used this system directly -- I am just going through the API docs).

    To load asynchronously without co-routines, it looks like you should use the "Completed" event on the AsyncOperationHandle struct that is returned by the LoadAsync() function.
    For this reason I would recommend to perform the loading in the constructor of the VisualElement subclass itself, not the Init() method directly (this way you can have a method to use as a callback for the completion event).

    You generally cannot have references to Visual Elements directly, only to VisualTreeAsset objects. So I think that rules out any use of the instantiation APIs from Addressables.

    I would leave that question to the Addressables team.

    That issue doesn't look specific to VisualElements. FWIW I am not finding references to a method called "Addressables.Release(loadUxmlOperation);" in the Addressables packages Scripting reference.

    Would you mind posting the link to the other forum post ? Thanks
     
    LarsLundh likes this.
  3. DasOhmoff

    DasOhmoff

    Joined:
    Nov 8, 2018
    Posts:
    11
    Thank you for your support.

    Ah yes, that seems to be the better option. I was testing this previously, but I got confused because this event used to return null in the operation handle as result of the loading, so I thought the asynchronous loading was not working for some reason, but in fact this is problem 3 that I mentioned before (loading works sometimes, and does not work at other times, for example when existing play mode).

    Yes, this problem is the most tricky one, and my biggest concern of these 4.

    What can be done about this? Releasing uxml assets causes errors, such as: "Cannot destroy element, use DestroyImmediate instead". Is there any plan to fix this? What should I do now?

    Here is the link to the other forum post.