一、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. Dubbo实践(十二)Refer

    Spring在启动Dubbo客户端应用时,会实例化ReferenceBean<T>并设置配置属性,然后调用ReferenceConfig中的get方法: public synchroniz ...

  2. C编程规范, 演示样例代码。

    /*************************************************************** *Copyright (c) 2014,TianYuan *All r ...

  3. iOS TabBarItem设置红点(未读消息)

    实现原理: 其实是自定义一个view,将view添加到UITabBar上面,也可以是一个按钮,设置背景图片,和label.废话少说直接上代码搞一个UITabBar的分类 #import <UIK ...

  4. 【js】 Uncaught RangeError: Invalid string length

    今天项目比较催的比较着急,浏览器总是崩溃,后来报了一个Uncaught RangeError: Invalid string length(字符串长度无效) 的错误. 在ajax请求后得到的json数 ...

  5. vue 复习(2)v-bind的应用 v-bind:classv-binf:style

    dasdclass与style绑定v-bind 1. 绑定HTML Class 对象语法 有些时候我们想动态的切换class的类名.在原生的js或jq中我们就要通过事件来动态的改变class类名,但在 ...

  6. ueditor getshell漏洞重现及分析

    0x00 概述 8月21日,网上爆出ueditor .net版本getshell漏洞,由于只校验ContentType而没校验文件后缀导致getshell. 0x01 漏洞重现 Payload: &l ...

  7. Vue 自动获取最新的Vue文件

    <script src="https://unpkg.com/vue/dist/vue.min.js"></script>

  8. Bootstrap源码解读之栅格化篇

    本文纯属自己研究所写笔记,如果有错误还请多多指教提出 版心(container) 版心:class名为.container的容器,其版心的宽度在各个屏幕设备下是不一样的值,版心两边就是留白. 各尺寸下 ...

  9. Call to a member function allowField() on null 错误总结

    Call to a member function allowField() on null 在空对象上调用  allowField() 没有该模型对象无法调用,需要创建相应的模型 错误版本: if ...

  10. 跟着马哥学python-day02

    1. 运算符 计算机可以进行的运算有很多种,可不只加减乘除这么简单,运算按种类可分为算数运算.比较运算.逻辑运算.赋值运算.成员运算.身份运算.位运算. 1.1 算数运算 以下假设变量:a=10,b= ...