自定义检视面板的使用:

先是定义一个脚本文件,我们来修饰它的检视面板:

[HelpURL("http://www.baidu.com")]
public class Atr : MonoBehaviour
{
public int id;
public string Name;
[Multiline()]
public string BackStory; public float health;
public float damage; public float weaponDamagel, weaponDamage2;
public string shoeName;
public int shoeSize;
public string shoeType; [Space()]
[Range(-,)]
public int time;
void Start ()
{
health = ;
}
}

然后在根目录的Editor文件夹下定义一个用来修饰上面脚本检视面板的类文件:

using UnityEngine;
using System.Collections;
using UnityEditor; [CustomEditor(typeof(Atr))]
//需要继承自editor,并且引入UnityEditor程序集
public class LearnInspector : Editor
{
private Atr atr;
private bool showWeapons; void OnEnable()
{
//获取当前自定义的Inspector对象
atr = (Atr) target;
} //执行该函数来自定义检视面板
public override void OnInspectorGUI()
{
//不写默认是垂直布局
EditorGUILayout.BeginVertical(); //空格
EditorGUILayout.Space();
EditorGUILayout.Space(); EditorGUILayout.LabelField("Base Info");
atr.id = EditorGUILayout.IntField("Atr ID", atr.id);
atr.Name = EditorGUILayout.TextField("Atr Name", atr.Name); EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space(); EditorGUILayout.LabelField("Back Story");
atr.BackStory = EditorGUILayout.TextArea(atr.BackStory, GUILayout.MinHeight()); EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space(); atr.health = EditorGUILayout.Slider("Health", atr.health, , ); if (atr.health < )
{
GUI.color = Color.red;
}
else if (atr.health>)
{
GUI.color = Color.green;
}
else
{
GUI.color = Color.grey;
} Rect progressRect = GUILayoutUtility.GetRect(, ); EditorGUI.ProgressBar(progressRect,atr.health/100.0f,"Health"); GUI.color = Color.white; EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space(); atr.damage = EditorGUILayout.Slider("Damage", atr.damage, , ); if(atr.damage<)
{
EditorGUILayout.HelpBox("伤害过低",MessageType.Error);
}
else if (atr.damage > )
{
EditorGUILayout.HelpBox("伤害过高",MessageType.Warning);
}
else
{
EditorGUILayout.HelpBox("伤害适中",MessageType.Info);
} EditorGUILayout.Space();
EditorGUILayout.Space();
EditorGUILayout.Space(); EditorGUILayout.LabelField("Shoe");
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Name", GUILayout.MaxWidth());
atr.shoeName = EditorGUILayout.TextField(atr.shoeName);
EditorGUILayout.LabelField("Size", GUILayout.MaxWidth());
atr.shoeSize = EditorGUILayout.IntField(atr.shoeSize); EditorGUILayout.LabelField("Type", GUILayout.MaxWidth());
atr.shoeType = EditorGUILayout.TextField(atr.shoeType); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical();
}
} //绘制字段用到的方法 //EditorGUILayout.LabelField()标签字段
//EditorGUILayout.IntField() 整数字段
//EditorGUILayout.FloatField() 浮点数字段
//EditorGUILayout.TextField() 文本字段
//EditorGUILayout.Vector2Field() 二维向量字段
//EditorGUILayout.Vector3Field() 三维向量字段
//EditorGUILayout.Vector4Field() 四维向量字段

可以看出该修饰类和效果图对应的关系。我们可以方便的定义检视面板来协助游戏的开发调试,让它直观的显示出帮助消息。

更多的信息可以查看帮助文档:http://www.ceeger.com/Script/Editor/Editor.html


自定义窗口:

 using System;
