首页 > 其他分享 >Unity 编辑器UI 杂记

Unity 编辑器UI 杂记

时间:2023-11-08 14:22:21浏览次数:32  
标签:style currentButton Color button 编辑器 private Unity UI clear

 

用 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");
        }
    }
}

 

标签:style,currentButton,Color,button,编辑器,private,Unity,UI,clear
From: https://www.cnblogs.com/sanyejun/p/17817300.html

相关文章

  • WPF 使用 CommunityToolkit.Mvvm
    参考文档: IntroductiontotheMVVMToolkit-CommunityToolkitsfor.NET|MicrosoftLearn它是一个现代化,快速和模块化的MVVM库,对应用程序的结构或编译规范没有严格的限制。NuGet安装包搜索:CommunityToolkit.Mvvm导入usingCommunityToolkit.Mvvm;使用ObservableObjectpubli......
  • [-006-]-Python3+Unittest+Selenium Web UI自动化测试之悬浮窗口中的元素点击
     1.分析现状:PPT模板悬浮出现悬浮窗口悬浮窗口中分为4大类:PPT模板,PPT模板页,PPT关系图,PPT图表大类下存在小类点击可跳转但是此页面里还存在PPT模板下的总结汇报等此种情况的元素此情况如果仅用text定位是无法定位到的所以排除了text定位方式2.解决方法:首先我们看下悬浮窗......
  • Web Profile Builder for Web Application Projects
    WebProfileBuilderforWebApplicationProjectsFilescanbedownloadedfromtheWebProfileBuilderprojectpage.IfyouuseWebApplicationProjects,youhaveprobablyrunintotheissueofnotbeingabletoaccesstheProfileatdesigntime.Thankfully......
  • C#多线程与UI响应
    一、概述在使用C#进行应用程序设计时,经常会采用多线程的方式进行一些后台任务的工作。对于不同的应用场景,使用的策略也不尽相同。1.  后台循环任务,少量UI更新:例如批量上传文件,并提供进度。这种情况使用BackgroundWorker组件是非常好的选择。2.  耗时的后......
  • WPF多UI线程
       internalclassSpashWindowManager{privatestaticSpashWindow_spashWindow;privatestaticThreadthread;publicstaticvoidShow(){thread=newThread(()=>{_spashW......
  • SQL Server 2005 数据库对象命名规范 Database Naming Conventions(Guidelines)
    使用SQLServer2005命名数据库对象时可以参照微软的示例数据库AdventureWorks。其次可以参照以下内容:DatabaseNamingConventionsVersion1.1LastRevisedMay13,2004byJasonMaussThemaingoalofadoptinganamingconventionfordatabaseobjectsissothatyouand......
  • Unity游戏排行榜制作与性能优化
    游戏排行榜是一个很重要的功能,在弱联网的单机游戏与网络游戏中排行榜都是非常重要的,今天我们来详细的讲解游戏排行榜的制作方案,主要有4个点:(1) 游戏排行榜排序算法核心算法实现;(2) 游戏排行服务器如何制作;(3) Unity客户端如何对接与请求排行榜数据;(4) Unity如何优化......
  • Long UUID生成
    编者在开发过程中用postman测试接口,发现要求id为必填且不能含有英文字母,问了对面开发人员才知道需要自己生成20位Long型uuid,写法大概如下,在需要生成的部分调用这个类即可。packagenc.bs.task.util;importjava.text.SimpleDateFormat;publicclassUUID{privatestaticvola......
  • 一键生成前端UI,公司90%项目UI都靠它搞定
    今天看公众号,有大神分析了这样的一篇文章:一键生成前端UI,公司90%项目UI都靠它搞定地址:https://mp.weixin.qq.com/s/UhmLwVeZ0jwZORur8XD2MQ ......
  • C/C++ __builtin 超实用位运算函数总结
    以__builtin开头的函数,是一种相当神奇的位运算函数,下面本人盘点了一下这些以__builtin开头的函数,希望可以帮到大家。1__builtin_ctz()/__buitlin_ctzll()用法:返回括号内数的二进制表示数末尾0的个数//eg:#include<bits/stdc++.h>usingnamespacestd;intmain......