Search Unity

Question Using Addressables within a custom Visual Element

Discussion in 'Addressables' 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 (for example when exiting play mode in the editor), 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?
     
    Last edited: May 22, 2021
  2. DasOhmoff

    DasOhmoff

    Joined:
    Nov 8, 2018
    Posts:
    11
    By now I was told in the other post that for asynchronously loading, the "Completed" event on the AsyncOperationHandle can be used. Also I was told that loading in the constructor of the visual element instead of the Init() method would be a better idea.

    I was told that problem 2 most likely is not resolve able (which is to use the instantiation api of the addressables).

    Problem 3 and 4 are still there. I added to the problem 3 that the errors occur for example when exiting play mode in the editor.

    Here is the link to the other forum.