目录

  • 创建一个窗口添加简单控件
  • 制作一个可以渐入渐出的窗口控件
  • 最后做一个比较炫酷的窗口

创建一个窗口,添加简单控件

在Editor创建脚本GUIWindow1如下,添加一些简单的控件,体验一下他们的功能

using UnityEngine;
using UnityEditor;public class GUIWindow1 : EditorWindow
{//  开启一个窗口[MenuItem ("Window/Example1")]static void Open (){GetWindow<GUIWindow1> ();}bool toogleVale;void OnGUI (){EditorGUI.BeginChangeCheck ();//      绘制一个LableEditorGUILayout.LabelField ("Example Label");//绘制toogle存储bool值toogleVale = EditorGUILayout.ToggleLeft ("Toogle", toogleVale);//检测窗口改变if (EditorGUI.EndChangeCheck ()) {if (toogleVale) {Debug.Log ("toogleVale :"+toogleVale);}}//      绘制一个TextFieldEditorGUILayout.TextField ("toogleVale:"+toogleVale.ToString());//      显示UIDisplay ();//      设置UI不可用EditorGUI.BeginDisabledGroup (toogleVale);Display ();EditorGUI.EndDisabledGroup ();GUI.enabled = toogleVale;Display();GUI.enabled = !toogleVale;Display ();}void Display (){//      空一行EditorGUILayout.Space ();EditorGUILayout.IntSlider (0, 10, 0);GUILayout.Button ("Button");}
}

在Unity中选择window–>Example1会打开如下窗口,点击Toggle有一些简单的禁用启用效果:

制作一个可以渐入渐出的窗口控件

功能稍微复杂一点,点击按钮可以控制控件有一个动画效果,创建代码GUIWindow2如下:

using UnityEngine;
using UnityEditor;
using UnityEditor.AnimatedValues;
using UnityEngine.Events;public class GUIWindow2 : EditorWindow
{[MenuItem("Window/Example2")]static void Open (){GetWindow<GUIWindow2> ();}//  声明一个动画浮点数AnimFloat animFloat = new AnimFloat (0.01f);Texture tex;void OnGUI (){bool on = animFloat.value == 1;if (GUILayout.Button (on ? "Close" : "Open", GUILayout.Width (64))) {animFloat.target = on ? 0.01f : 1;animFloat.speed = 4f;//          animFloat值变化时重新绘制窗口var env = new UnityEvent ();env.AddListener (() => Repaint ());animFloat.valueChanged = env;}//      水平排列控件EditorGUILayout.BeginHorizontal ();
//      控制控件淡入淡出EditorGUILayout.BeginFadeGroup (animFloat.value);Display ();EditorGUILayout.EndFadeGroup ();Display ();EditorGUILayout.EndHorizontal ();}bool boolValue;void Display (){
//      垂直排列控件EditorGUILayout.BeginVertical ();EditorGUILayout.Space ();boolValue= EditorGUILayout.ToggleLeft ("Toggle", boolValue);var options = new []{GUILayout.Width (128), GUILayout.Height (128)};tex = EditorGUILayout.ObjectField (tex, typeof(Texture), false, options) as Texture;GUILayout.Button ("Button");EditorGUILayout.EndVertical ();}
}

在Unity中选择window–>Example2会打开如下窗口,点击Open按钮会渐入新的控件,下面是效果图:

最后做一个比较炫酷的窗口

其实并不很炫酷啊,好吧,程序猿美感不好,创建代码GUIWindow3如下:

