How TEngine's UI Code Generation Tool Works: Automated C# Binding from Unity Hierarchies
TEngine's UI code generation tool is a Unity Editor utility that automatically creates type-safe C# binding code from GameObject hierarchies by traversing the selected transform tree, matching naming conventions against configurable rules, and outputting field declarations, component lookups, and event listeners to the clipboard.
The TEngine framework provides a sophisticated editor-only code generation system that eliminates manual UI wiring in Unity projects. This tool converts naming-convention-driven UI hierarchies into clean, typed, event-ready C# classes, with optional UniTask async support.
Entry Points and Menu Integration
MenuItem Commands
The primary interface for TEngine's UI code generation tool is exposed through several MenuItem entries under GameObject → ScriptGenerator. Available options include UIProperty, UIPropertyAndListener, and their UniTask variants. When a menu item is clicked, Unity calls the static Generate method defined in UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L12-L27】.
Inspector Integration
Beyond the menu system, the UIComponentInspectorEditor class provides a "Generate UI Script" button directly within Unity's Inspector panel for UI components. This button calls ScriptGenerator.GenerateCSharpScript to automate the same workflow without navigating the menu bar【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/UIComponentInspectorEditor.cs#L157-L176】.
Configuration and Naming Conventions
ScriptGeneratorSetting Asset
All generation rules are centralized in the ScriptGeneratorSetting asset located at UnityProject/Assets/Editor/UIScriptGenerator/ScriptGeneratorSetting.cs. This singleton configuration stores:
- Namespace definitions for generated classes
- Code style preferences (e.g.,
_orm_prefixes for private fields) - Nullable reference types enablement
- Output path configurations
- Bind component version settings
The generator queries ScriptGeneratorSetting.Instance throughout the process to determine field naming, accessibility modifiers, and whether to generate widget-specific code【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGeneratorSetting.cs#L9-L30】.
Regex-Based Component Mapping
The ScriptGenerateRuler class (defined in ScriptGenerateRuler.cs) stores the data structure for generation rules. Each rule maps a regular expression prefix (e.g., _btn, m_img) to a specific Unity component type (Button, Image, Text, etc.) and specifies whether the generated field should be treated as a UI widget【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerateRuler.cs】.
The Generation Pipeline
Hierarchy Traversal with Ergodic
The Generate method initiates the process by capturing Selection.activeTransform (the currently selected GameObject in the Unity hierarchy). It then recursively traverses the entire subtree using the Ergodic method【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L36-L50】.
For each child Transform encountered during this depth-first traversal, the system calls WriteScript to process that specific GameObject.
Component Matching and Rule Lookup
Within WriteScript, the generator examines the child's GameObject name against the configured script generation rules via ScriptGeneratorSetting.GetScriptGenerateRule(). When a name matches a rule's regex pattern (e.g., a GameObject named _btnStart matching the _btn prefix), the system identifies the associated component type【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L212-L221】.
Code String Construction
The generator constructs the final C# code through several string builders:
-
strVar– Field declarations such asprivate Button _btnStart;orprivate Button m_btnStart;depending on the selected code style【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L258-L260】. -
strBind– Runtime binding statements usingFindChildComponent<Button>("Panel/StartBtn")to assign hierarchy references to the declared fields【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L272-L279】. -
strOnCreateandstrCallback– Event listener wiring for interactive components (Button,Toggle,Slider). The generator addsonClick.AddListenercalls and creates empty callback method stubs. When UniTask support is enabled, these become async methods returningUniTaskVoid【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L283-L306】.
When UIPropertyAndListener mode is active, the generator wraps these components in a full class scaffold—either inheriting from UIWindow or UIWidget based on the selected root object's naming convention—using the namespace defined in settings【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L84-L100】.
Output and Clipboard Integration
Rather than writing directly to the file system, TEngine's UI code generation tool places the complete script onto the system clipboard using Unity's TextEditor class with the Copy() method. The console logs a message prompting the user to paste the generated code into a new C# file【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L27-L33】【https://github.com/alex-rachel/tengine/blob/main/UnityProject/Assets/Editor/UIScriptGenerator/ScriptGenerator.cs#L31-L34】.
This clipboard-based approach allows developers to review the generated code before committing it to the project, preventing accidental overwrites of existing files while maintaining full control over file naming and placement.
Summary
-
TEngine's UI code generation tool is an editor-only utility that transforms Unity GameObject hierarchies into type-safe C# binding code.
-
Entry points include
MenuItemcommands under GameObject → ScriptGenerator and an inspector button inUIComponentInspectorEditor. -
Configuration is centralized in
ScriptGeneratorSetting, controlling namespaces, naming conventions (_vsm_prefixes), and nullable reference types. -
Traversal uses recursive
Ergodicmethods to process the selected transform hierarchy, matching GameObject names against regex rules defined inScriptGenerateRuler. -
Output generates field declarations,
FindChildComponentbinding statements, and event listener wiring (with optional UniTask async support), placing the final code on the system clipboard.
Frequently Asked Questions
How do I access TEngine's UI code generation tool in the Unity Editor?
You can access the tool through two primary methods. First, select a UI GameObject in the hierarchy, then navigate to GameObject → ScriptGenerator and choose either UIProperty (fields only) or UIPropertyAndListener (fields plus event callbacks). Alternatively, use the custom inspector button available in UIComponentInspectorEditor when inspecting a UI component, which calls ScriptGenerator.GenerateCSharpScript directly.
What naming conventions does the generator recognize?
The generator uses regex-based rules stored in the ScriptGeneratorSetting asset. Each rule in ScriptGenerateRuler maps a prefix pattern (such as _btn, m_img, or _txt) to a specific Unity component type like Button, Image, or Text. When traversing the hierarchy, the WriteScript method checks each GameObject's name against GetScriptGenerateRule() to determine the appropriate component type and field naming style.
Can I customize the output code style and namespace?
Yes, all generation preferences are configurable through the ScriptGeneratorSetting singleton asset. You can specify the target namespace, choose between underscore (_) or Hungarian (m_) prefixes for private fields, enable nullable reference types, and define whether to generate bind-component versions. These settings are queried throughout the generation process in ScriptGenerator.cs to construct the final C# syntax.
Does the tool support async/await patterns?
Yes, TEngine's UI code generation tool includes optional UniTask support. When you select the UniTask variants from the menu (such as UIPropertyAndListener - UniTask), the generator produces async callback methods returning UniTaskVoid instead of void methods. The strOnCreate and strCallback string builders in ScriptGenerator.cs handle the different listener wiring logic for async event handlers when the UniTask flag is enabled in the generation settings.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →