当前位置:网站首页>Unity foundation 2 editor expansion
Unity foundation 2 editor expansion
2022-07-28 20:55:00 【W.C.Zeng】
UI Toolkit Editor Extension
Official document
Use UI Toolkit Can be in unity Customize some UI Tools , It can be run in runtime or editor state . for example , Farm management games can write an item editor ItemEditor, Card games can write card editors .
There are also cases on the official website .
Item Editor
For farm experience projects , Write an article editor ( Wheat field story course ) , It's convenient for us to operate CropDataList database (scriptableobject), increase 、 Delete 、 Change item data .
Editor Window Create editor window 
UI Document Templates that can be used to create items 
C#
This file is the main script of the item editor
UXML
The file is similar to HTML equally , Describe the content of the item editor
double-click , By default UI Builder Open the edit window 
USS
The file is similar to CSS , Describe the style of the item editor
Basic operation steps :
Library window Contains various controls , Usually use first VisualElement Make containers , Put some more inside Lable Button Other controls
Hierarchy window Contains the hierarchical information of each control
Viewport window Preview the contents of the current item editor
create a window
《ItemEditor.cs》
public class ItemEditor : EditorWindow
{
// Show item editor options in the menu bar
// [MenuItem("Window/UI Toolkit/ItemEditor")]
[MenuItem("My Tools/ItemEditor")]
public static void ShowExample()
{
ItemEditor wnd = GetWindow<ItemEditor>();
wnd.titleContent = new GUIContent("ItemEditor");
}
}
load uxml
public void CreateGUI()
{
// Each editor window contains a root VisualElement object
VisualElement root = rootVisualElement;
// Import UXML
var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/UIBuilder/ItemEditor.uxml");
VisualElement labelFromUXML = visualTree.Instantiate();
root.Add(labelFromUXML);
}
Data binding
Make sure the Hierarchy The hierarchical order of each component in the window , Find control
Query control , from root Nodes by type ListView And name ItemList , Find for control
// Get the left column we drew in the item editor listview
private ListView listView;
// The right detail bar of the item editor
private ScrollView itemDetailsSection;
...
// Find the left column in the item editor
listView = root.Q<VisualElement>("ItemList").Q<ListView>("ListViewer");
itemDetailsSection = root.Q<ScrollView>("ItemDetails");
Bind a callback , When editing item data , Automatically write data back to the database
For the writing of callback, please refer to the official example 
itemNameField.RegisterCallback<ChangeEvent<string>>(
evt =>
{
activeItem.itemName = evt.newValue;
// Refresh the name of the left item column after changing the name
listView.Rebuild();
}
);
Load data
Load item data
// Load item database
private void LoadDatabase()
{
// What we found was GUID character string
var dataArray = AssetDatabase.FindAssets("t:ItemListSO");
// Other versions use strings ItemListSO It's OK
if (dataArray.Length > 0)
{
dataArray[0] = AssetDatabase.GUIDToAssetPath(dataArray[0]);
database = AssetDatabase.LoadAssetAtPath<ItemListSO>(dataArray[0]);
}
itemList = database.itemDetailsList;
// To modify file data , The object must be marked
EditorUtility.SetDirty(database);
// load cropData Do not want to be in editor window Modify the contents in , Read only
dataArray = AssetDatabase.FindAssets("t:CropDataList_SO");
if (dataArray.Length > 0)
{
dataArray[0] = AssetDatabase.GUIDToAssetPath(dataArray[0]);
cropDatabase = AssetDatabase.LoadAssetAtPath<CropDataList_SO>(dataArray[0]);
}
cropDetailList = cropDatabase.cropDetailsList;
}
Generate items
For example , Generate a list of items on the left
private void GenerateListView()
{
Func<VisualElement> makeItem = () => itemTemplate.CloneTree();
Action<VisualElement, int> bindItem = (e, i) =>
{
if (i < itemList.Count)
{
if (itemList[i].itemIcon != null)
{
e.Q<VisualElement>("ItemIcon").style.backgroundImage = itemList[i].itemIcon.texture;
}
e.Q<Label>("ItemName").text = itemList[i].itemName == null ? "No Item" : itemList[i].itemName;
}
};
listView.fixedItemHeight = 56;
listView.itemsSource = itemList;
listView.makeItem = makeItem;
listView.bindItem = bindItem;
listView.onSelectionChange += OnListSectionChange;
// When the left item is not selected , The right detail bar is not displayed
itemDetailsSection.visible = false;
}
If you select an item on the left , Automatically generate right item details
private void OnListSectionChange(IEnumerable<object> obj)
{
// Parameters passed when the option is selected obj Indicates one or more selected objects So take the first object to represent the currently selected object
activeItem = obj.First() as ItemDetails;
// After marking, you can perform editing 、 Undo, etc
itemDetailsSection.MarkDirtyRepaint();
GetItemDetails();
itemDetailsSection.visible = true;
}
边栏推荐
- Unity performance optimization scheme arrangement
- Want to draw a picture that belongs to you? AI painting, you can also
- Unity gets which button (toggle) is selected under togglegroup
- Oracle library access is slow. Why?
- Introduction to redis I: redis practical reading notes
- Record an error in runtime. Getruntime().Exec ("command")
- LVM logical volume
- 到底为什么不建议使用SELECT * ?
- Space shooting Lesson 15: props
- Algorithm interview high frequency problem solving guide [1]
猜你喜欢

PL515 SOT23-5 单/双口 USB 充电协议端口控制器 百盛电子代理商

融合数据库生态:利用 EventBridge 构建 CDC 应用

Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~

Integrating database Ecology: using eventbridge to build CDC applications

Prometheus complete process of configuring alertmanager

阿里云 MSE 支持 Go 语言流量防护

Read the recent trends of okaleido tiger and tap the value and potential behind it

Subcontracting loading of wechat applet

What is data center? What value does the data center bring_ Light spot technology

Three steps to teach you unity serial communication
随机推荐
Network shell
Want to draw a picture that belongs to you? AI painting, you can also
Cartoon JS shooting game source code
Explain the camera in unity and its application
Dynamic planning: code summary of knapsack problem template
既要便捷、安全+智能,也要颜值,萤石发布北斗星人脸锁DL30F和极光人脸视频锁Y3000FV
卡通js射击小游戏源码
动态规划:背包问题模板代码汇总
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
LVS+KeepAlived高可用部署实战应用
Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
Two written interview questions about regular
什么是“安全感”?沃尔沃用它自己独特的理解以及行动来告诉你
一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力
Space shooting Lesson 10: score (painting and writing)
Redis review summary
What is data center? What value does the data center bring_ Light spot technology
【ADB常用命令及其用法大全(来自[醒不了的星期八]的全面总结)】
Prize essay solicitation | 2022 cloud native programming challenge draft activity opens
Space shooting lesson 14: player life