using UnityEngine;
using UnityEditor;public class GUIWindow3 : EditorWindow
{//  开启一个窗口[MenuItem ("Window/Example3")]static void Open (){GetWindow<GUIWindow3> ();}float angle = 0;bool on, one, two, three;int selected;GameObject go;Material mat;Texture texture;void OnGUI (){
//      制定特定位置的GUIEditorGUI.MultiFloatField (new Rect (0, 0, 200, EditorGUIUtility.singleLineHeight),new GUIContent ("Label"),new GUIContent[] {new GUIContent ("X"),new GUIContent ("Y"),new GUIContent ("Z")},new float[] { 0, 1, 2 });
//      空出几行不要盖上上面的GUI,还是GUILayout方便for (int i = 0; i < 6; i++) {EditorGUILayout.Space ();}//      创建object选择控件 GameObject,Material,AudioClipgo = EditorGUILayout.ObjectField (go, typeof(GameObject), false) as GameObject;mat = EditorGUILayout.ObjectField (mat, typeof(Material), false) as Material;EditorGUILayout.ObjectField (null, typeof(AudioClip), false);//      做一个固定大小的Layoutvar options = new []{ GUILayout.Width (64), GUILayout.Height (64) };texture = EditorGUILayout.ObjectField (texture, typeof(Texture), false, options) as Texture;EditorGUILayout.ObjectField (null, typeof(Sprite), false, options);//      创建一个带有层级关系的控件EditorGUILayout.LabelField ("Parent");EditorGUI.indentLevel++;for (int i = 0; i < 3; i++) {EditorGUILayout.LabelField ("Child"+i);//EditorGUI.indentLevel++;}EditorGUI.indentLevel--;EditorGUILayout.LabelField ("Parent");EditorGUI.indentLevel++;for (int i = 0; i < 3; i++) {EditorGUILayout.LabelField ("Child"+i);//EditorGUI.indentLevel--;}//      创建一个拉动旋钮angle = EditorGUILayout.Knob (Vector2.one * 100,angle, 0, 360, "度>>上下拉动旋钮", Color.blue, Color.red, true);//      创建一个横向排列的按钮using (new EditorGUILayout.HorizontalScope ()) {
//          按钮的使用if (GUILayout.Button ("Button1")) {Debug.Log ("点击了btn1");}
//          设置按钮颜色using (new BackgroundColorScope (Color.green)) {GUILayout.Button ("Button");}GUILayout.Button ("Button2");using (new BackgroundColorScope (Color.yellow)) {GUILayout.Button ("Button");}}//      创建一个toggleon = GUILayout.Toggle (on, on ? "on" : "off", "button");
//      复选框using (new EditorGUILayout.HorizontalScope ()) {one = GUILayout.Toggle (one, "1", EditorStyles.miniButtonLeft);two = GUILayout.Toggle (two, "2", EditorStyles.miniButtonMid);three = GUILayout.Toggle (three, "3", EditorStyles.miniButtonRight);}//      单选框selected = GUILayout.Toolbar (selected, new string[]{ "1", "2", "3" });selected = GUILayout.Toolbar (selected,new string[]{ "a", "b", "c" }, EditorStyles.toolbarButton);selected = GUILayout.SelectionGrid (selected,new string[]{ "1", "2", "3" }, 1, "PreferencesKeysElement");}
}public class BackgroundColorScope : GUI.Scope
{private readonly Color color;public BackgroundColorScope(Color color){this.color = GUI.backgroundColor;GUI.backgroundColor = color;}protected override void CloseScope(){GUI.backgroundColor = Color.cyan;}
}

最后上图,其实这些只是一个使用介绍,并不具有实际功能,至于用它们都能实现什么功能,还需要骚年们脑洞大开啊,上图如下:

本文工程:http://download.csdn.net/detail/warrenmondeville/9694668
本文链接:http://blog.csdn.net/warrenmondeville/article/details/53354806

