拓展自定义编辑器窗口(EditorGUILayout类)
Unity支持自行创建窗口,也支持自定义窗口布局。在Project视图中创建一个Editor文件夹,在文件夹中再创建一条脚本。
自定义窗口需要让脚本继承EditorWindow再设置MenuItem,此时在Unity导航菜单栏中GameObjec->window就可创建一个自定义窗口。
0.窗口:
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;
// }
}
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 -));
EditorGUILayout.EndScrollView();
}
voidReadTextAsset(TextAsset txt){
text = txt.text;
txtAsset = txt;
}
}

string text="";
voidOnGUI()
{
EditorGUILayout.SelectableLabel(text); //文本:可以选择然后复制粘贴
}
//创建密码字段并可视化在密码字段有什么键入。
string text ="Some text here";
function OnGUI(){
text =EditorGUILayout.PasswordField("Type Something:",text);
EditorGUILayout.LabelField("Written Text:", text);
}
}

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-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 =;
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 =,
SPHERE =,
PLANE =
}
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 =;
string[] names ={"Normal","Double","Quadruple"};
int[] sizes ={,,};
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=;
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 =;
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类)的更多相关文章
- Unity3D研究院之拓展自定义编辑器窗口
Unity支持自行创建窗口,也支持自定义窗口布局.在Project视图中创建一个Editor文件夹,在文件夹中在创建一条脚本. 自定义窗口需要让脚本继承EditorWindow在设置MenuItem, ...
- 【Unity】自定义编辑器窗口——拓展编辑器功能
最近学习了Unity自定义编辑器窗口,下面简单总结,方便用到时回顾. 新建一个脚本: using UnityEngine; using System.Collections; using UnityE ...
- Unity场景道具模型拓展自定义编辑器
(一)适用情况 当游戏主角进入特定的场景或者关卡,每个关卡需要加载不同位置的模型,道具等.这些信息需要先在unity编辑器里面配置好,一般由策划干这事,然后把这些位置道具信息保存在文件,当游戏主角进入 ...
- Unity 自定义编辑器窗口 画线
最近在学习状态机, 想自己实现一个可视化编辑器, 需要将多个状态之间用线条连接起来, 效果如下: 代码如下: Material m;Vector2 start;Vector2 end;Color co ...
- 【Unity编辑器】UnityEditor多重弹出窗体与编辑器窗口层级管理
一.简介 最近马三为公司开发了一款触发器编辑器,对于这个编辑器策划所要求的质量很高,是模仿暴雪的那个触发器编辑器来做的,而且之后这款编辑器要作为公司内部的一个通用工具链使用.其实,在这款触发器编辑器之 ...
- WPF自定义窗口基类
WPF自定义窗口基类时,窗口基类只定义.cs文件,xaml文件不定义.继承自定义窗口的类xaml文件的根节点就不再是<Window>,而是自定义窗口类名(若自定义窗口与继承者不在同一个命名 ...
- WPF自学入门(九)WPF自定义窗口基类
今天简单记录一个知识点:WPF自定义窗口基类,常用winform的人知道,winform的窗体继承是很好用的,写一个基础窗体,直接在后台代码改写继承窗体名.但如果是WPF要继承窗体,我个人感觉没有理解 ...
- WPF 之 创建继承自Window 基类的自定义窗口基类
开发项目时,按照美工的设计其外边框(包括最大化,最小化,关闭等按钮)自然不同于 Window 自身的,但窗口的外边框及窗口移动.最小化等标题栏操作基本都是一样的.所以通过查看资料,可按如下方法创建继承 ...
- (转)初步认识拓展UnityEditor编辑器定制
初步认识拓展UnityEditor编辑器定制 热度 9529 2015-9-4 18:50 |个人分类:Unity3d| 编辑器, 拓展 我相信无数初学者看别人游戏都经常看到他们的Inspector中 ...
随机推荐
- HTTP header 介绍
HTTP(Hyper Text Transfer Protocol)是超文本传输协议的缩写,它用于传送www方式的数据.HTTP协议采用了请求/响应模型.客服端向服务器发送一个请求,请求头包含请求的方 ...
- Java 快速开发平台 WB 6.8 发布
WebBuilder是一款开源的可视化Web应用开发和运行平台. 基于浏览器的集成开发环境,采用可视化的设计模式,支持控件的拖拽操作,能轻松完成前后台应用开发: 高效.稳定和可扩展的特点,适合复杂企业 ...
- LESS编译方案
我的LESS编译方案 2013-08-07 10:22 by 逆风之羽, 469 阅读, 2 评论, 收藏, 编辑 背景 近期项目前端决定使用less,简单介绍一下,详细信息有兴趣查看官方文档(htt ...
- Mathematics for Computer Graphics
Mathematics for Computer Graphics 最近严重感觉到数学知识的不足! http://bbs.gameres.com/showthread.asp?threadid=105 ...
- 页面缓存OutputCache
更新页面缓存OutputCache 为什么要使用OutputCache 我认为OutputCache是最简单的缓存技术了,它针对的是页面级别的,简单的一条指令就可以达到缓存的效果,有效的减轻服务器 ...
- Klockwork告警常见错误
下面列举的是Klockwork告警中常见的告警形式,这些情况在编译阶段都不会报出来语法上的错误,并且在运行阶段执行到的概率很小.但是在某些场景下一旦执行到了这些语句, 很可能引起进程的跑飞和挂起. ...
- javascript数组、对象和Null的typeof同为object,区分解决办法
在JS里typeof 大家用的很多,可以使对于数组.对象和Null无法区分的问题,看了看犀牛书还是有解决办法的. document.writeln(typeof "abc"); / ...
- hdu1205(类似 分布垃圾数列)
Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequen ...
- Docker容器内存监控
linux内存监控 要明白docker容器内存是如何计算的,首先要明白linux中内存的相关概念. 使用free命令可以查看当前内存使用情况. [root@localhost ~]$ free tot ...
- Android RecyclerView完全解析
RecyclerView完全解析 (一) 前言 话说RecyclerView已经面市很久,也在很多应用中得到广泛的使用,在整个开发者圈子里面也拥有很不错的口碑,那说明RecyclerView拥有比Li ...