Class VBuilder
- Namespace
- Velvet.Experimental
- Assembly
- Velvet.Docs.dll
EXPERIMENTAL — PoC. Object/collection-initializer authoring surface that lets a
V.* tree be written in a declarative nested-brace form while keeping full IDE completion
and requiring no IDE plugin.
This is a thin ergonomic layer: a builder never reaches the Reconciler. Each builder is converted to a
plain VNode via Build() (invoked by the implicit VBuilder -> VNode
conversion), delegating to the existing V.* factories so all parsing / pooling semantics are reused.
Why initializers (C# 6/7) and not collection expressions (C# 12): Unity 6.3 defaults to C# 9 and does not
officially support newer language versions; collection expressions [...] would force every consumer
of the redistributed UPM package to swap their Roslyn compiler. Object/collection initializers compile on
stock Unity, so this layer needs no toolchain change.
VNode Render()
{
var (count, setCount) = UseState(0);
return new VDiv("flex flex-col items-center gap-4 p-4")
{
new VLabel("text-2xl font-bold") { Text = $"Count: {count}" },
new VButton("px-4 py-2 rounded bg-primary text-white")
{
Text = "Increment",
OnClick = () => setCount(count + 1),
},
};
}
public abstract class VBuilder
- Inheritance
-
objectVBuilder
- Derived
Constructors
Properties
- Class
Utility class string (space-separated), equivalent to the
classNamefactory argument.
- Key
Reconciler key, equivalent to the
keyfactory argument.
- Name
UnityEngine.UIElements.VisualElement.name, equivalent to the
namefactory argument.
Methods
- Add(VNode?)
Collection-initializer hook. Accepts any VNode (e.g. the result of a
V.*factory orV.When(...)) and, through the implicitVBuilder -> VNodeconversion, any nested builder. Null children are skipped, matching theV.When(false, ...)convention. The scratch accumulator list is rented from a pool so repeated authoring does not churn list allocations.
- BuildChildren()
Materializes the collected children into the VNode[] shape the
V.*factories expect, then returns the scratch accumulator list to the pool. The produced array is rented from Velvet.VNodePool, so the reconciler reclaims it on the next diff exactly as it does forV.Listoutput — keeping the builder's only irreducible per-build cost the builder object itself.
Operators
- implicit operator VNode?(VBuilder?)
Implicit conversion that lets a builder be used anywhere a VNode is expected — as a child passed to Add(VNode?), or as the return value of a component
Rendermethod.