Is there any default state management? Is there one in plans? Like similar to Flutter - Provider or some kind of dependency injection like in ASP.NET Core 3? Is Zenject an option, will it work in UIToolkit?
Imagine this scenario - you have custom EditorWindow, you are deep in children VisualElements. root -> VisualElement -> Container -> Column -> Tile -> Button
. So you are in Button visual element, how do you access the data that is stored in ScriptableObject related to this EditorWindow instance in, for example, Tile custom visual element? As of right now, is there a common approach to this problem?
Singleton is not an option because the data type could be the same for same type of window.
Dependency injection per window could be a solution.
If we had some kind of context
passed to every child down the tree then we would be able to get required instance from there as a solution.
Sorry for so many questions. It’s just somewhat confusing at this point and it’s hard to google any information like that. I may have made some mistakes in my questions, if you could correct me that would be great.
Thank you for any answer.
1 Like
VisualElement has the userData property which you can use to store whatever context you might need. From the child, deep in the hierarchy, you can walk up the visualTree until parent.userData is assigned (using VisualElement.FindAncestorUserData()). Since userData is a plain object reference, you can always store a Dictionary if you need to store more than one property.
I never tried ZenJect, but their documentation states that it works with c# classes and not just MonoBehaviors/ScriptableObjects. Let me know if it works for you.
Thank you, I will tinker around with userData. Having it eases the process of creating something like that. A couple of wrappers and it’s good to go. Maybe, little unnecessary overhead but it should be way too small to be an issue.
Can I be sure that nothing is assigned to userData in all default components and things like GraphView? Is it only for custom(user) assignment? Or is it used somewhere internally? I am afraid I might overwrite some important data for some VisualElement and its descendants.
userData should be safe for you to write into. It’s only used in some fields sub elements internally, not on plain VisualElements.I don’t see any usage in plain GraphView, but I can’t vouch for usage in ShaderGraph or VFXGraph so YMMV…
Also, the FindAncestorUserData() helper already performs the walk up I described in my previous answer.
1 Like