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编辑器的菜单和视图界面,以及最基本的操作,这是入门的最基础部分,必须掌握. 二.启动界面 双击桌 ...
随机推荐
- SpringBoot3整合SpringSecurity6(三)基于数据库的用户认证
大家好,我是晓凡. 写在前面 上一篇文章中,我们了解了SpringSecurity怎么基于内存进行用户认证.但这还远远不够,在实际开发中. 用户往往都存在于数据库,所以从这篇文章开始,我们就要开始学习 ...
- ChatGPT为何放弃WebSocket?揭秘EventSource的三大决胜优势
ChatGPT为何放弃WebSocket?揭秘EventSource的三大决胜优势 感觉本篇对你有帮助可以关注一下我的微信公众号(深入浅出谈java),会不定期更新知识和面试资料.技巧!!! Chat ...
- 参考用例之“本地Excel导入系统测试方案”
本地Excel导入系统测试方案 Excel 代码 @Test public void importperson() throws FileNotFoundException { FileInputSt ...
- CentOS 7.6安装nginx
1.安装EPEL仓库 [root@ecs-lanxinmeichen nginx]# sudo yum install epel-releaseLoaded plugins: fastestmirro ...
- 内网服务器离线安装部署 Ollama
一.安装 Ollama 1.官网下载地址:Releases · ollama/ollama 2.cd至下载目录 3.执行二进制文件安装 sudo tar -C /usr -xzf ollama-lin ...
- MethodImpl优化性能
参数解释 MethodImplOptions.AggressiveInlining:请求编译器在可能的情况下对方法进行内联. MethodImpl:这是一个属性,允许开发者为方法指定特定的实现行为,比 ...
- Linux软件安装目录分类讲解
参考:https://blog.csdn.net/qq_22771739/article/details/83933473 /usr:系统级的目录,可以理解为C:/Windows/,/usr/lib理 ...
- ubuntu2204 ROS2安装
ubuntu初始环境配置ROS2 换源 备份原来的文件 sudo cp /etc/apt/sources.list /etc/apt/sources_init.list 换源 sudo gedit / ...
- 【目录】【中文】【deplearning.ai】【吴恩达课后作业目录】
[目录][吴恩达课后作业目录] 吴恩达深度学习相关资源下载地址(蓝奏云) 课程 周数 名称 类型 语言 地址 课程1 - 神经网络和深度学习 第1周 深度学习简介 测验 中英 传送门 无编程作业 编程 ...
- MyBatis实现对数据库的增删改查
首先,整个项目的结构如图: 本次主要是对tb_brand表实现增删改查. 创建先后顺序 创建的先后顺序我在前一篇博客已经说清楚了,就不再赘述了,如果不知道如何创建的话,说明对mybatis还是不了解, ...