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 ...
随机推荐
- git编译安装报错 http-push.c:20:19: 警告:expat.h:没有那个文件或目录
解决: [root@hdoop3 git-2.18.1]# yum install expat-devel
- 软工之404 Note Found 队选题报告
目录 NABCD分析引用 N(Need,需求): A(Approach,做法): B(Benefit,好处): C(Competitors,竞争): D(Delivery,交付): 初期 中期 个人贡 ...
- vue项目中分享到朋友圈,调用微信接口
虽然微信提供了jssdk,不代表可以点击按钮进行分享到朋友圈,是需要微信自带的浏览器右上角进行分享.手机浏览器需要浏览器支持分享到朋友圈的分享机制. 微信jssdk地址: https://mp.wei ...
- Web前端---HTTP协议
目录 HTTP协议 一.http协议概述 二.http请求报文 1.GET请求 2.POST请求 三.http响应报文 1.响应报文内容 2.状态码(Status Code) HTTP协议 一.htt ...
- OSI参考模型和TCP/IP模型基本知识
OSI七层模型 为了解决网络之间的兼容性问题,实现网络设备间的相互通信,ISO于1984年提出的OSI参考模型(开放系统互连参考模型).但是由于种种原因,并没有一种完全忠实于OSI参考模型的协议族流行 ...
- 树莓派ubuntu系统下修改config.txt文件 树莓派config.txt文件修改记录
原文:https://www.raspberrypi.org/documentation/configuration/config-txt.md译文:http://my.oschina.net/fun ...
- Node.js 引用 gm 包错误 Error: Could not execute GraphicsMagick/ImageMagick
今天在学习前后台图像剪切时,下载了有图片剪切瑞士军刀之称的 GraphicsMagick. 给 gm.exe 配置了环境变量,在 npm 下好了 gm 的模块,但是运行却出现了错误. 错误如图: [E ...
- Vue2.5入门-2
todolist功能开发 代码 <!DOCTYPE html> <html> <head> <title>vue 入门</title> &l ...
- lncRNA芯片重注释
.caret,.dropup>.btn>.caret{border-top-color:#000!important}.label{border:1px solid #000}.table ...
- 目标反射回波检测算法及其FPGA实现 之一:算法概述
目标反射回波检测算法及其FPGA实现之一:算法概述 前段时间,接触了一个声呐目标反射回波检测的项目.声呐接收机要实现的核心功能是在含有大量噪声的反射回波中,识别出发射机发出的激励信号的回波.我会分几篇 ...