一、Create Custom Inspector

重绘inspector面板一方面是我们的挂在脚本的窗口变得友好,另一方面可以让其变得更强大,比如添加一些有效性验证。

二、重要说明

1.EditorUtility.SetDirty(object target):该方法可以表明target对象为dirty((⊙﹏⊙)b,怎么翻译合适?)的,unity会将有dirty标记的已经改变的对象在磁盘中存储,可以用来对一些修改数据的强制刷新,这里用来立即绘制GUI(我们对GUI的修改不会立即生效,只有鼠标在Scene上时才会强制重绘界面元素)。

2.对于类似GUI.Style等静态属性的修改,在修改前需要备份,使用后在设回,避免修改后的对其他无需使用修改后属性的干扰。

3.布局中的GUILayout.BeginVertical等需成对出现,有始有终。

4.该类型的脚本都需放在Editor文件夹下,unity会自动调用。

三、Code

 using UnityEngine;
using System.Collections;
using UnityEditor; namespace RunAndJump.LeveCreator
{
//[CanEditMultipleObjects] //希望多个挂在该类的对象可以在inspector面板选定多个一同修改的话
//需要将该标签添加上,由于level类一个场景下只有一个,所以无需添加该标记
[CustomEditor(typeof(Level))]
public class LevelInspector : Editor
{
private Level _myTarget;
private int _newTotalColumns;
private int _newTotalRows; private void OnEnable()
{
Debug.Log("OnEnable was called...");
_myTarget = (Level)target;
InitLevel();
ResetResizeValues();
} private void OnDisable()
{
Debug.Log("OnDisable was called...");
} private void OnDestory()
{
Debug.Log("OnDestroy was called...");
} public override void OnInspectorGUI()
{
EditorGUILayout.LabelField("The GUI of this inspector was modified.");
EditorGUILayout.LabelField("The current level time is:"+_myTarget.TotalTime); // DrawDefaultInspector();//使用unity默认的绘制方式来显示Inspector面板
DrawDateGUI();
DrawLevelSizeGUI(); //当使用绘制面板方法并且GUI改变时刷新GUI页面
if(GUI.changed)
EditorUtility.SetDirty(_myTarget);
} private void DrawLevelSizeGUI()
{
EditorGUILayout.LabelField("Size",EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal("box");
EditorGUILayout.BeginVertical();
_newTotalColumns = EditorGUILayout.IntField("Columns", Mathf.Max(, _newTotalColumns));
_newTotalRows = EditorGUILayout.IntField("Rows", Mathf.Max(, _newTotalRows));
EditorGUILayout.EndVertical();
EditorGUILayout.BeginVertical(); bool oldEnable = GUI.enabled;
GUI.enabled = (_newTotalColumns != _myTarget.TotalColumns || _newTotalRows != _myTarget.TotalRows);
bool buttonResize = GUILayout.Button("Resize", GUILayout.Height(*EditorGUIUtility.singleLineHeight));
if (buttonResize)
{
if (EditorUtility.DisplayDialog(
"Level Creator",
"Are you sure you want to resize the leve?\nThis action cannot be undone",
"yes",
"no"
))
{
ResizeLeve();
} } bool buttonReset = GUILayout.Button("Reset");
if (buttonReset)
{
ResetResizeValues();
}
GUI.enabled = oldEnable; EditorGUILayout.EndVertical();
EditorGUILayout.EndHorizontal();
} private void InitLevel()
{
if (_myTarget.Pieces == null || _myTarget.Pieces.Length == )
{
Debug.Log("Initializing thie Pieces array...");
_myTarget.Pieces=new LevelPiece[_myTarget.TotalColumns*_myTarget.TotalRows];
}
} private void ResetResizeValues()
{
_newTotalColumns = _myTarget.TotalColumns;
_newTotalRows = _myTarget.TotalRows;
} private void ResizeLeve()
{
LevelPiece[] newPieces=new LevelPiece[_newTotalColumns*_newTotalRows];
for (int col = ; col < _myTarget.TotalColumns; ++col)
{
for (int row = ; row < _myTarget.TotalRows; ++row)
{
if (col < _newTotalColumns && row < _newTotalRows)
{
newPieces[col + row*_newTotalColumns] = _myTarget.Pieces[col + row*_myTarget.TotalColumns];
}
else
{
LevelPiece piece = _myTarget.Pieces[col+row*_myTarget.TotalColumns];
if (piece != null)
{
//在editor模式下我们必须使用DestroyImmediate
Object.DestroyImmediate(piece.gameObject);
}
}
}
}
_myTarget.Pieces = newPieces;
_myTarget.TotalColumns = _newTotalColumns;
_myTarget.TotalRows = _newTotalRows;
} private void DrawDateGUI()
{
EditorGUILayout.LabelField("Data",EditorStyles.boldLabel);
EditorGUILayout.BeginVertical("box");
_myTarget.TotalTime = EditorGUILayout.IntField("Total Time", Mathf.Max(, _myTarget.TotalTime));
_myTarget.Gravity = EditorGUILayout.FloatField("Gravity", _myTarget.Gravity);
_myTarget.Bgm = (AudioClip) EditorGUILayout.ObjectField("Bgm", _myTarget.Bgm, typeof (AudioClip), false);
_myTarget.Background =
(Sprite) EditorGUILayout.ObjectField("Background", _myTarget.Background, typeof (Sprite), false);
EditorGUILayout.EndVertical();
}
}
}

四、效果

前:                    后:

    

Unity编辑器扩展 Chapter3--Create Custom Inspector的更多相关文章

  1. Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据

    Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject  Unity编辑器扩展 Chapt ...

  2. unity 编辑器扩展简单入门

    unity 编辑器扩展简单入门 通过使用编辑器扩展,我们可以对一些机械的操作实现自动化,而不用使用额外的环境,将工具与开发环境融为一体:并且,编辑器扩展也提供GUI库,来实现可视化操作:编辑器扩展甚至 ...

  3. Unity编辑器扩展chapter1

    Unity编辑器扩展chapter1 unity通过提供EditorScript API 的方式为我们提供了方便强大的编辑器扩展途径.学好这一部分可以使我们学会编写一些工具来提高效率,甚至可以自制一些 ...

  4. Unity编辑器扩展-Custom List, displaying data your way

    本文转自http://catlikecoding.com/unity/tutorials/editor/custom-list/ Custom List, displaying data your w ...

  5. Unity 编辑器扩展

    自定义检视面板的使用: 先是定义一个脚本文件,我们来修饰它的检视面板: [HelpURL("http://www.baidu.com")] public class Atr : M ...

  6. Unity编辑器扩展Texture显示选择框

    学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=. Unity开发了组件Inspector视图扩展API,如果我们要写插 ...

  7. Unity编辑器扩展

    在开发中有可能需要自己开发编辑器工具,在Unity中界面扩展常见两种情况,拿某插件为例: 1,自建窗口扩展 2,脚本Inspector显示扩展 不管使用那种样式,都需要经常用到两个类EditorGUI ...

  8. Unity 编辑器扩展 场景视图内控制对象

    http://blog.csdn.net/akof1314/article/details/38129031 假设有一个敌人生成器类,其中有个属性range用来表示敌人生成的范围区域大小,那么可以用O ...

  9. unity编辑器扩展学习

    扩展编辑器实际上就是在unity菜单栏中添加一些按钮,可以一键执行一些重复性的工作. 一.添加按钮 1.简单使用MenuItem特性 using UnityEngine; using UnityEdi ...

随机推荐

  1. Webdriver API中文版

    Webdriver API中文版 1.1   下载selenium2.0的lib包 http://code.google.com/p/selenium/downloads/list 官方UserGui ...

  2. ArcMap中用python的split方法提取字段的值

    提取PROPERTY_L字段空格分隔符前面的地址编号 提取前:5105 ABERDEEN LANE 提取后:5105 提取的表达式:!PROPERTY_L!.split(" ")[ ...

  3. XAMPP启动mysql问题

    Problem detected!21:57:44 [mysql] Port 3306 in use by ""E:\MySQL\bin\mysqld" --defaul ...

  4. Android 心跳呼吸动画

    废话少说,看东西 一个很简单的心跳呼吸的动画,几行代码搞定: 代码: private ImageView ivHart; //图片 AlphaAnimation alphaAnimation = nu ...

  5. 企业案例:查找当前目录下所有文件,并把文件中的https://www.cnblogs.com/zhaokang2019/字符串替换成https://www.cnblogs.com/guobaoyan2019/

    企业案例:查找当前目录下所有文件,并把文件中的https://www.cnblogs.com/zhaokang2019/字符串替换成https://www.cnblogs.com/guobaoyan2 ...

  6. redis相关操作&基本命令使用

    Redis简介 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API. Redis是 NoSQL技术阵营中的一员,它 ...

  7. 一步一步学习大数据:Hadoop 生态系统与场景

    Hadoop概要 到底是业务推动了技术的发展,还是技术推动了业务的发展,这个话题放在什么时候都会惹来一些争议. 随着互联网以及物联网的蓬勃发展,我们进入了大数据时代.IDC预测,到2020年,全球会有 ...

  8. Python学习手册之Python介绍、基本语法(一)

    一.什么是python? python是一种高级的编程语言.它适合编写一些应用程序,比如:网站编程,脚本编程,科学计算和最近非常热门的AI(人工智能).目前,Google,腾讯,百度,阿里巴巴,豆瓣都 ...

  9. ubuntu软件安装

    介绍常用的ubuntu软件及其安装 首先声明,本人在以下的操作全部基于腾讯云16.04版本ubuntu,若版本不一,有些出入,遇到问题可以在楼下留言. ubuntu中文官网 汉化终端 下载中文包 su ...

  10. 【python3】——centos7下安装

    centos7下安装python3总步骤分三步: 一.依赖解决: 1.安装依赖包: yum install zlib-devel bzip2-devel openssl-devel ncurses-d ...