using UnityEngine;
using System.Collections;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement; public class MyFirstWindow : EditorWindow
{ public Texture TxTexture;
string bugReporterName = "";
string description = "";
GameObject buggyGameObject; MyFirstWindow()
{
this.titleContent=new GUIContent("Bug Rt");
} [MenuItem("Tool/Bug Reporter")]
static void showWindow()
{
EditorWindow.GetWindow(typeof (MyFirstWindow));
} //绘制窗口界面
void OnGUI()
{
GUILayout.BeginVertical(); GUILayout.Space();
GUI.skin.label.fontSize = ;
GUI.skin.label.alignment = TextAnchor.MiddleCenter;
GUILayout.Label("Bug Report"); GUILayout.Space();
bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName); GUILayout.Space();
GUI.skin.label.fontSize = ;
GUI.skin.label.alignment = TextAnchor.UpperLeft;
GUILayout.Label("Currently Scene:"+EditorSceneManager.GetActiveScene().name); GUILayout.Space();
GUILayout.Label("Time:"+System.DateTime.Now); GUILayout.Space();
buggyGameObject =
(GameObject) EditorGUILayout.ObjectField("Buggy Go", buggyGameObject, typeof (GameObject), true); GUILayout.Space();
GUILayout.BeginHorizontal();
GUILayout.Label("Description", GUILayout.MaxWidth());
description = EditorGUILayout.TextArea(description, GUILayout.MaxHeight());
GUILayout.EndHorizontal(); EditorGUILayout.Space(); if (GUILayout.Button("Save Bug"))
{
SaveBug(); } if (GUILayout.Button("Save Bug With Screenshoot"))
{
SaveBugWithScreeshot();
} EditorGUILayout.EndVertical();//布局开始和结束相对应,缺少时可能出现窗口中的元素无法自适应的情况
} private void SaveBugWithScreeshot()
{
Writer();
Application.CaptureScreenshot("Assets/BugReports/" + bugReporterName + "/" + bugReporterName + ".png");
} private void SaveBug()
{
Writer();
} //IO类,用来写入保存信息
void Writer()
{
Directory.CreateDirectory("Assets/BugReports/" + bugReporterName);
StreamWriter sw = new StreamWriter("Assets/BugReports/" + bugReporterName + "/" + bugReporterName + ".txt");
sw.WriteLine(bugReporterName);
sw.WriteLine(DateTime.Now.ToString());
sw.WriteLine(EditorSceneManager.GetActiveScene().name);
sw.WriteLine(description);
sw.Close();
}
}

自定义菜单项:

 using UnityEditor;
using UnityEngine;
using System.Collections; /// <summary>
/// 工具类改名
/// </summary>
public class ChangeName : ScriptableWizard
{ public string PrefixStr = null; /// <summary>
/// 当没有任何GameObject被选中的时候,将菜单disable(注意,这个函数名可以随意取)
/// </summary>
/// <returns></returns>
[MenuItem("ChangeName/AddPrefixAndEuipment", true)]
static bool CreateWindowDisabled()
{
return Selection.activeTransform;
} /// <summary>
/// 创建编辑窗口(注意,这个函数名可以随意取)
/// </summary>
[MenuItem("ChangeName/AddPrefixAndEuipment")]
static void CreateWindow()
{
//第一个参数窗口标题
// 定制窗口标题和按钮,其中第二个参数是Create按钮,第三个则属于other按钮
// 如果不想使用other按钮,则可调用DisplayWizard的两参数版本
DisplayWizard<ChangeName>(
"AddPrefix",
"Add", "Remove");
} /// <summary>
/// 窗口创建或窗口内容更改时调用
/// </summary>
void OnWizardUpdate()
{
helpString = "Note: Prefix are not created"; if (string.IsNullOrEmpty(PrefixStr))
{
errorString = "Please enter Prefix";
isValid = false;
}
else
{
errorString = "";
isValid = true;
}
} /// <summary>
/// 点击Add按钮(即Create按钮)调用
/// </summary>
void OnWizardCreate()
{ Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); foreach (Transform transform in transforms)
{
transform.name = PrefixStr + transform.name; } Debug.Log("AddPrefixAndEuipment " + PrefixStr+"successed!");
} /// <summary>
/// 点击Remove(即other按钮)调用
/// </summary>
void OnWizardOtherButton()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); foreach (Transform transform in transforms)
{
if (transform.name.Contains(PrefixStr))
{
transform.name=transform.name.Replace(PrefixStr, "");
}
} Debug.Log("ReMove prefix " + PrefixStr + "successed!");
}     
[MenuItem("ChangeName/AddOrderNume2Name ")]// 为选择的对象名称添加序列号,让重名的物体相区别
static void AddOrder2Name()
{
int n = ;
Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); foreach (Transform transform in transforms)
{
transform.name = transform.name + n.ToString();
n++;
}
} [MenuItem("GameObject/Add Child %g")]
static void MenuAddChild()
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.TopLevel | SelectionMode.OnlyUserModifiable); foreach (Transform transform in transforms)
{
GameObject newChild = new GameObject("_Child");
newChild.transform.parent = transform; }
} [MenuItem("ChangeName/ReNameUILabelX")]
static void ReNameUILableX()
{
UIButton [] UIButtions = GameObject.Find("CenterMenu").transform.GetChild().GetComponentsInChildren<UIButton>(); foreach (UIButton uiButtion in UIButtions)
{
if (uiButtion.gameObject.activeSelf)
{
uiButtion.GetComponentInChildren<EptBtnOnClick>().EptName="X_"+ uiButtion.GetComponentInChildren<UILabel>().text;
}
}
} }
ScriptableWizard:
继承自EditorWindow,主要用来做向导。有2个按钮,一个是Create,另一个是Other。当我们使用它的时候,他始终显示在最上层,不会被unity其他窗口遮挡。

   SelectionMode:

