拓展自定义编辑器窗口(EditorGUILayout类)
原文地址:http://www.cnblogs.com/caymanlu/p/5722549.html
Unity支持自行创建窗口,也支持自定义窗口布局。在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本。
自定义窗口需要让脚本继承EditorWindow再设置MenuItem,此时在Unity导航菜单栏中GameObjec->window就可创建一个自定义窗口。
0.窗口:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
using UnityEngine;
using UnityEditor; //引入编辑器命名空间
publicclassMyEditor:EditorWindow
{
[MenuItem( "GameObject/caymanwindow" )]
staticvoidAddWindow()
{
//创建窗口
Rect wr =newRect(0,0,500,500);
//另一种方式:myEditor window = (myEditor)EditorWindow.GetWindow(typeof(myEditor), true, "cayman");
MyEditor window =(MyEditor)EditorWindow.GetWindowWithRect( typeof (MyEditor),wr, true , "widow name" );
window.Show();
}
//[MenuItem("GameObject/caymanwindow", true)] //如果没有选择物体,禁用菜单
// static bool ValidateSelection() {
// return Selection.activeGameObject != null;
// }
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
publicclass myEditor3 :EditorWindow{
//在编辑器显示一个标签,带有自编辑器开始的秒数
[MenuItem( "cayman/tempShow" )]
staticvoid newWelcome()
{
myEditor3 window3 =(myEditor3)EditorWindow.GetWindow( typeof (myEditor3), true , "Eam" );
window3.Show();
}
voidOnGUI()
{
EditorGUILayout.LabelField( "Time since start: " ,EditorApplication.timeSinceStartup.ToString());
this .Repaint(); //实时刷新
}
}
|
//如果开关控件被选择,显示一个按钮。 bool showBtn =true; voidOnGUI() { showBtn =EditorGUILayout.Toggle("Show Button", showBtn); if(showBtn) { if(GUILayout.Button("Close")) this.Close(); } }
//通过字段,自动改变选择物体的名字 string objectName =""; voidOnGUI() { GUILayout.Label("Select an object in the hierarchy view"); if(Selection.activeGameObject) Selection.activeGameObject.name =EditorGUILayout.TextField("Object Name: ",Selection.activeGameObject.name); this.Repaint();//实时刷新 } }
//在编辑器窗口可视化脚本,这可扩展保存脚本 string text ="Nothing Opened..."; TextAsset txtAsset; Vector2 scroll; voidOnGUI() { TextAsset newTxtAsset =EditorGUILayout.ObjectField("添加", txtAsset,typeof(TextAsset),true)asTextAsset; if(newTxtAsset != txtAsset) ReadTextAsset(newTxtAsset); scroll =EditorGUILayout.BeginScrollView(scroll); text =EditorGUILayout.TextArea(text,GUILayout.Height(position.height -30)); EditorGUILayout.EndScrollView(); } voidReadTextAsset(TextAsset txt){ text = txt.text; txtAsset = txt; } }
string text="123"; voidOnGUI() { EditorGUILayout.SelectableLabel(text); //文本:可以选择然后复制粘贴 }
//创建密码字段并可视化在密码字段有什么键入。 string text ="Some text here"; function OnGUI(){ text =EditorGUILayout.PasswordField("Type Something:",text); EditorGUILayout.LabelField("Written Text:", text); } }
1
2
3
4
5
6
7
8
|
int clones=1;
voidOnGUI(){
clones=EditorGUILayout.IntField( "Number of clones:" , clones);
if (GUILayout.Button( "Clone!" ))
for ( var i =0; i < clones; i++) //复制选择物体的次数。
Instantiate(Selection.activeGameObject,Vector3.zero,Quaternion.identity);
}
}
|
1
2
3
4
5
6
7
8
9
10
11
|
//缩放选择的游戏物体,在1-100之间
float scale =0.0f;
voidOnGUI()
{
scale =EditorGUILayout.Slider(scale,1,100);
}
voidOnInspectorUpdate()
{
if (Selection.activeTransform)
Selection.activeTransform.localScale =newVector3(scale, scale, scale);
}
|
//随机放置选择的物体在最小最大滑动条之间 float minVal =-10.0f; float minLimit =-20.0f; float maxVal =10.0f; float maxLimit =20.0f; voidOnGUI() { EditorGUILayout.LabelField("Min Val:", minVal.ToString()); EditorGUILayout.LabelField("Max Val:", maxVal.ToString()); EditorGUILayout.MinMaxSlider(ref minVal,ref maxVal, minLimit, maxLimit); if(GUILayout.Button("Move!")) PlaceRandomly(); } voidPlaceRandomly() { if(Selection.activeTransform) Selection.activeTransform.position = newVector3(Random.Range(minVal, maxVal), Random.Range(minVal, maxVal), Random.Range(minVal, maxVal)); else Debug.LogError("Select a GameObject to randomize its position."); }
string[] options ={"Cube","Sphere","Plane"}; int index =0; voidOnGUI() { index =EditorGUILayout.Popup(index, options); if(GUILayout.Button("Create")) InstantiatePrimitive(); } voidInstantiatePrimitive(){ switch(index){ case0: GameObject cube=GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position =Vector3.zero; break; case1: GameObject sphere=GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position =Vector3.zero; break; case2: GameObject plane=GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.position =Vector3.zero; break; default: Debug.LogError("Unrecognized Option"); break; } } }
enum OPTIONS { CUBE =0, SPHERE =1, PLANE =2 } publicclass myEditor3 :EditorWindow{ OPTIONS op=OPTIONS.CUBE; [MenuItem("cayman/tempShow")] staticvoid newWelcome() { myEditor3 window3 =(myEditor3)EditorWindow.GetWindow(typeof(myEditor3),true,"Eam"); window3.Show(); } voidOnGUI() { op =(OPTIONS)EditorGUILayout.EnumPopup("Primitive to create:", op); } }
int selectedSize =1; string[] names ={"Normal","Double","Quadruple"}; int[] sizes ={1,2,4}; voidOnGUI() { selectedSize =EditorGUILayout.IntPopup("Resize Scale: ", selectedSize, names, sizes); if(GUILayout.Button("Scale")) ReScale(); } voidReScale() { if(Selection.activeTransform) Selection.activeTransform.localScale =newVector3(selectedSize, selectedSize, selectedSize); elseDebug.LogError("No Object selected, please select an object to scale."); }
string tagStr ="";int selectedLayer=0; voidOnGUI() { //为游戏物体设置 tagStr =EditorGUILayout.TagField("Tag for Objects:", tagStr); if(GUILayout.Button("Set Tag!")) SetTags(); if(GUILayout.Button("Set Layer!"))SetLayer(); } voidSetTags(){ foreach(GameObject go inSelection.gameObjects) go.tag = tagStr; }voidSetLayer(){ foreach(GameObject go inSelection.gameObjects) go.laye= tagStr; }
Object source; Texture myme; voidOnGUI() { EditorGUILayout.BeginHorizontal(); source =EditorGUILayout.ObjectField("hiahia",source,typeof(Object)); myme= (Texture)EditorGUILayout.ObjectField("hehe",myme,typeof(Texture));//注意类型转换 EditorGUILayout.EndHorizontal(); }
float distance =0; Vector2 p1, p2; voidOnGUI() { p1 =EditorGUILayout.Vector2Field("Point 1:", p1); p2 =EditorGUILayout.Vector2Field("Point 2:", p2); EditorGUILayout.LabelField("Distance:", distance.ToString()); } voidOnInspectorUpdate()//面板刷新 { distance =Vector2.Distance(p1, p2); this.Repaint(); }
Color matColor =Color.white; voidOnGUI() { matColor =EditorGUILayout.ColorField("New Color", matColor); if(GUILayout.Button("Change!")) ChangeColors(); } voidChangeColors(){ if(Selection.activeGameObject) foreach(var t inSelection.gameObjects) if(t.GetComponent<Renderer>()) t.GetComponent<Renderer>().sharedMaterial.color = matColor; }
using UnityEngine;
using UnityEditor;
publicclassMyEditor:EditorWindow
{
[MenuItem("GameObject/caymanwindow")]
staticvoidAddWindow()
{
//创建窗口
Rect wr =newRect(0,0,500,500);
MyEditor window =(MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor),wr,true,"widown name");
window.Show();
}
//输入文字的内容
privatestring text;
//选择贴图的对象
privateTexture texture;
float myFloat =1.23f;
private bool kaiguan;//开关
private bool groupEnabled;//区域开关
publicvoidAwake()
{
//在资源中读取一张贴图
texture =Resources.Load("1")asTexture;
}
//绘制窗口时调用
voidOnGUI()
{
//输入框控件
text =EditorGUILayout.TextField("输入文字:",text);//3.制作一个文本字段
if(GUILayout.Button("打开通知",GUILayout.Width(200)))
{
//打开一个通知栏
this.ShowNotification(newGUIContent("This is a Notification"));
}
if(GUILayout.Button("关闭通知",GUILayout.Width(200)))
{
//关闭通知栏
this.RemoveNotification();
}
//文本框显示鼠标在窗口的位置
EditorGUILayout.LabelField("鼠标在窗口的位置",Event.current.mousePosition.ToString());//1.制作一个标签字段(通常用于显示只读信息)
- showBtn = EditorGUILayout.Toggle("开关", showBtn);
groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);text21 = EditorGUILayout.TextField("请输入帐号:", text21);text3 = EditorGUILayout.PasswordField("请输入密码",text3); //密码输入if (GUILayout.Button("登录", GUILayout.Width(400))){//}int01 = EditorGUILayout.IntField("输入实例化份数:",int01);if (GUILayout.Button("实例化")) //根据份数,实例化选择的物体{for (int i = 0; i < int01; i++){Instantiate(Selection.activeGameObject,Vector3.zero,Quaternion.identity);}}EditorGUILayout.EndToggleGroup();scale1 = EditorGUILayout.Slider(scale1,1,100); //滑动条index = EditorGUILayout.Popup(index,options); //弹出选择菜单if(GUILayout.Button("创建一个")){switch (index){case 0:GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);cube.transform.position = Vector3.zero;break;case 1:GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);sphere.transform.position = Vector3.zero;break;case 2:GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);plane.transform.position = Vector3.zero;break;default:break;}}showPosition = EditorGUILayout.Foldout(showPosition, status); //制作一个左侧带有箭头的折叠标签if (showPosition){if (Selection.activeTransform){Selection.activeTransform.position =EditorGUILayout.Vector3Field("Position", Selection.activeTransform.position);status = Selection.activeTransform.name;}if (!Selection.activeTransform){status = "Select a GameObject";showPosition = false;}}
//选择贴图
texture =EditorGUILayout.ObjectField("添加贴图",texture,typeof(Texture),true)asTexture;
groupEnabled =EditorGUILayout.BeginToggleGroup("Optional Settings",groupEnabled);//起始----------------------------
//这里放开关区域内内容
myFloat =EditorGUILayout.Slider("Slider", myFloat,-3,3);//滑动条
kaiguan=EditorGUILayout.Toggle("开关", kaiguan);//2.开关
EditorGUILayout.EndToggleGroup();//结束-------------------------------------
if(GUILayout.Button("关闭窗口",GUILayout.Width(200)))
{
//关闭窗口
this.Close();
}
}
voidOnFocus()
{
Debug.Log("当窗口获得焦点时调用一次");
}
voidOnLostFocus()
{
Debug.Log("当窗口丢失焦点时调用一次");
}
voidOnHierarchyChange()
{
Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
}
voidOnProjectChange()
{
Debug.Log("当Project视图中的资源发生改变时调用一次");
}
voidOnInspectorUpdate()//实时刷新面板
{
//Debug.Log("窗口面板的更新");
//这里开启窗口的重绘,不然窗口信息不会刷新
this.Repaint();
}
voidOnSelectionChange()
{
//当窗口出去开启状态,并且在Hierarchy视图中选择某游戏对象时调用
foreach(Transform t inSelection.transforms)
{
//有可能是多选,这里开启一个循环打印选中游戏对象的名称
Debug.Log("OnSelectionChange"+ t.name);
}
}
voidOnDestroy()
{
Debug.Log("当窗口关闭时调用");
}
}
//http://www.ceeger.com/Script/EditorGUILayout/EditorGUILayout.html
拓展自定义编辑器窗口(EditorGUILayout类)相关推荐
- 【unity】拓展自定义编辑器窗口(二)
http://www.xuanyusong.com/archives/2211 Unity支持自行创建窗口,也支持自定义窗口布局.在Project视图中创建一个Editor文件夹,在文件夹中在创建一条 ...
- Unity 自定义编辑器窗口的使用
Unity 自定义编辑器窗口功能强大,可以实现所有希望实现的功能.我在近期的项目中仿照魔兽争霸3开发了一套简单的游戏单位功能组件,其中包括单位数据.移动方式.动画播放.武器系统等等.如果用传统的Ins ...
- 【Unity编辑器】UnityEditor多重弹出窗体与编辑器窗口层级管理
一.简介 最近马三为公司开发了一款触发器编辑器,对于这个编辑器策划所要求的质量很高,是模仿暴雪的那个触发器编辑器来做的,而且之后这款编辑器要作为公司内部的一个通用工具链使用.其实,在这款触发器编辑器之 ...
- 为SSIS编写自定义数据流组件(DataFlow Component)之进阶篇:自定义编辑器
我们之前几篇讨论过自定义数据流组件的一些技术,分别如下 入门篇 http://www.cnblogs.com/chenxizhang/archive/2009/06/20/1507467.html 数 ...
- Toast拓展--自定义显示时间和动画
Toast拓展–自定义显示时间和动画 我们在Android应用开发中经常会需要在界面上弹出一个对界面操作无影响的小提示框来提示用户一些信息,这时候一般都会使用Android原生的Toast类 Toas ...
- Spring -- 入门,装备集合,自动装配,分散装配,自定义编辑器
1. 概要 struts2:web hibernate:持久化 spring:业务层.管理bean的,容器.List Map Set.体验spring:1.创建java项目.2.引入spring的类库 ...
- 百度地图api html信息窗口,百度地图 javascript api自定义信息窗口
百度 js api中存在infoWindow的类,就是marker 点击后显示的信息窗口, 他存在一下问题 1.在map中只能显示一个infowindow, 2.该信息窗口的样式无法修改,显示的内容具 ...
- cesium 之自定义气泡窗口 infoWindow 篇
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 自 ...
- cesium 之自定义气泡窗口 infoWindow 后续优化篇(附源码下载)
前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 该 ...
最新文章
- 一个缓存穿透引发的血案
- 开发自定义JSF组件(4) 保存状态与恢复状态
- [旧博客]Python 第一次
- 28. 实现 strStr() golang
- maven项目多模块部署的时候构建顺序
- TIOBE 5 月编程语言排行榜:Python、C++ 竞争白热化,Objective-C 已沦为小众语言
- zabbix agent类型的所有key 值
- [leetcode]208. 实现 Trie (前缀树)
- ORACLE数据泵还原(IMPDP命令)【转】
- C# 使用 Task 替换 ThreadPool ,异步监测所有线程(任务)是否全部执行完毕
- Can you answer these queries? HDU 4027 线段树
- Java集合类源码详解
- 卸载ps显示无法连接adobe服务器,PS还在无法安装?无法卸载?通通搞定!
- python矩阵运算法则_python怎么进行矩阵运算?
- python求音频的梅尔倒谱系数
- 研华IO板卡驱动安装与PICE-1753(PCIE-1751)接线说明
- dso详解--dso原理
- C/C++结构体struct详解
- JDK中的Hprof命令
- 【可见光室内定位】(二)基于光电器件PD的可见光室内定位技术
热门文章
- 华为路由器(可网管)连接光猫配置(光猫为路由模式)
- 学习笔记(16):重叠元素
- $().ajaxSubmit is not a function解决方案
- opencv 去除背景算法的比较
- word公式编号及交叉引用技巧
- GDP: Network Pruning
- RK3399 Android7.1如何查看屏幕分辨率
- MTK AF Tuning Preparation项目
- 微信小程序wxml和wxss样式
- 【OpenVINO】OpenVINO 2022.1 安装教程(Windows)