当前位置:网站首页>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
- Introduction to redis I: redis practical reading notes
- 想画一张版权属于你的图吗?AI作画,你也可以
- Sorting out problems in interface endpoint testing practice using Supertest
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- EasyNLP中文文图生成模型带你秒变艺术家
- 远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
- Understanding of C # delegate
- 到底为什么不建议使用SELECT * ?
- [1331. Array serial number conversion]
猜你喜欢

JS page black and white background switch JS special effect

EasyNLP中文文图生成模型带你秒变艺术家

PXE_ KS unattended system

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

Introduction to redis II: RedHat 6.5 installation and use

【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例

How do we do full link grayscale on the database?

Three deletion strategies and eviction algorithm of redis

到底为什么不建议使用SELECT * ?

"When you are no longer a programmer, many things will get out of control" -- talk to SUSE CTO, the world's largest independent open source company
随机推荐
Sorting out problems in interface endpoint testing practice using Supertest
Pl515 SOT23-5 single / Dual Port USB charging protocol port controller Parkson electronic agent
js可拖拽alert弹窗插件
有奖征文 | 2022 云原生编程挑战赛征稿活动开启
What is data center? What value does the data center bring_ Light spot technology
H5 wechat shooting game source code
Explain in detail the rays and radiographic testing in unity
Yyds dry inventory interview must brush top101: every k nodes in the linked list are turned over
阿里云 MSE 支持 Go 语言流量防护
7/27 training log (bit operation + suffix array)
UE4 3dui widget translucent rendering blur and ghosting problems
Mongoose condition queries the data of a certain time period
How can enterprises successfully complete cloud migration?
第六七八次作业
Unity object path query tool
【服务器数据恢复】HP StorageWorks系列存储RAID5两块盘故障离线的数据恢复案例
远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作
Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
瀚高数据库最佳实践配置工具HG_BP日志采集内容
Integrating database Ecology: using eventbridge to build CDC applications