拓展自定义编辑器窗口(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中 ...
随机推荐
- WCF学习笔记之传输安全
WCF学习笔记之传输安全 最近学习[WCF全面解析]下册的知识,针对传输安全的内容做一个简单的记录,这边只是简单的记录一些要点:本文的内容均来自[WCF全面解析]下册: WCF的传输安全主要涉及认证. ...
- TCP可靠传输的实现
TCP可靠传输的实现 1.概述 为方便描述可靠传输原理,假定数据传输只在一个方向上进行,即A发送数据,B给出确认 2.以字节为单位的滑动窗口 TCP的滑动窗口是以字节为单位的.为了 ...
- 设计模式:空对象模式(Null Object Pattern)
设计模式:空对象模式(Null Object Pattern) 背景 群里聊到<ASP.NET设计模式>,这本书里有一个“Null Object Pattern”,大家就闲聊了一下这个模式 ...
- U盘安装CentOS 6.4 + Windows 7双系统 (Windows 7下安装 CentOS 6.4)
最近在看<鸟哥私房菜:基础学习篇>,觉得很不错,想要装个windows 7 和 CentOS 6.4 双系统,在网上找了很多教程,觉得乱七八糟的,弄得很复杂,而且很多都不是很完整,对于新手 ...
- WCF学习心得----(三)服务承载
WCF学习心得----(三)服务承载 这一章节花费了好长的时间才整理个大概,主要原因是初次接触这个东西,在做练习实践的过程中,遇到了很多的问题,有些问题到目前还没有得以解决.所以在这一章节中,有一个承 ...
- SPFILE 、PFILE 的全面解读
这里先阐述一下数据库的启动过程: 1. 启动实例/例程(nomount状态)时,读取参数文件(文本文件PFILE 或服务器参数文件SPFILE),分配SGA.启动后台进程.打开告警文件及后台 ...
- Cocos2d-精灵的几个常识
性能考虑 该部分是总结的cocos2d的在线文档 1)如果有每个帧有25个以下的精灵需要更新,可以直接使用精灵 class TLayer(cocos.layer.Layer): is_even ...
- oracle报表开发方案
PL/SQL也是一门语言,后台开发经常会用到. 目前做要到一个功能,关于"报表任务调度",说明白了就是做几张报表,每天统计一次新数据,用于在PC页面上显示,我苦思冥想了几天,总结出 ...
- winform总结4> 工欲善其事,必先利其器之xml校验
@echo 根据xml自动生成xml @echo 当前路径包含空格会导致执行失败 ::pause @echo off set path=%~dp0 for /r %path% %%i in (*.xm ...
- Golang爬虫示例包系列教程(一):pedaily.com投资界爬虫
Golang爬虫示例包 文件结构 自己用Golang原生包封装了一个爬虫库,源码见go get -u -v github.com/hunterhug/go_tool/spider ---- data ...