自定义检视面板的使用:

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

[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. 安装mysql的时候提示1045错误的解决方法

    在安装mysql的时候提示1045错误,如图所示: 这种情况一般是之前卸载msyql的时候没有清理完一些文件之类的,导致给你提示存在安全问题,因此,只需要找到mysql一些系统的配置文件,并且将他们删 ...

  2. 【题解】洛谷P1074 [NOIP2009TG] 靶形数独(DFS+剪枝)

    洛谷P1074:https://www.luogu.org/problemnew/show/P1074 思路 这道题一看就是DFS 打一个分数表方便后面算分 我用x y z数组分别表示行 列 宫 是否 ...

  3. MVC验证码生成类库

    public class ValidateCode { /// <summary> /// 验证码的最大长度 /// </summary> public int MaxLeng ...

  4. trunc(sysdate)的含义是什么

    1.ORACLE中TRUNC是截取的函数

  5. Java并发包:AtomicBoolean和AtomicReference

      AtomicBoolean AtomicBoolean是一个读和写都是原子性的boolean类型的变量.这里包含高级的原子操作,例如compareAndSet().AtomicBoolean位于J ...

  6. 『C++』Temp_2018_12_26_02

    #include <iostream> #include <string> #include <stdio.h> #include <stdlib.h> ...

  7. MySQL学习之事务安全

    事务安全 事务概念 事务(transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit),事务通常由高级数据操纵语言或编程语言 书写的用户程序的执行所引起.事务有事务开始(b ...

  8. Vue directive自定义指令+canvas实现H5图片压缩上传-Base64格式

    前言 最近优化项目-手机拍照图片太大,回显速度比较慢,使用了vue的自定义指令实现H5压缩上传base64格式的图片 canvas自定义指令 Vue.directive("canvas&qu ...

  9. Python学习 :迭代器&生成器

    列表生成式 列表生成式的操作顺序: 1.先依次来读取元素 for x 2.对元素进行操作 x*x 3.赋予变量 Eg.列表生成式方式一 a = [x*x for x in range(10)] pri ...

  10. python3 练习题100例 (六)

    题目六:斐波那契数列(Fibonacci sequence),又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……. #!/usr/bin/env python3 ...