首页 / Unity3D Unity插件两种写法

Unity插件两种写法

原创 分类: Unity3D 2024-4-20 00:23 阅读量:98
常用的插件制作方式有两种,分别是EditorGUILayout 和UIElements EditorGUILayout 不如 UIElements 强大,但对于简单的布局和控件,写起来要方便很多 个人写起来EditorGUILayout类似...

常用的插件制作方式有两种,分别是EditorGUILayout 和UIElements

EditorGUILayout 不如 UIElements 强大,但对于简单的布局和控件,写起来要方便很多

个人写起来EditorGUILayout类似于tkinter和winform
UIElements类似于前端或者wpf

DressingTools和姿势替换就是用EditorGUILayout写的
thum-f06b1741884846.png

thum-19d11741884846.png

可以看到只用EditorGUILayout也能实现相当不错的效果

对比两个程序实现的相同功能

EditorGUILayout


public class TestEditorGUILayout : EditorWindow
{
    private List<string> items = new List<string>()
    {
        "1", "2", "3"
    };

    private int languageChoice = 0;
    private string[] languageAll = { "English", "Chinese", "Japanese" };

    [MenuItem("Qychui/ListView Example")]
    public static void ShowExample()
    {
        TestEditorGUILayout wnd = GetWindow<TestEditorGUILayout>();
        wnd.titleContent = new GUIContent("ListView Example");
    }

    public void OnGUI()
    {
        // Title
        EditorGUILayout.LabelField("Avatar光剑工具", EditorStyles.boldLabel, GUILayout.ExpandWidth(true), GUILayout.Height(50));

        // Language selection
        languageChoice = EditorGUILayout.Popup("Select Language", languageChoice, languageAll);
        EditorGUILayout.Space();

        // Display the list
        for (int i = 0; i < items.Count; i++)
        {
            EditorGUILayout.BeginHorizontal();

            // First label
            EditorGUILayout.LabelField(items[i], GUILayout.Width(100));

            // Second label
            EditorGUILayout.LabelField("Additional Info " + items[i], GUILayout.Width(150));

            // Delete button
            if (GUILayout.Button("Delete", GUILayout.Width(60)))
            {
                items.RemoveAt(i);
            }
            EditorGUILayout.EndHorizontal();
        }
    }
}

效果如下
thum-79541741884102.png

UIElements


using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;

public class TestScripts : EditorWindow
{
    [MenuItem("Qychui/ListViewExampleWindow")]
    public static void OpenDemoManual()
    {
        GetWindow<TestScripts>().Show();
    }

    private ListView listView;

    public void OnEnable()
    {
        var items = new List<string>()
        {
            "Name","Text","Age"
        };

        Func<VisualElement> makeItem = () => {
            var container = new VisualElement();
            container.style.flexDirection = FlexDirection.Row;

            // Create the label
            var label = new Label();
            label.name = "itemLabel";
            label.style.flexGrow = 1;
            label.style.marginLeft = 10;
            label.style.unityTextAlign = TextAnchor.MiddleCenter;
            container.Add(label);

            var label2 = new Label();
            label2.name = "itemLabel2";
            label2.style.flexGrow = 1;
            label2.style.marginLeft = 10;
            label2.style.unityTextAlign = TextAnchor.MiddleCenter;
            container.Add(label2);

            // Create the delete button
            var button = new Button();
            button.text = "Delete";
            button.style.flexGrow = 0;
            button.style.alignSelf = Align.FlexEnd;
            button.clickable.clicked += () => {
                int index = container.parent.IndexOf(container);
                if (index >= 0 && index < items.Count)
                {
                    items.RemoveAt(index);
                    listView.Rebuild();
                    Debug.Log(items.Count.ToString());
                }
            };
            container.Add(button);

            return container;
        };

        Action<VisualElement, int> bindItem = (e, i) => {
            var label = e.Q<Label>("itemLabel");
            label.text = items[i];

            var label2 = e.Q<Label>("itemLabel2");
            label2.text = "Additional Info " + items[i];
        };

        listView = new ListView(items, 22, makeItem, bindItem);

        listView.selectionType = SelectionType.Multiple;

        listView.itemsChosen += objects => Debug.Log(objects);
        listView.selectionChanged += objects => Debug.Log(objects);

        listView.style.flexGrow = 1.0f;

        rootVisualElement.Add(listView);
    }
}

thum-881d1741884102.png

标签: .Net 工具

上一篇:2025第一季度工作日志

下一篇:下一篇