can be used to tweak the selection returned by Selection.GetTransforms.

Note: This is an editor class. To use it you have to place your script in Assets/Editor inside your project folder. Editor classes are in the UnityEditor namespace so for C# scripts you need to add "using UnityEditor;" at the beginning of the script.

The default transform selection mode is: SelectionMode.TopLevel | SelectionMode.ExcludePrefab | SelectionMode.Editable.

Unfiltered

Return the whole selection.

TopLevel

Only return the topmost selected transform. A selected child of another selected transform will be filtered out.

Deep

Return the selection and all child transforms of the selection.

ExcludePrefab

Excludes any prefabs from the selection.

Editable

Excludes any objects which shall not be modified.

Assets

Only return objects that are assets in the Asset directory.

DeepAssets

If the selection contains folders, also include all assets and subfolders within that folder in the file hierarchy.

补充(190426):

一键导出包,非常方便迁移测试程序或者自己的代码库

 using System;
#if UNITY_EDITOR
using UnityEditor;
using System.IO;
#endif
using UnityEngine; namespace WSFramework{ public class ExportPakge{ #if UNITY_EDITOR
[MenuItem("WSTool/ExportPac %e")]
private static void ExportPackageAndCopyName(){
var path="Assets/WSFramework";
var fileName="WS_"+DateTime.Now.ToString("yyyyMMdd_hh")+".unitypackage"; //--剪切板API,导出后直接粘贴出当前包的名字
GUIUtility.systemCopyBuffer=fileName; //--导出package api
AssetDatabase.ExportPackage(path,fileName, ExportPackageOptions.Recurse); //--打开asset目录
Application.OpenURL("file://"+Application.dataPath); }
} #endif
}

assetstore一个很优秀的编辑器扩展插件,只需要在类中添加 attribute标签就能方便的实现很多功能,有兴趣的可以下载学习下

Unity 编辑器扩展的更多相关文章

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

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

  2. Unity编辑器扩展chapter1

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

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

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

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

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

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

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

  6. Unity编辑器扩展

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

  7. Unity 编辑器扩展 Chapter2—Gizmos

    二. 使用Gizoms绘制网格及矩阵转换使用 1. 创建Leve类,作为场景控制类: using UnityEngine; //使用namespace方便脚本管理 namespace RunAndJu ...

  8. unity编辑器扩展学习

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

  9. Unity 编辑器扩展自定义窗体

    这次看见Unity还可以自定义弹出窗体,让我很好奇.于是就去网上找文章看了看. 如果想自定义窗体需要把类放入Editor文件夹下面. 代码如下: using UnityEngine; using Un ...

随机推荐

  1. 阿里云linux服务器登录失败,Connection closed

    ssh_exchange_identification: read: Connection reset by peer报错如下: [root@izbp17x1~]# ssh admin@139.196 ...

  2. java连接数据库增删改查公共方法

    package dao; import java.io.IOException; import java.sql.CallableStatement; import java.sql.Connecti ...

  3. MVC学习十二:Ajax.ActionLink用法

    Ajax.ActionLink用法 <!--使用Ajax.BeginForm必须引用的js文件--> <script type="text/javascript" ...

  4. HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...

  5. Flex布局(一)flex-direction

    采用Flex布局的元素,被称为Flex容器(flex container),简称"容器".其所有子元素自动成为容器成员,成为Flex项目(Flex item),简称"项目 ...

  6. 【最新】LuaJIT 32/64 位字节码,从编译到使用全纪录

    网上关于 LuaJIT 的讨论,已经显得有些陈旧.如果你对 LuaJIT 编译 Lua 源文件为具体的 32位或64位字节码,极其具体使用感兴趣的话,不妨快速读一下这篇文章.此文章针对尝试在 iOS ...

  7. MySQL学习【SQL语句上】

    1.连接服务端命令 1.mysql -uroot -p123 -h127.0.0.1 2.mysql -uroot -p123 -S /tmp/mysql.sock 3.mysql -uroot -p ...

  8. 转:30分钟了解Springboot整合Shiro

    引自:30分钟了解Springboot整合Shiro 前言:06年7月的某日,不才创作了一篇题为<30分钟学会如何使用Shiro>的文章.不在意之间居然斩获了22万的阅读量,许多人因此加了 ...

  9. 消息队列MSMQ的使用

    1.MSMQ安装 控制面板-程序和功能-打开或关闭Windows功能-Microsoft Message Queue(MSMQ)服务器,选中所有,点击确定. 2.消息队列的应用场景(转载自http:/ ...

  10. vmware 虚拟机设置 redhat 桥接模式

    1.设置 vmware 网络模式 2.设置 linux 网络模式