Unity编辑器扩展 Chapter3--Create Custom Inspector
一、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的更多相关文章
- Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据
Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject Unity编辑器扩展 Chapt ...
- unity 编辑器扩展简单入门
unity 编辑器扩展简单入门 通过使用编辑器扩展,我们可以对一些机械的操作实现自动化,而不用使用额外的环境,将工具与开发环境融为一体:并且,编辑器扩展也提供GUI库,来实现可视化操作:编辑器扩展甚至 ...
- Unity编辑器扩展chapter1
Unity编辑器扩展chapter1 unity通过提供EditorScript API 的方式为我们提供了方便强大的编辑器扩展途径.学好这一部分可以使我们学会编写一些工具来提高效率,甚至可以自制一些 ...
- Unity编辑器扩展-Custom List, displaying data your way
本文转自http://catlikecoding.com/unity/tutorials/editor/custom-list/ Custom List, displaying data your w ...
- Unity 编辑器扩展
自定义检视面板的使用: 先是定义一个脚本文件,我们来修饰它的检视面板: [HelpURL("http://www.baidu.com")] public class Atr : M ...
- Unity编辑器扩展Texture显示选择框
学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=. Unity开发了组件Inspector视图扩展API,如果我们要写插 ...
- Unity编辑器扩展
在开发中有可能需要自己开发编辑器工具,在Unity中界面扩展常见两种情况,拿某插件为例: 1,自建窗口扩展 2,脚本Inspector显示扩展 不管使用那种样式,都需要经常用到两个类EditorGUI ...
- Unity 编辑器扩展 场景视图内控制对象
http://blog.csdn.net/akof1314/article/details/38129031 假设有一个敌人生成器类,其中有个属性range用来表示敌人生成的范围区域大小,那么可以用O ...
- unity编辑器扩展学习
扩展编辑器实际上就是在unity菜单栏中添加一些按钮,可以一键执行一些重复性的工作. 一.添加按钮 1.简单使用MenuItem特性 using UnityEngine; using UnityEdi ...
随机推荐
- PAT——1071. 小赌怡情
常言道“小赌怡情”.这是一个很简单的小游戏:首先由计算机给出第一个整数:然后玩家下注赌第二个整数将会比第一个数大还是小:玩家下注t个筹码后,计算机给出第二个数.若玩家猜对了,则系统奖励玩家t个筹码:否 ...
- 我的QT5学习之路(二)——第一个程序
一.前言 “工欲善其事,必先利其器”,上一节,我介绍了Qt的安装和配置方法,搭建了基本的开发平台.这一节,来通过一个简单的例子来了解Qt的编程样式和规范,开始喽~~~ 二.第一个程序——Hello W ...
- iOS 根据url生成二维码贴到底图上
根据url 生成指定尺寸的二维码图片 UIImage * createBinaryCodeImg(const char * url ,CGFloat size) { //create binary c ...
- mac终端输入python默认打开python3
*** 1. 终端打开.bash_profile文件 ***open ~/.bash_profile *** 2. .bash_profile文件内容 ***# Setting PATH for Py ...
- 设置PL/SQL Developer 字符集
本文转自:http://blog.itpub.net/26613085/viewspace-765429/ 适用于:客户端和服务端不一致的情况,或者客户端某个字段的值乱码
- Java运算符使用总结(重点:自增自减、位运算和逻辑运算)
Java运算符共包括这几种:算术运算符.比较运算符.位运算符.逻辑运算符.赋值运算符和其他运算符.(该图来自网络) 简单的运算符,就不过多介绍使用了,可自行测试.关于赋值运算,可以结合算术运算和位运算 ...
- webpack+vuecli使用问题总结
1,按照官网安装步骤install $ npm install -g vue-cli $ vue init webpack my-project $ cd my-project $ npm insta ...
- scrapy基础
scrapy Scrapy 是用 Python 实现的一个为了爬取网站数据.提取结构性数据而编写的应用框架. Scrapy 常应用在包括数据挖掘,信息处理或存储历史数据等一系列的程序中. Scrapy ...
- 一个数据仓库时代开始--Hive
一.什么是 Apache Hive? Apache Hive 是一个基于 Hadoop Haused 构建的开源数据仓库系统,我们使用它来查询和分析存储在 Hadoop 文件中的大型数据集.此外,通过 ...
- 随记181120Service Fabric问题
https://github.com/Azure/service-fabric-issues/issues/1056 不能启动node one /five 问题