[FEATURE REQUEST] Template Code Gen
Closed this issue · 2 comments
It would be awesome if the code generation was able to understand templates.
At the moment, it just puts it as a TemplateContainer, but i propose it does 2 things:
- Creates the template container field with a
Template
suffix - Creates a typed field without said suffix and initializes it in the assign.
For example, it would go from:
partial class ProvinceEditor
{
private Label title;
private TemplateContainer areaDropdown;
protected void AssignQueryResults(VisualElement root)
{
title = root.Q<Label>("Title");
areaDropdown = root.Q<TemplateContainer>("AreaDropdown");
}
}
To:
partial class ProvinceEditor
{
private Label title;
private TemplateContainer areaDropdownTemplate;
private AreaDropdown areaDropdown;
protected void AssignQueryResults(VisualElement root)
{
title = root.Q<Label>("Title");
areaDropdownTemplate = root.Q<TemplateContainer>("AreaDropdown");
areaDropdown = new AreaDropdown(areaDropdownTemplate);
}
}
Not entirely sure if the template is even required to be exposed outside of the AssignQueryResults function.
Hi @Lachee
Do you have any ideas on how would that feature work exactly?
Some things to consider with this.
- Templates are not bound to any C# type.
- Type from .gen.cs can be used.
- To create a new instance of a type in the
AssignQueryResults
this type needs a parameterless constructor. This would require modifying the interface of all generated scripts, or... - Maybe this system should create a field with more specific type only if this type has a parameterless constructor.
- In this case the code-gen scripts would not compile if later users decides to remove parameterless constructor. Should this be something to worry about?
- Type from .gen.cs also requires a
AssignQueryResults
to run for its initialisation, so a constructor withVisualElement
parameter is probably more appropriate than a parameterless one.
Example logic:
If we need to create a field for a TemplateContainer
- See if its UXML has a reference to .gen.cs file
- See if a type from this file has a
Type(VisualElement root)
constructor - if it does, use this type instead of
TemplateContainer
- See if a type from this file has a
I would also like to see a project where this feature is actually useful. In my experience all of the .gen.cs types I used required initialisation. For this reason I most often used constructors with more parameters.
No further input- closing