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:
public class CustomVisualElement : VisualElement
{
public new class UxmlTraits : VisualElement.UxmlTraits
{
public override void Init(VisualElement ve,
IUxmlAttributes bag,
CreationContext cc)
{
CustomVisualElement customVisualElement = (CustomVisualElement) ve;
customVisualElement.Clear();
//PROBLEM 1: Cannot use coroutines, how to load asynchronously?
var loadUxmlOperation = Addressables.LoadAssetAsync<VisualTreeAsset>(...);
loadUxmlOperation.WaitForCompletion();
//PROBLEM 2: Is using the Addressables instantiation API possible?
//PROBLEM 3: This causes reference exceptions SOMETIMES.
customVisualElement.Add(loadUxmlOperation.Result.CloneTree().ElementAt(0));
customVisualElement.styleSheets.Add(loadUxmlOperation.
Result.stylesheets.First());
//PROBLEM 4:
//Causes error: Cannot destroy element, use DestroyImmediate instead
Addressables.Release(loadUxmlOperation);
}
}
public new class UxmlFactory : UxmlFactory<CustomVisualElement, UxmlTraits> { }
}
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?