第02章 标准编辑器扩展 Standard Editor Extensions

原地址: http://anchan828.github.io/editor-manual/web/part2-standardextension.html

使用预制的附加特性

2.1 更改 inspector 外观

01. Range

这是一个允许使用滑动条更改 int, float, long 和 double 数值的函数
在开发的时候, 可以使我们 public 的一些字段控制在一定范围, 而不是随意的修改值.
操作

  1. 附加下面脚本到游戏物体.
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    [Range(1, 10)]
    public int num1;

    [Range(1, 10)]
    public float num2;

    [Range(1, 10)]
    public long num3;

    [Range(1, 10)]
    public double num4;
}

对比

未添加 Range 和添加 Range 对比

02. Multiline / TextArea

文本区默认是一行的, 但是更改为多行文本区, Multiline 和 TextAreal 几乎拥有相同的功能, 但是 Multiline 有些限制, 比如: 没有自动换行适应, 没有滚动条.
推荐使用 TextAreal, 除非有特殊需求.
操作

  1. 附加脚本
using UnityEngine;
using System.Collections;

public class NewBehaviourScript : MonoBehaviour
{
    [Multiline(5)]
    public string multiline;
    [TextArea(3, 5)]
    public string textArea;
}

效果

普通文本, Multiline 和 TextArea 对比

2.2 Adding functions handled by Inspector

01. ContextMenuItem

inspector 中, 在 public 字段处添加一个 context menu.
必须想要单独充值一个字段值, 那么此处会用到..

`在字段处单击右键弹出`

操作

  1. 添加如下代码
using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    [ContextMenuItem ("Random", "RandomNumber")]
    [ContextMenuItem ("Reset", "ResetNumber")]
    public int number;

    void RandomNumber ()    {
        number = Random.Range (0, 100);
    }

    void ResetNumber ()  {
        number = 0;
    }
}
02. ColorUsage

We use a color picker to change color. ColorUsage enables you to enable / disable alpha use in the color picker or change it to a color picker for HDR.

左: 默认颜色选择器, 中: 不带 alpha, 右: HDR 选择器

操作

using UnityEngine;

public class NewBehaviourScript : MonoBehaviour
{
    public Color color1;

    [ColorUsage (false)]
    public Color color2;

    [ColorUsage (true, true, 0, 8, 0.125f, 3)]
    public Color color3;
}

2.3 调整 inspector 显示.

此小结并不会取直接修改属性, 却会使它看起来更工整写.

01. Header

如图:

image.png

操作:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class HeaderTest : MonoBehaviour {

    [Header("__ PlayerSettings __")]
    public Player player;
    [Serializable]
    public class Player
    {
        public string name;
        [Range(0, 100)]
        public int hp;
    }
    [Header("__ GameSettings __")]
    public Color background;
}

02. Space

给两个字段设置边距.
如图:

image.png

操作:

public class SpaceTest : MonoBehaviour {
    public string str1;
    [Space(5)]
    public string str2;
    [Space(10)]
    public string str3;
}
03. Tooltip

你也可以给字段添加一段描述..


image.png
public class ToolTipTest : MonoBehaviour {
    [Tooltip("__ display Tooltip __")]
    public long toolTip;
}
04. 在 inspector 中隐藏字段
image.png
public class NewBehaviourScript : MonoBehaviour
{
    public string str1;

    [HideInInspector]
    public string str2;
}

2.4 让 inspector 更方便些..

01. RequireComponent

当在脚本组件中需要 Animator 组件的时候, 假如忘记了添加 Animator, 那就会出错, 此时可以使用 RequireComponent 提前防止此错误.
添加使用 RequireComponent 附加的脚本时候也会自动添加, 如果已经添加, 那什么都不会做, 当尝试删除这个组件时候, 就会弹出"无法删除"对话框.

当尝试删除 Animator 的时候会有弹窗提示.
[RequireComponent(typeof(Animator))]
public class NewBehaviourScript : MonoBehaviour
{
    Animator animator;

