Unity 编辑器UI 杂记

 

用 rootVisualElement 方法绘制按钮和用 GUILayout.Button 绘制按钮混用的案例

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

public class MyTestPanel : EditorWindow
{
    [MenuItem("工具/测试面板2")]
    private static void Open() {
        var window = EditorWindow.GetWindow<MyTestPanel>("MyTestPanel");
        window.Show();
        window.minSize = new Vector2(700, 700);
        window.autoRepaintOnSceneChange = true;
    }
    
    private int FunctionIndex;
    private static readonly string[] functionTitles = new string[3] {
        "",
        "小树",
        "大树"
    };
    private VisualElement root;
    private Button myButton;
    private VisualElement border;
    private GameObject prefabToDisplay {
        get {
            return Resources.Load<GameObject>("Cube");
        }
    }

    private List<Button> btnList = new List<Button>();
    private Button currentButton;
    
    private void OnEnable()
    {
        // 创建根VisualElement
        root = rootVisualElement;

        // 创建一个容器,用于包含按钮
        VisualElement buttonContainer = new VisualElement();
        // 添加上面的间距
        buttonContainer.style.marginTop = 30; 
        root.Add(buttonContainer);
        btnList.Clear();
        // 创建按钮网格
        for (int row = 0; row < 3; row++)
        {
            VisualElement rowContainer = new VisualElement();
            rowContainer.style.flexDirection = FlexDirection.Row;
            buttonContainer.Add(rowContainer);

            for (int col = 0; col < 5; col++) {
                int index = btnList.Count;
                Button button = new Button();
                button.text = "Button" + index;
                button.style.width = 100;
                button.style.height = 100;
                button.style.unityTextAlign = TextAnchor.LowerCenter;
                Texture2D previewTexture = AssetPreview.GetAssetPreview(prefabToDisplay);
                button.style.backgroundImage = previewTexture;
                button.clicked += () => BtnClick(index);
                
                rowContainer.Add(button);
                btnList.Add(button);
            }
        }
        Debug.Log("button数量" + btnList.Count);
    }

    private void BtnClick(int index) {
        if (currentButton != null) {
            currentButton.style.borderBottomColor = Color.clear;
            currentButton.style.borderTopColor = Color.clear;
            currentButton.style.borderLeftColor = Color.clear;
            currentButton.style.borderRightColor = Color.clear;
        }
        currentButton = btnList[index];
        Debug.Log(index);
        currentButton.style.borderBottomColor = Color.cyan;
        currentButton.style.borderTopColor = Color.cyan;
        currentButton.style.borderLeftColor = Color.cyan;
        currentButton.style.borderRightColor = Color.cyan;
    }

    private void OnGUI()
    {
        if (Event.current.type == EventType.MouseDown)
        {
            // 点击面板的其他地方时,重置按钮的边框颜色为无色
            //border.style.borderBottomColor = Color.clear;
            //border.style.borderTopColor = Color.clear;
            //border.style.borderLeftColor = Color.clear;
            //border.style.borderRightColor = Color.clear;
        }
        FunctionIndex = GUILayout.Toolbar(FunctionIndex, functionTitles);

        //空行为了给 rootVisualElement 绘制的按钮让出空间
        for (int i = 0; i < 17; i++) {
            GUILayout.Label("");
        }

        if (GUILayout.Button("999"))
        {
            Debug.Log("999");
        }
    }
}

 

posted @ 2023-11-08 14:13  三页菌  阅读(11)  评论(0编辑  收藏  举报