用于创建内置 UI 控件的默认实现的 Utility 类。
通过 UI 系统使用 UI 控件的建议工作流程是为每种类型的控件都创建预制件,并且在需要时实例化这些预制件。这样,对预制件的更改将对所有使用的实例立即生效。
但是在某些情况下,可能需要完全通过代码创建 UI 控件。DefaultControls 类提供用于创建每个内置 UI 控件的方法。生成的对象与使用编辑器 GameObject 菜单中对应 UI 菜单条目获取的对象相同。
这种方法的一种示例用法是为模仿 Unity 内置 UI 控件的自定义新 UI 控件创建菜单项。某些此类 UI 控件可能包含其他 UI 控件。例如,一个滚动视图包含滚动条。通过使用 DefaultControls 方法创建这些部件,可确保它们在外观和设置方面与 Unity 内置菜单项提供的部件相同。
请注意,此类中的方法所创建的 UI 控件的设置细节可能会随 UI 系统后续修订而变化。因此,它们不保证 100% 向后兼容。建议不要依赖于这些方法所创建的 GameObject 的特定层级视图,将代码限制为仅与每个方法所创建的根 GameObject 形成接口。
CreateButton | 创建按钮。 |
CreateDropdown | 创建下拉列表。 |
CreateImage | 创建图像。 |
CreateInputField | 创建输入字段。 |
CreatePanel | 创建面板。 |
CreateRawImage | 创建原始图像。 |
CreateScrollbar | 创建滚动条。 |
CreateScrollView | 创建滚动视图。 |
CreateSlider | 创建滑动条。 |
CreateText | 创建文本对象。 |
CreateToggle | 创建开关。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.