Cache Visual Elements

Making a tool for procedurally generating UI with the UI Toolkit and would like to cache my generated Visual Elements to save me from having to generate new elements when I just want the previously generated element. Only problem is that the Visual Elements never survive a domain reload (and sometimes just saving), so they are just wiped everytime I re-enter the editor from code changes. Any way to make caching of Visual Elements work?

Are you developing a custom editor script? If you are, I don’t know about the specifics of what you might mean by “caching” visual elements, but my previous experience doing custom editors for level builder, it sounds like a serialization problem.

That is you need some way of saving the data that constitutes your visual elements in a serialized form, and then reconstituting that data when you access your editor.

I was having a problem with a simple array not remembering values between compiles, so I think your problem is much more complex.

Not knowing enough about what you are trying to do, if looking further into the serialization issues of “caching” visual elements doesn’t help you then off the top of my head perhaps Scriptableobjects would be an approach to take because they are remembered.

That or look at the PrefabUtility class, which you can access from an editor script. I think you’d be able to create a prefab out of the Visual elements you want to save.

The problem has to do with Visual Elements not surviving a domain reload, as seen in one of my previous threads: ( https://discussions.unity.com/t/811081 ). No matter how much I serialize it, it is erased when a domain reload happens, and I have to generate it again.

That’s just the way VisualElement works. It has to be created e.g. in OnEnable either via code or by loading and cloning from the UXML/VisualTreeAsset. In my experience, this is pretty fast and probably optimized enough to just do for anything.

There are a few forum posts that ask for the ability to instantiate/clone VisualElement via C# scripting without having to use UXML, but it was made clear by the UI Toolkit developers that VisualElement stores a lot of runtime state that is not meant to be saved and potentially difficult to decide which parts of the state should be duplicates in different scenarios. This all sounds like it won’t be possible to cache/clone/reuse VisualElement other than loading and recreating everything from UXML. But again, with everything I’ve attempted so far, this was working fine.

The only real use-case I saw for wanting to clone a VisualElement, was when building a ListView in the UI Builder where I wanted to quickly add a single item row as preview and then would have liked to use the row as a template I could clone and cache via C# at runtime, but I had to use a separate UXML file for the row template instead.

Thanks for the detailed explanation. Would be cool if I could just save the style instructions of the element + its hierarchy, but regeneration works too.