自定义检视面板的使用:

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

[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. DU1525 Euclid's Game 博弈

    HDU1525 Euclid's Game 博弈 题意 给定两个数字 a, b. 每次只能用 较大的值 减去 较小的值的倍数, 两个人轮流进行操作, 第一个得到 0 的胜利. 分析 对于 a == b ...

  2. 【Node.js】Jade视图模板的使用

    跟MVC里面的Rezor做差不多的事儿,但是比Rezor弱了一些,比较不喜欢CoffeeScript.Jade这种靠缩进来维系层级结构的做法,就好比接受不了c#中if下面只有一句很长的代码,但是却不加 ...

  3. 用HTML编写迪士尼乐园页面

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/html"><head lang="e ...

  4. Matplotlib——中级

    关于Matplotlib的愚见 初级中,我只是简单介绍了Matplotlib的使用方法,在中级部分,我系统地说一下我总结的内容. 上图是我画的关于Matplotlib几个对象之间的关系图.它们都来自于 ...

  5. js中哪些值会被认为false?

    在javascript中,只有 false null undefined 空字符串 即 “” 数字  0 数字 NaN 会被当作false,其余都是真. 注:字符串 “false” 也会被当作真.

  6. JavaScript document对象

    1.document对象是window对象的子对象,可直接使用,多用于获取HTML页面元素 2.document对象属性 a) alinkColor活动链接颜色 b) linkColor文本链接颜色 ...

  7. #leetcode刷题之路13-罗马数字转整数

    罗马数字包含以下七种字符: I, V, X, L,C,D 和 M.字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做 II ,即为两个并列的 1.12 写 ...

  8. jQuery树形控件zTree使用小结

    作者:Fonour 字体:[增加 减小] 类型:转载 时间:2016-08-02我要评论 这篇文章主要为大家详细介绍了jQuery树形控件zTree使用方法,zTree树插件的基本使用方法,感兴趣的小 ...

  9. Vue清除所有JS定时器

    Vue清除所有JS定时器 在webpack + vue 的项目中如何在页面跳转的时候清除所有的定时器 JS定时器会有一个返回值(数字),通过这个返回值我们可以找到这个定时器 在vue项目中可以使用路由 ...

  10. css中三种隐藏方式

    1.overflow 溢出隐藏 overflow:hidden 2.display 隐藏不占据原来的文档,即会让出空间 display:black  显示 display:none  隐藏 3.vis ...