Unity Editor 编辑器扩展 五 EditorGUI相关推荐

  1. Unity Editor 编辑器扩展一 编辑器特殊文件夹及内置资源读取

    目录 目录 编辑器特殊文件夹及内置资源读取 编辑器相关文件夹 读取内置资源包 代码 Test1_1cs 代码Editor1_1cs 这个要放到Editor文件夹内 编辑器特殊文件夹及内置资源读取 使用 ...

  2. Unity3D Editor 编辑器扩展3 Editor脚本

    Unity3D Editor 编辑器扩展3 Editor脚本 环境:Unity2017.2 语言:C# 总起: 在编辑Unity项目的时候,总不可能避免的接触到Unity自身自带的Inspector参 ...

  3. unity shader 编辑器扩展类 ShaderGUI

    这应该unity5才出的新功能了,今天看文档时刚巧看到了,就来尝试了一下. 效果如图: shader 的编辑器扩展分为2种方法: 是通过UnityEditor下的ShaderGUI类来实现的,形式比较 ...

  4. Unity教程之-制作闪亮的星星Star(三):给Star创建Unity Editor编辑器

    继续上篇文章<Unity教程之-制作闪亮的星星Star(二):创建Shader>,本篇我们来讲解 unity star editor的创建! 建立编辑器 Creating the Insp ...

  5. unity editor 编辑器插件制作基础:五、物体在Scene窗口中编辑状态下的辅助显示,以及控制功能扩展OnDrawGizmos GizmoType

    一 说明 该功能只能在编辑状态下运行,并只能在scene窗口中显示. 脚本必须继承MonoBehaviour ,并且引入UnityEngine和UnityEditor库 可以选择相关方法,在鼠标选择父 ...

  6. 【Unity Editor编辑器】 代码获取project面板选中资源路径(自定义右键菜单)

    在Unity编辑器中,如果想要快捷的获取到Project面板中选中文件的路径,比如我们需要用Resources.Load的方式加载一个prefab,就需要知道这个prefab的路径,或者在自定义窗口中 ...

  7. Unity3D Editor 编辑器扩展2 选取物体、撤销操作和窗口小部件的显示

    环境:Unity2017.2 语言:C# 总起: 今天主要介绍以下内容: 1.通过MenuCommand.Selection获取选中对象: 2.通过Undo编写可撤销的命令: 3.窗口小部件的显示. ...

  8. Unity3D 入门:Unity Editor 编辑器常用快捷键

    本文为 Unity3D 入门小伙伴整理 Unity 编辑器中的常用快捷键. 本文内容 调节工具 视图调节 窗口调整 进入退出播放模式 所有快捷键 调节工具 Unity 编辑器左上角的一组按钮,正好也对 ...

  9. Unity中编辑器扩展打包apk等

    记个笔记 因项目需要需要写个自定义打包工具,首先unity中提供了打包的方法我们只需要调用就可以,下面是核心的打包方法, 第一个参数是需要打包的场景, 第二个是打包路径, 第三个是打包类型比如安卓包等 ...

最新文章

  1. CentOS 6 IPv6 关闭方法
  2. 事件委托能够优化js性能
  3. 成功解决ModuleNotFoundError: No module named '_pywrap_tensorflow_internal'
  4. 标题栏外区域拖动窗体
  5. jQuery手机菜单
  6. 40、Power Query-Text.Combine函数进阶
  7. cannot bind to cxOutlet since it is not a known property of ng template
  8. windows组件向导里没有internet 信息服务(iis) 的解决办法
  9. QT添加资源文件并使用
  10. 数据库新技术:分布式数据库的体系结构,特点与查询优化(思维导图版总结)
  11. 关于解压软件和压缩软件
  12. JDK 安装与环境变量配置(Win10详细版)
  13. 安卓bochs安装linux教程,Ubuntu 14.04 LTS 安装和配置Bochs
  14. 服务器销售工资如何计算公式,拿提成的工资怎么算的有公式吗
  15. 一个创业者的自白:假如重回华为怎么做?
  16. 很有哲理的句子,每天都值得看一遍
  17. Javaweb-ajax的使用
  18. 网络安全实验--网络嗅探器
  19. apicloud命名空间$api方法集合
  20. 算法6.11 弗洛伊德算法

热门文章

  1. 用python画分段函数图像_使用Python的matplotlib画出一个分段函数的图像
  2. SpringBoot整合thymeleaf和Shiro项目绑定JS接口安全域名问题
  3. Manjaro Linux 双显卡安装步骤及独立显卡运行游戏(Nvidia GeForce GTX 980m)
  4. js控制的Flv视频播放器源码下载
  5. 【无标题语音聊天app源码——语音聊天派对】
  6. 尤雨溪:VUE 3 之后会休息一下
  7. vue图片懒加载插件vue-lazyload监听加载失败事件的解决方案
  8. Android 10 手机端控制车载蓝牙音乐上一首、暂停、下一首、获取音乐信息等流程
  9. 常用app URL schemes
  10. android kindle 无效文档,URL schemes 按官方文档设置无效