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");
}
}
}
Unity 编辑器UI 杂记的更多相关文章
- 实现Unity编辑器模式下的旋转
最近在做一个模型展示的项目,我的想法是根据滑动屏幕的x方向差值和Y方向的差值,来根据世界坐标下的X轴和Y轴进行旋转,但是实习时候总是有一些卡顿.在观察unity编辑器下的旋转之后,发现编辑器下的旋转非 ...
- 【Unity编辑器】UnityEditor多重弹出窗体与编辑器窗口层级管理
一.简介 最近马三为公司开发了一款触发器编辑器,对于这个编辑器策划所要求的质量很高,是模仿暴雪的那个触发器编辑器来做的,而且之后这款编辑器要作为公司内部的一个通用工具链使用.其实,在这款触发器编辑器之 ...
- Unity快手上手【熟悉unity编辑器,C#脚本控制组件一些属性之类的】
Unity学习参考文档和开发工具 unity的官网文档:https://docs.unity3d.com/cn/current/Manual/ScriptingSection.html ■ 学习方式: ...
- 定制你的Unity编辑器
Unity的编辑器可以通过写脚本进行界面定制,包括添加功能菜单,今天写游戏Demo用到了记录一下. 为Unity添加子菜单 示例程序 [AddComponentMenu("Defend Ho ...
- 【Unity优化】如何实现Unity编辑器中的协程
Unity编辑器中何时需要协程 当我们定制Unity编辑器的时候,往往需要启动额外的协程或者线程进行处理.比如当执行一些界面更新的时候,需要大量计算,如果用户在不断修正一个参数,比如从1变化到2,这种 ...
- Unity的UI究竟为什么可以合批
1.UI/Default代码研究首先,我想到的是,既然是对图集纹理进行采样,而且又不能统一更改材质的纹理UV值,我们通常写的shader都是直接根据模型UV值对主纹理进行采样,那会不会是shader中 ...
- Unity编辑器:自定义编辑器样式——GUIStyle
通过GUIStyle,可以自定义Unity编辑器的样式. GUIStyle可以new一个全新的实例,这样,需要自己处理所有自己需要的效果. GUIStyle还可以基于已经存在的实例new一个新的实例, ...
- 使用C#的Conditional特性与Unity编辑器宏命令做条件编译
概要 在传统的C#项目中,用Conditional特性做条件编译时,需要在Visual Studio中项目的属性里添加上条件编译符号,用法参考这篇文章. 而在Unity项目中,条件编译符号需要在Uni ...
- 通过反射调用Unity编辑器提供的各种功能
Unity编辑器功能丰富易上手,其实编辑器提供的大多数菜单操作,在代码里面都是能够找到对应接口的,但是这些接口都没有对我们开放,怎么办? 很简单,直接使用反射调用即可. 首先使用Reflector或I ...
- 【Unity】2.1 初识Unity编辑器
分类:Unity.C#.VS2015 创建日期:2016-03-26 一.简介 本节要点:了解Unity编辑器的菜单和视图界面,以及最基本的操作,这是入门的最基础部分,必须掌握. 二.启动界面 双击桌 ...
随机推荐
- 【译】Visual Studio 推出预览版 Agent 模式
规划.构建.测试.修复 -- 一切只需一个提示. Visual Studio 17.14 版本已向所有用户公开预览版 Agent 模式.Visual Studio 中的 Agent 模式允许您使用自然 ...
- C++ ADL 与 模板
什么是ADL ADL(Argument Dependent Lookup),参数依赖查找,明确的意思是依赖参数的函数查找,即对于函数调用,不仅会依照常规的名称查找规则,还会在函数参数所在的命名空间内查 ...
- 使用qt+网上的api做股票查看器
股票球,采用的是qt和新浪的api来设计,目前只有查看功能,2021年1月17日开始开发,后续可能会持续更新(可能跟心情有关) k线图在 Windows下获取数据有问题,还没来得及做,目前在Linux ...
- Maven中dependencyManagement标签的正确使用方法
目录 前言 使用方法 结束语 前言 Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式,她用于声明所依赖的jar包的版本号等信息.当所有子项目再次引入这些ja ...
- centos7系统安装后的基础优化2
1.更改网卡信息 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 3 ...
- 技术分享:主流GUI自动化框架的窗口置顶机制实现对比
前言 在自动化测试和RPA开发过程中,窗口置顶是一个常见且关键的需求.无论是确保测试窗口始终可见,还是保证自动化操作的准确性,各大Python自动化框架都提供了相应的窗口置顶功能.本文将深入分析主流框 ...
- Kubernetes数据存储-本地存储
简单存储 EmptyDir EmptyDir是最基础的Volume类型,一个EmptyDir就是Host上的一个空目录. EmptyDir是在Pod被分配到Node时创建的,它的初始内容为空,并且无须 ...
- 基于CentOS Stream 8的物联网数据采集与展示方案
系统架构全景图 图表 一.系统平台优化(CentOS Stream 8) 1. 系统基础配置 bash # 1. 系统更新与加固 sudo dnf update -y sudo dnf install ...
- 了解php当中简单的单例模式
本文由 ChatMoney团队出品 单例模式是一种常用的设计模式,它的核心思想是确保一个类只有一个实例,并提供一个全局访问点来获取这个实例.在 PHP 中实现单例模式通常有三种形式:饿汉式(Eager ...
- AWS S3文件存储服务器搭建(新)
之前写过一版 AWS S3文件存储服务器搭建 基本操作一样,只是在搭建 CloudFront 有点不同. 一:创建s3桶 AWS S3文件存储服务器搭建 二:使用自己的链接访问 1:申请Cloud ...