当前位置:网站首页>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;
}
边栏推荐
- Redis review summary
- Seventeen year operation and maintenance veterans, ten thousand words long, speak through the code of excellent maintenance and low cost~
- Job CE
- Learn about the native application management platform of rainbow cloud
- 3D激光SLAM:LeGO-LOAM论文解读---简介部分
- Explain in detail the rays and radiographic testing in unity
- Laser slam:logo-loam --- code compilation, installation and gazebo test
- Two written interview questions about regular
- LVS+KeepAlived高可用部署实战应用
- Understanding of C # delegate
猜你喜欢

研发效能的思考总结

Talking about canvas and three rendering modes in unity

Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key

远光软件获得阿里云产品生态集成认证,携手阿里云共建新合作

What is "security"? Volvo tells you with its unique understanding and action

Lvs+keepalived high availability deployment practical application

How bad can a programmer be? Nima, they are all talents

想画一张版权属于你的图吗?AI作画,你也可以

漂亮的蓝色背景表单输入框样式

System. ArgumentException: Object of type ‘System. Int64‘ cannot be converted to type ‘System.Int32‘
随机推荐
如何用Redis实现事物以及锁?
Unity uses shader to quickly make a circular mask
微信小程序的分包加载
3D激光SLAM:LeGO-LOAM论文解读---简介部分
Read the recent trends of okaleido tiger and tap the value and potential behind it
The engineering practice of super large model was polished, and Baidu AI Cloud released the cloud native AI 2.0 solution
[complete collection of common ADB commands and their usage (from a comprehensive summary of [wake up on Sunday)]
C reads the data in the CSV file and displays it after importing the DataTable
SQL audit tool self introduction owls
The 678th operation
融合数据库生态:利用 EventBridge 构建 CDC 应用
prometheus配置alertmanager完整过程
Unity makes prefabricated bodies with one key and modifies prefabricated bodies with one key
太空射击第10课: Score (繪畫和文字)
Space shooting Lesson 11: sound and music
有奖征文 | 2022 云原生编程挑战赛征稿活动开启
Network layer performance test
融合数据库生态:利用 EventBridge 构建 CDC 应用
Unity gets which button (toggle) is selected under togglegroup
flask 静态文件服务搭建