    void Awake ()
    {
        animator = GetComponent<Animator> ();
    }
}
02. DisallowMultipleComponent (禁用重复组件)

这是一个禁止将相同组件添加到游戏物体的特性..


尝试附加相同脚本时会显示一个对话框
[DisallowMultipleComponent]
public class Base : MonoBehaviour
{
}
03. Formerly SerializedAs

在更改变量名的时候, 将变量值转到新的变量..
在 Inspector 中现实 SerializeField 的字段. 如果在这儿修改了字段名, 那么数据也会丢失.

public class SerializeAsTest : MonoBehaviour {
    [SerializeField]
    string hoge;
}

在 inspector 中输入值


image.png

将字段名从 hoge 更改为 fuga

public class SerializeAsTest : MonoBehaviour {
    [SerializeField]
    string fuga;
}
~

使用 FormerlySeriallizedAs

using UnityEngine;
using UnityEngine.Serialization;

public class NewBehaviourScript : MonoBehaviour
{
    [SerializeField]
    [FormerlySerializedAs("hoge")]
    string fuga;
}

The point to watch out here is that changing the variable name, adding the FormerlySerializedAs attribute, and specifying oldName must be done at the same time . Data that is not needed by compiling the script is discarded, and even if you changed the variable name (compiled for the first time), you forgot to specify FormerlySerializedAs and even if you add hastily (compile the second time) It has been destroyed. This is because data is discarded in the second compilation.

04. AddComponentMenu

在"Component" 菜单中添加项
所有的脚本/组件都会在 component 中进行分组, 如果我们像把自己定义的脚本组件归类到 component 中, 使用 AddComponentMenu;

[AddComponentMenu("MyUI/MyTweenColor")]
public class AddComponentMenuTest : MonoBehaviour {
}
image.png

此时选中游戏物体, 点击菜单就会添加我们自定义的脚本组件.
如果未选中游戏无图, 菜单是灰色未激活的状态..

2.5 Ease the development of games

01. 在 Editor 模式下 执行

即使游戏未运行, 那么也可以执行 继承Monobehaviour的脚本.
测试后, 脚本添加到游戏物体时候会进行执行, update 会执行多次,
当有脚本更改后, Unity 进行编译, Update 会再次执行.

using UnityEngine;

[ExecuteInEditMode]
public class NewBehaviourScript : MonoBehaviour
{
    [Range(0,10)]
    public int number;
    void Awake ()    {
        Debug.Log ("Awake");
    }
    void Start (){
        Debug.Log ("Start");
    }
    void Update ()    {
        Debug.Log ("Update");
    }
}
02. ContextMenu;

在组件的 Context Menu 中执行 方法,


image.png
public class ContextMenuTest : MonoBehaviour {
    [Range(0, 10)]
    public int number = 0;

    [ContextMenu("MyRandomNumber")]
    void RandomNumber()    {
        number = Random.Range(0, 10);
    }

    [ContextMenu("MyResetNumber")]
    void ResetNumber()    {        
        number = 0;
    }
}
03. SelectionBase

在场景中选择游戏物体时使用它, 指定要选择的游戏物体, 或者游戏物体的选择顺序,

  1. 新建空游戏物体,
  2. 新建子物体 Cube
    此时:


    当在场景中选择游戏物体时候, 默认选择 cube

给 GameObject 空游戏物体附加脚本

[SelectionBase]
public class NewBehaviourScript : MonoBehaviour
{
}

此时再点击选择场景中的 cube 游戏物体, 会优先选择 Cube 的父物体.再次点击 cube Hierarchy 中才会选中 Cube 游戏物体.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,497评论 18 139
  • 有时候,明明心如刀割,却要灿烂的微笑。明明很脆弱,却表现的如此坚强。眼泪在眼里打转,却告诉每个人我很好。
    西恩Matcha阅读 264评论 0 0
  • 我一直都有一个疑问,对着自己。到底爱一个人该是什么样子?是为了结婚还是为了流浪?我一直想对着爱人喊她的名字,拉着手...
    云中君的风阅读 150评论 0 1