第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 游戏物体.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

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